Xamarin Form: Android using Firebase Cloud Messagi

2019-01-27 05:30发布

I have made the Firebase Cloud Message Notification working even work through the Notification Hub from Azure. The time I can't receive the message is when I try to re-run the application.

Process: 1) Fresh install the application with Visual Studio IDE 2) Stop the debugger
3) Debug and run the application again through Visual Studio IDE 4) Send a test message through FCM Console

If I am not doing 3, I still can receive the message even if the application is in background

   LoadApplication(new App());

            FirebaseOptions options = new FirebaseOptions.Builder()
            .SetApplicationId(GetString(Resource.String.fcmAppId))
            .SetApiKey(GetString(Resource.String.fcmApiKey))
            .SetGcmSenderId(GetString(Resource.String.fcmGCMSenderId))
            .Build();

            FirebaseApp.InitializeApp(Android.App.Application.Context, options); 

?

    public class MyFirebaseInstanceIdService : FirebaseInstanceIdService
{
    const string TAG = "MyFirebaseInstanceIdService";

    public override void OnTokenRefresh()
    {
        var refreshedToken = FirebaseInstanceId.Instance.Token;
        Settings.NotificationToken = refreshedToken;
        Android.Util.Log.Debug(TAG, "Refreshed token: " + refreshedToken);
        SendRegistrationToServer(refreshedToken);
    }

    void SendRegistrationToServer(string token)
    {
    }
}

Initially I thought it has a different token but is the same.

Message from Azure Portal:

The token obtained from the token provider is wrong.

1条回答
相关推荐>>
2楼-- · 2019-01-27 06:11

Android using Firebase Cloud Messaging not receiving message

As the GCM docs said :

An ID issued by the GCM connection servers to the client app that allows it to receive messages.

So only when the token is available, your app can receive message from GCM.

How can an app get that token?

You could see the document :

On initial startup of your app, the FCM SDK generates a registration token for the client app instance. If you want to target single devices or create device groups, you'll need to access this token.

The onTokenRefreshcallback fires whenever a new token is generated, so calling getToken in its context ensures that you are accessing a current, available registration token. Make sure you have added the service to your manifest, then call getToken in the context of onTokenRefresh

Every time when you re-run the application in your device the token is changed, but as the document said, OnTokenRefresh is only called when the system determines that the tokens need to be refreshed, it is needed for key rotation and to handle Instance ID changes due to :

  • When the app is installed or uninstalled.
  • When the user deletes app data.
  • When the app erases the Instance ID.
  • When the security of the token has been compromised.

You need to trigger OnTokenRefresh method. You should first uninstall the app from the device, then reinstall the app and open it, the OnTokenRefresh will be triggered and the token will be updated and your app could receive GCMmessage again.

查看更多
登录 后发表回答