Forum Xamarin Xamarin.iOS

Firebase notifications: messaging:didReceiveRegistrationToken is not being called.

I have an Xamarin.iOS app and I've recently updated my Xamarin.Firebase.iOS.CloudMessaging package from 1.2.2 to 4.1.1.1. Once I did this update and added the relevant code changes, I've stopped receiving notifications in my app. When I started debugging it I've realized DidReceiveRegistrationToken is never getting called.

Here's my code:

        public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
            Firebase.Core.App.Configure();

            if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))       // I don't care about iOS < 10
            {
                UNUserNotificationCenter.Current.Delegate = this;
                Messaging.SharedInstance.Delegate = this;

                var authOptions = UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound;
                UNUserNotificationCenter.Current.RequestAuthorization(authOptions, (granted, error) =>
                {
                    Console.WriteLine("Notification authorization granted: " + granted);
                });
            }

            UIApplication.SharedApplication.RegisterForRemoteNotifications();
    }

        [Export("messaging:didReceiveRegistrationToken")]
        public void DidReceiveRegistrationToken(string token)
        {
            Console.WriteLine("Token!! " + token);
            //do something with token,
        }

Any ideas what am I doing wrong?
I'm using:
Xamarin.Firebase.iOS.CloudMessaging 4.1.1.1
Xamarin.Firebase.iOS.InstanceID 4.2.1
Xamarin.Firebase.iOS.Core 6.1.0
Which are all currently up to date.

Thanks!

Best Answer

  • TakoSheetTakoSheet Member ✭✭
    Accepted Answer

    Ok, I've figured it out. I'll post my solution here in case anyone else is experiencing the same thing:

    Seems like we need to register for ObserveTokenRefresh instead of waiting for DidReceiveRegistrationToken to be called.

                Firebase.InstanceID.InstanceId.Notifications.ObserveTokenRefresh((sender, e) =>
                {
                    Firebase.InstanceID.InstanceId.SharedInstance.GetInstanceId((result, error) =>
                    {
                        if (error == null)
                        {
                            string token = result.Token;
                            Console.WriteLine("Got a notification token: " + token);
                            //do something with token
                        }
                        else
                        {
                            Console.WriteLine("couldn't get Firebase Token: " + error);
                        }
                    });
                }
    

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    Once I did this update and added the relevant code changes, I've stopped receiving notifications in my app

    Does the notification not work in background only ? see the similar issue : https://github.com/xamarin/GoogleApisForiOSComponents/issues/335

    Try this possible workaround: https://github.com/xamarin/GoogleApisForiOSComponents/issues/335#issuecomment-558937366.

  • TakoSheetTakoSheet Member ✭✭

    Thanks @ColeX, but I'm not getting notifications whether the app is in the background of foreground, so I don't think it's the same issue.

  • TakoSheetTakoSheet Member ✭✭
    Accepted Answer

    Ok, I've figured it out. I'll post my solution here in case anyone else is experiencing the same thing:

    Seems like we need to register for ObserveTokenRefresh instead of waiting for DidReceiveRegistrationToken to be called.

                Firebase.InstanceID.InstanceId.Notifications.ObserveTokenRefresh((sender, e) =>
                {
                    Firebase.InstanceID.InstanceId.SharedInstance.GetInstanceId((result, error) =>
                    {
                        if (error == null)
                        {
                            string token = result.Token;
                            Console.WriteLine("Got a notification token: " + token);
                            //do something with token
                        }
                        else
                        {
                            Console.WriteLine("couldn't get Firebase Token: " + error);
                        }
                    });
                }
    
  • ColeXColeX Member, Xamarin Team Xamurai

    @TakoSheet said:
    Ok, I've figured it out. I'll post my solution here in case anyone else is experiencing the same thing:

    Seems like we need to register for ObserveTokenRefresh instead of waiting for DidReceiveRegistrationToken to be called.

                Firebase.InstanceID.InstanceId.Notifications.ObserveTokenRefresh((sender, e) =>
                {
                  Firebase.InstanceID.InstanceId.SharedInstance.GetInstanceId((result, error) =>
                  {
                      if (error == null)
                      {
                          string token = result.Token;
                          Console.WriteLine("Got a notification token: " + token);
                          //do something with token
                      }
                      else
                      {
                          Console.WriteLine("couldn't get Firebase Token: " + error);
                      }
                  });
              }
    

    Please mark you solution as answer .

Sign In or Register to comment.