GithubHelp home page GithubHelp logo

Comments (6)

aktug avatar aktug commented on June 23, 2024 1

Thank you for PR, we'll look at your changes but there are different development processes in our team so at this time we can't merge your PR directly, thank you @KholofeloMoyaba for your contribution 🍻

from hms-flutter-plugin.

atavci avatar atavci commented on June 23, 2024

By examining the logs, I can say that you are able to get the token from the server (0 result code means success). Are you listening the EventChannel that emits this token ? The plugin's example project displays a demo on this, the code is shown below:

  String _token = '';
  static const EventChannel TokenEventChannel = EventChannel(Channel.TOKEN_CHANNEL);

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  Future<void> initPlatformState() async {
    if (!mounted) return;
    TokenEventChannel.receiveBroadcastStream()
        .listen(_onTokenEvent, onError: _onTokenError);
  }

  void _onTokenEvent(Object event) {
    setState(() {
      _token = event;
    });
    showResult("TokenEvent", event);
  }

  void _onTokenError(Object error) {
    setState(() {
      _token = error;
    });
    showResult("TokenEvent", error);
  }

If you are unable to obtain the token even after listening the TokenEventChannel, can you provide us with more details ?

  • Huawei Push Kit Plugin version on pubspec.yaml
  • Output from flutter doctor command
  • EMUI Version

from hms-flutter-plugin.

KholofeloMoyaba avatar KholofeloMoyaba commented on June 23, 2024

Hi @atavci

Thanks for the guidance. Adding the token event channel does work.

However, it still confuses me.

My understanding is that listening to TokenEventChannel stream is for listening to any changes on the token channel retrospectively.

What I'm trying to do, however, is request the token on demand. Push.getToken has a method signature as follows: Future<String> getToken(). So I'm expecting this method to return the token on demand - outside of subscribing to the event channel.

For example, I may need specific hooks where I need to get the token at that point. So I would expect to Push.getToken and get a value.

from hms-flutter-plugin.

KholofeloMoyaba avatar KholofeloMoyaba commented on June 23, 2024

@atavci / @Mike-mei

I looked through the plugin source code and I see the issue.

The issue is that the android code for handling the getToken method doesn't accept the MethodChannel.Result object to pass the result back to flutter. It does, however, trigger the token event channel. This, in my opinion, is incorrect - or at least inconsistent behaviour. If the intent isn't for Push.getToken to return the token value, then the method should be updated accordingly (e.g. it should return void) and documented properly.

Here is a link to the part of the code that's the culprit.
https://github.com/HMS-Core/hms-flutter-plugin/blob/master/flutter-hms-push/android/src/main/java/com/huawei/hms/flutter/push/hms/FlutterHmsInstanceId.java#L65

I can make a PR to fix this if you would allow that.

from hms-flutter-plugin.

aktug avatar aktug commented on June 23, 2024

Hi @KholofeloMoyaba ;
First of all, thank you very much for your concerns,
While we were developing the plugin, we paid special attention to support EMUI 10 and lower versions. Because;

If the EMUI version is 10.0 or later on a Huawei device, a token will be returned through the getToken method. If the getToken method fails to be called, Huawei Push Kit automatically caches the token request and calls the method again. A token will then be returned through the onNewToken method.
If the EMUI version on a Huawei device is earlier than 10.0 and no token is returned using the getToken method, a token will be returned using the onNewToken method.

But as you pointed out, that section has escaped from attention. Actually it's okay, we can get token with listening "TOKEN_CHANNEL" as it is, we'll fix the return type in our next version.

Thanks again @KholofeloMoyaba for your interest ~

from hms-flutter-plugin.

KholofeloMoyaba avatar KholofeloMoyaba commented on June 23, 2024

Hi again @aktug

Thanks for your response. Yes, I think the issue which is problematic on my side is just the communication back to flutter via the MethodChannel.Result. I don't have an issue with anything EMUI related.

I've already made and tested a fix which would make it much easier for other Flutter developers. I don't have the rights to make a PR directly against this repo, so I made a fork. Please see below the PR from the forked repo:
#7

I have already tested this on a flutter app running on a Huawei device and it works, while still respecting the TOKEN_CHANNEL. So nothing is lost.

Just to give you an example of how it becomes an issue on the flutter side using the current version:

  1. You basically have a request to getToken and you won't know within the same context if you got a reply or not.
  2. You then have to listen to the TOKEN_CHANNEL in another part of the code and put it in some sort of persistent storage.
  3. Finally, in the original getToken you need to add artificial delay and check the persistent storage to see if you have a result or not.

So it's possible, but it becomes very hard to have an on-demand implementation of getting a token on Flutter. The PR I supplied fixes this while keeping the TOKEN_CHANNEL intact.

from hms-flutter-plugin.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.