question

VuyiswaMaseko-8882 avatar image
0 Votes"
VuyiswaMaseko-8882 asked LeonLu-MSFT edited

Firebase Push Notification Token issue

Good Day All

I am using Firebase in Xamarin , i have the following situation ,

i have a Token that is receiving an error "UNREGISTERED" as i trapped it via exception handling as depicted below

 catch (Exception ex)
             {
                var error  =  (FirebaseMessagingException)ex;
    
                 if (error.MessagingErrorCode.ToString().ToUpper() == "UNREGISTERED")
                 {
                     UnRegisterToken(model.SENDER_USER_ID);
                 }
                 else if (error.MessagingErrorCode.ToString().ToUpper() == "INVALID_ARGUMENT")
                 {
                     UnRegisterToken(model.SENDER_USER_ID); 
                 }
                 else if(error.MessagingErrorCode.ToString().ToUpper() == "QUOTA_EXCEEDED")
                 {
                     Crashes.TrackError(ex);
                 }
                 else if (error.MessagingErrorCode.ToString().ToUpper() == "THIRD_PARTY_AUTH_ERROR")
                 {
                     Crashes.TrackError(ex);
                 }
                 else   
                 {
                     Crashes.TrackError(ex);
                 }
             } 

if its unregistered , i clear the database token for that user . my challenge is the following

1) When will the next token be generated without the app data being cleared

and obviously on the Android side , i have the part that normally get a new token for the device when its first used like this



 [Service(DirectBootAware = true, Exported = false, Enabled = true)]
 [IntentFilter(new[] { "com.google.firebase.INSTANCE_ID_EVENT" })]
 [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
 public class CustomFirebaseMessagingIIService : FirebaseMessagingService 
 {
    
     public override void OnNewToken(string token)
     {
         try
         {
             base.OnNewToken(token);
             GenericMethods.PUSH_TOKEN = token;
             Task.Run(() => GenericMethods.StorePushToken(token));
         }
         catch (Exception ex)
         {
             Crashes.TrackError(ex);
         }
     }

and the on Xamarin form App.cs i alsos subscribe to it like this


         CrossFirebasePushNotification.Current.Subscribe("Push");
         CrossFirebasePushNotification.Current.OnTokenRefresh += Current_OnTokenRefresh;

and store it on the server like this

    private void Current_OnTokenRefresh(object source, FirebasePushNotificationTokenEventArgs e)
         {  
                    
             ChangeUserOneSignalID(e.Token); 
         }

i read the Firebase Documentation that the Token can take 2 months to expire , but this one is fairly new i created it two hours ago . but it seems like it gives an error like that when i try to send a push notification. is it a good idea to clear or and when will it be generated again if its no longer active for the device.

Thanks




dotnet-xamarin
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The registration token may change when: 1. The app deletes Instance ID; 2. The app is restored on a new device; 3. The user uninstalls/reinstall the app; 4. The user clears app data. If this issue happend in debug mode. Everything would be fine in release mode.
You can search Unregistered Registration Token in Firebase with your favorite browser, you will find an answer about fixing this issue.

0 Votes 0 ·

1 Answer

VuyiswaMaseko-8882 avatar image
0 Votes"
VuyiswaMaseko-8882 answered

Thank you for your reply , i followed the comments there and i was able to come up with this

         private void ConfigureFireBase()
         {
 #if DEBUG
             Task.Run(async () =>
             { 
                 var token = FirebaseInstallations.Instance.GetId();
                
                 await GenericMethods.StorePushToken(token.Result.ToString()); 
             });
    
 #endif

This solved my problem and Push notifications works


     }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.