Firebase Push Notification Token issue

Vuyiswa Maseko 351 Reputation points
2022-05-26T21:34:24.08+00:00

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

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,293 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Vuyiswa Maseko 351 Reputation points
    2022-05-28T20:10:42.62+00:00

    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

        }
    
    0 comments No comments