firebase / firebase-ios-sdk

Firebase SDK for Apple App Development
https://firebase.google.com
Apache License 2.0
5.68k stars 1.48k forks source link

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

Closed monster74 closed 3 years ago

monster74 commented 3 years ago

Sorry if I wrote to the wrong address.

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?

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);
            }
        }
google-oss-bot commented 3 years ago

I found a few problems with this issue:

monster74 commented 3 years ago

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

morganchen12 commented 3 years ago

Hey @monster74, much of our staff is currently on vacation but we'll get back to you once we're back. Apologies for the delay.

charlotteliang commented 3 years ago

@monster74 messaging: didReceiveRegistrationToken: should always be called during app launch. As long as you set the FIRMessagingDelegate correctly. This is expected behavior as chances are FCM token can be refreshed so we always return the current fcm token during app launch.

monster74 commented 3 years ago

"didReceiveRegistrationToken" is always called after application start. But if I previously updated the token, closed the application, then when the application starts, "didReceiveRegistrationToken" returns the old remote token to me.

monster74 commented 3 years ago

What am I doing. 1) I delete the token (instanceID.DeleteToken) 2) I receive a new correct token (instanceID.GetToken) 3) I close the application. 4) I open the application 5) "didReceiveRegistrationToken" returns the old token I deleted.

monster74 commented 3 years ago

If instanceID.GetToken is called in "didReceiveRegistrationToken", then instanceID.GetToken will return the correct current token.

monster74 commented 3 years ago

Am I deleting the token incorrectly?

charlotteliang commented 3 years ago

I'm not able to reproduce. I get the refreshed new token after reopen the app. It would be helpful to share the debugLog if you wish.

Other than that, couple of questions on the use case here, it seems like you delete a token every time in this function, are you calling this frequently once per app? Because it's not recommended to reset your token this frequently.

Another thing is IID token API is deprecated, to better work with messaging:didReceiveRegistrationToken, please use the getToken and deleteToken call from Messaging because the API you are using has more parameters that isn't ideal for exposure and developers might enter unexpected input to cause issues.

monster74 commented 3 years ago

` 24.12.2020 12:13 AutoGRAPHMobile Info iPhone-Anton mobile 0000:MonoTouch 1 Firebase DidReceiveRegistrationToken : fYaf_8i3ykh0oj2_hF88kJ:APA91bFRxwZhI91H9u_AetKTFQFQBp2SMmJRL3M3xeav6xcUSHfSAHOaUIna3derrw9mAWPhYBTiFFCJ0iQwy06ctnf3uSr86OiISkvfGkbMQprgh_SuNAxiMNV5Z6bo0_FQnR3BsXHa 24.12.2020 12:13 AutoGRAPHMobile Info iPhone-Anton mobile 0000:MonoTouch 1 Firebase RegisteredForRemoteNotifications : {length = 32, bytes = 0xacdf3bcd dde7ae86 c724a43b 5e283fd8 ... 0764bcf9 16524718 } 24.12.2020 12:13 AutoGRAPHMobile Info iPhone-Anton mobile 0000:MonoTouch 1 Firebase DidReceiveRegistrationToken : cfMTEI6Fh0sXm3vYlZxP4H:APA91bEpVDJoA15xhg-g3t-JPsbXOia16-QxgLJIML8w5itQ2yuH9vM3fIxfUB3gZ5K5OVwBmy0seUBpANADeAaMoA8nOZL8wGi5UbldK3INwUgA4JDtnBq6qBBcWMbXLy4tdtaCpTOk 24.12.2020 12:14 AutoGRAPHMobile Info iPhone-Anton mobile 0000:MonoTouch 1 Firebase DeleteToken 24.12.2020 12:14 AutoGRAPHMobile Info iPhone-Anton mobile 0000:MonoTouch 1 Firebase GetToken : cfMTEI6Fh0sXm3vYlZxP4H:APA91bET-9rYal5ruPDZkQtBZAdsOBC5Y1fM3EvwuWTnPYZLn_YTFFDWdZX-QavFlgul42Yx9ECBHcLK73hfMrap0Ag88IbteLq-z_F3kxBLenXMUj_7SsxYiCRKyrtJx_4991rGsQcB

24.12.2020 12:15 AutoGRAPHMobile Info iPhone-Anton mobile 0000:MonoTouch 1 Firebase RegisteredForRemoteNotifications : {length = 32, bytes = 0xacdf3bcd dde7ae86 c724a43b 5e283fd8 ... 0764bcf9 16524718 } 24.12.2020 12:15 AutoGRAPHMobile Info iPhone-Anton mobile 0000:MonoTouch 1 Firebase DidReceiveRegistrationToken : cfMTEI6Fh0sXm3vYlZxP4H:APA91bET-9rYal5ruPDZkQtBZAdsOBC5Y1fM3EvwuWTnPYZLn_YTFFDWdZX-QavFlgul42Yx9ECBHcLK73hfMrap0Ag88IbteLq-z_F3kxBLenXMUj_7SsxYiCRKyrtJx_4991rGsQcB

24.12.2020 12:16 AutoGRAPHMobile Info iPhone-Anton mobile 0000:MonoTouch 1 Firebase RegisteredForRemoteNotifications : {length = 32, bytes = 0xacdf3bcd dde7ae86 c724a43b 5e283fd8 ... 0764bcf9 16524718 } 24.12.2020 12:16 AutoGRAPHMobile Info iPhone-Anton mobile 0000:MonoTouch 1 Firebase DidReceiveRegistrationToken : cfMTEI6Fh0sXm3vYlZxP4H:APA91bET-9rYal5ruPDZkQtBZAdsOBC5Y1fM3EvwuWTnPYZLn_YTFFDWdZX-QavFlgul42Yx9ECBHcLK73hfMrap0Ag88IbteLq-z_F3kxBLenXMUj_7SsxYiCRKyrtJx_4991rGsQcB

24.12.2020 12:16 AutoGRAPHMobile Info iPhone-Anton mobile 0000:MonoTouch 1 Firebase RegisteredForRemoteNotifications : {length = 32, bytes = 0xacdf3bcd dde7ae86 c724a43b 5e283fd8 ... 0764bcf9 16524718 } 24.12.2020 12:16 AutoGRAPHMobile Info iPhone-Anton mobile 0000:MonoTouch 1 Firebase DidReceiveRegistrationToken : cfMTEI6Fh0sXm3vYlZxP4H:APA91bET-9rYal5ruPDZkQtBZAdsOBC5Y1fM3EvwuWTnPYZLn_YTFFDWdZX-QavFlgul42Yx9ECBHcLK73hfMrap0Ag88IbteLq-z_F3kxBLenXMUj_7SsxYiCRKyrtJx_4991rGsQcB

24.12.2020 12:17 AutoGRAPHMobile Info iPhone-Anton mobile 0000:MonoTouch 1 Firebase RegisteredForRemoteNotifications : {length = 32, bytes = 0xacdf3bcd dde7ae86 c724a43b 5e283fd8 ... 0764bcf9 16524718 } 24.12.2020 12:17 AutoGRAPHMobile Info iPhone-Anton mobile 0000:MonoTouch 1 Firebase DidReceiveRegistrationToken : cfMTEI6Fh0sXm3vYlZxP4H:APA91bET-9rYal5ruPDZkQtBZAdsOBC5Y1fM3EvwuWTnPYZLn_YTFFDWdZX-QavFlgul42Yx9ECBHcLK73hfMrap0Ag88IbteLq-z_F3kxBLenXMUj_7SsxYiCRKyrtJx_4991rGsQcB 24.12.2020 12:17 AutoGRAPHMobile Info iPhone-Anton mobile 0000:MonoTouch 1 Firebase DeleteToken 24.12.2020 12:17 AutoGRAPHMobile Info iPhone-Anton mobile 0000:MonoTouch 1 Firebase GetToken : cfMTEI6Fh0sXm3vYlZxP4H:APA91bEIUJMfsYJZTkGpRYKNhdWjB7bZitacTmYg_qYH1Y8KptlEMiX7V6pE8KzPRalMxLtyTDxk4rcaHNSXxCRBs7nzB1BBOitLufHICsOa116HXMFu8meqJiBcRNucWXtbahua7aRf 24.12.2020 12:17 AutoGRAPHMobile Info iPhone-Anton mobile 0000:MonoTouch 1 Firebase DeleteToken 24.12.2020 12:17 AutoGRAPHMobile Info iPhone-Anton mobile 0000:MonoTouch 1 Firebase GetToken : cfMTEI6Fh0sXm3vYlZxP4H:APA91bHMRV-IE88vqdb0QXK6IvJBK7siCwyyWydNpK6ZJPXkOTumOgcG-PHERM-EEOLezkSmMVTznUaWvlGO8SMZHDHgDPoZAKqQL6xxR9D-GJzKzRycOHIF4XR2UgN_JhI51IV-lO9y

24.12.2020 12:17 AutoGRAPHMobile Info iPhone-Anton mobile 0000:MonoTouch 1 Firebase RegisteredForRemoteNotifications : {length = 32, bytes = 0xacdf3bcd dde7ae86 c724a43b 5e283fd8 ... 0764bcf9 16524718 } 24.12.2020 12:17 AutoGRAPHMobile Info iPhone-Anton mobile 0000:MonoTouch 1 Firebase DidReceiveRegistrationToken : cfMTEI6Fh0sXm3vYlZxP4H:APA91bET-9rYal5ruPDZkQtBZAdsOBC5Y1fM3EvwuWTnPYZLn_YTFFDWdZX-QavFlgul42Yx9ECBHcLK73hfMrap0Ag88IbteLq-z_F3kxBLenXMUj_7SsxYiCRKyrtJx_4991rGsQcB

24.12.2020 12:18 AutoGRAPHMobile Info iPhone-Anton mobile 0000:MonoTouch 1 Firebase RegisteredForRemoteNotifications : {length = 32, bytes = 0xacdf3bcd dde7ae86 c724a43b 5e283fd8 ... 0764bcf9 16524718 } 24.12.2020 12:18 AutoGRAPHMobile Info iPhone-Anton mobile 0000:MonoTouch 1 Firebase DidReceiveRegistrationToken : cfMTEI6Fh0sXm3vYlZxP4H:APA91bET-9rYal5ruPDZkQtBZAdsOBC5Y1fM3EvwuWTnPYZLn_YTFFDWdZX-QavFlgul42Yx9ECBHcLK73hfMrap0Ag88IbteLq-z_F3kxBLenXMUj_7SsxYiCRKyrtJx_4991rGsQcB

24.12.2020 12:18 AutoGRAPHMobile Info iPhone-Anton mobile 0000:MonoTouch 1 Firebase RegisteredForRemoteNotifications : {length = 32, bytes = 0xacdf3bcd dde7ae86 c724a43b 5e283fd8 ... 0764bcf9 16524718 } 24.12.2020 12:18 AutoGRAPHMobile Info iPhone-Anton mobile 0000:MonoTouch 1 Firebase DidReceiveRegistrationToken : cfMTEI6Fh0sXm3vYlZxP4H:APA91bET-9rYal5ruPDZkQtBZAdsOBC5Y1fM3EvwuWTnPYZLn_YTFFDWdZX-QavFlgul42Yx9ECBHcLK73hfMrap0Ag88IbteLq-z_F3kxBLenXMUj_7SsxYiCRKyrtJx_4991rGsQcB ` This is from the logs that my application writes.

monster74 commented 3 years ago

From the logs you can see that after the first update everything is fine. And after the second update, the old token began to return.

Other than that, couple of questions on the use case here, it seems like you delete a token every time in this function, are you calling this frequently once per app? Because it's not recommended to reset your token this frequently.

No. Often I don't update the token.

monster74 commented 3 years ago

Another thing is IID token API is deprecated, to better work with messaging:didReceiveRegistrationToken, please use the getToken and deleteToken call from Messaging because the API you are using has more parameters that isn't ideal for exposure and developers might enter unexpected input to cause issues.

I will try to do as you advise.

monster74 commented 3 years ago

You were right. I changed from "InstanceId" to "Messaging" and the problem disappeared. Here's the code: ` messaging.Delegate = null; messaging.DeleteFcmToken(_authorizedEntity, new MessagingDeleteFcmTokenCompletionHandler((errorDeleteToken) => {

                if (errorDeleteToken != null)
                {
                    messaging.Delegate = _messagingDelegate;
                    GlobalToast.Toast.ShowToast(errorDeleteToken.DebugDescription, "Error SharedInstance.DeleteToken");
                    AutoGRAPHMobile.App.LoggingService.Error("SharedInstance.DeleteToken: " + errorDeleteToken.DebugDescription);
                }
                else
                {
                    string logStr = $"Firebase DeleteFcmToken";
                    System.Diagnostics.Debug.WriteLine(logStr);
                    AutoGRAPHMobile.App.LoggingService.Info(logStr);
                    messaging.RetrieveFcmToken(_authorizedEntity, new MessagingFcmTokenFetchCompletionHandler((string newToken, NSError errorGetToken) =>
                    {
                        if (errorGetToken != null)
                        {
                            GlobalToast.Toast.ShowToast(errorGetToken.DebugDescription, "Error SharedInstance.GetToken");
                            AutoGRAPHMobile.App.LoggingService.Error("SharedInstance.GetToken: " + errorGetToken.DebugDescription);
                        }
                        else
                        {
                            logStr = $"Firebase RetrieveFcmToken : {newToken}";
                            System.Diagnostics.Debug.WriteLine(logStr);
                            AutoGRAPHMobile.App.LoggingService.Info(logStr);
                            SendRegistrationToServer("RefreshToken", newToken);
                        }
                        messaging.Delegate = _messagingDelegate;
                    }));
                }
            }));

` I reset messaging.Delegate so that the "messaging: didReceiveRegistrationToken:" event is not triggered during the update. It's not bad? I will now test everything well again and write to you.

monster74 commented 3 years ago

Thank you for your help. I checked everything. Switching to the "Messaging API" solved the problem. The old deleted token is no longer returned.