Unregistered Registration Token in Firebase

2019-02-11 19:42发布

问题:

I got unregistered registration token even I am sure my token is correct and I check it in my log I am using master token FirebaseInstanceId.Instance.Token.

Here is my method:

private void ConfigureFireBase()
        {

            Task.Run(() => {
                var instanceId = FirebaseInstanceId.Instance;
                Android.Util.Log.Debug("TAG", "{0} {1}", instanceId?.Token?.ToString(), instanceId.GetToken(GetString(Resource.String.gcm_defaultSenderId), Firebase.Messaging.FirebaseMessaging.InstanceIdScope));

            });

        }

I check as well OnTokenRefresh method the same token

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

but when I tried in Firebase console it gives me this error message, when I tried in http://pushtry.com/ with the same token I got not NotRegistered message

Note when I uninstall the app and install again the token working, but after while I got this error message.

回答1:

The reason why this issue fired cause that token is unregistered

The registration token may change when:

The app deletes Instance ID
The app is restored on a new device
The user uninstalls/reinstall the app
The user clears app data.

Reference

and this happen in debug mode only so dont worry in release mode every thing will be fine.

How you can fix the issue ?

its easy just force to refresh token call this method in your landing activity (MainActivity , Login ) , this method force firebase to call OnTokenRefresh()

private void ConfigureFireBase()
        {

#if DEBUG

            Task.Run(() =>
            {
                var instanceId = FirebaseInstanceId.Instance;
                instanceId.DeleteInstanceId();
                Android.Util.Log.Debug("TAG", "{0} {1}", instanceId?.Token?.ToString(), instanceId.GetToken(GetString(Resource.String.gcm_defaultSenderId), Firebase.Messaging.FirebaseMessaging.InstanceIdScope));

            });

            // For debug mode only - will accept the HTTPS certificate of Test/Dev server, as the HTTPS certificate is invalid /not trusted
            ServicePointManager.ServerCertificateValidationCallback += (o, certificate, chain, errors) => true;


#endif
        }

Hope this help any one face same issue



回答2:

In my case i had used Emulator and the Firewall was blocking it. I used my mobile, enabled the developer options and it got worked.

Hope this helps.