xamarin / GoogleApisForiOSComponents

MIT License
225 stars 161 forks source link

When the application is restarted, "messaging: didReceiveRegistrationToken:" returns the old token. #465

Closed monster74 closed 3 years ago

monster74 commented 3 years ago

Xamarin.Firebase.iOS.CloudMessaging 4.7.1 Xamarin.Forms 4.8.0.1687

I update the token in the application. The new token turns out fine. Everything is working. But after restarting the application, "messaging: didReceiveRegistrationToken:" is called again and the old inactive token comes. This was not the case before. Previously, "messaging: didReceiveRegistrationToken:" was called only once the first time the application was launched. Tell me what to do?

monster74 commented 3 years ago

Token refresh code:

        public void RefreshToken()
        {
            try
            {
                Firebase.InstanceID.InstanceId instanceID = Firebase.InstanceID.InstanceId.SharedInstance;
                string scope = Firebase.InstanceID.InstanceId.ScopeFirebaseMessaging;
                instanceID.DeleteToken(_authorizedEntity, scope, new Firebase.InstanceID.InstanceIdDeleteTokenHandler((errorDeleteToken) =>
                {
                    if (errorDeleteToken != null)
                    {
                        GlobalToast.Toast.ShowToast(errorDeleteToken.DebugDescription, "Error SharedInstance.DeleteToken");
                        AutoGRAPHMobile.App.LoggingService.Error("SharedInstance.DeleteToken: " + errorDeleteToken.DebugDescription);
                    }
                    else
                    {
                        NSDictionary<NSString, NSObject> options = new NSDictionary<NSString, NSObject>
                        (
                            new NSString("apns_token"), _apns_token
                        );
                        instanceID.GetToken(_authorizedEntity, scope, options, new Firebase.InstanceID.InstanceIdTokenHandler((string newToken, NSError errorGetToken) =>
                        {
                            if (errorGetToken != null)
                            {
                                GlobalToast.Toast.ShowToast(errorGetToken.DebugDescription, "Error SharedInstance.GetToken");
                                AutoGRAPHMobile.App.LoggingService.Error("SharedInstance.GetToken: " + errorGetToken.DebugDescription);
                            }
                            else
                            {
                                SendRegistrationToServer("RefreshToken", newToken);
                            }
                        }));
                    }
                }));
            }
            catch (Exception exp)
            {
                Console.WriteLine("RefreshToken exp: " + exp.Message);
                AutoGRAPHMobile.App.LoggingService.Error("RefreshToken exp: " + exp.Message);
            }
        }
monster74 commented 3 years ago

Small addition. The "messaging: didRefreshRegistrationToken:" event is not called.