Xamarin.iOS Firebase not receiving push notifications but receiving token

Viewed 2532

I'm setting up push notifications in a Xamarin.Forms project. I already did everything for Xamarin.Forms.Android and it works without any problem but I'm getting a lot of troubles with the iOS part. This isn't even the first time I do this but I still can't figure out what's going on.

What I did:
1. Included Nuget Xamarin.Firebase.iOS.CloudMessaging v3.1.2 (which is not the latest, but the latest is not even building because of library errors)
2. Create Firebase application and follow the usual setup, uploaded my .p12 for push notifications and added my Team ID.
p12
3. Added my GoogleService-Info.plist and set it's BuildAction to "BundleResource"
4. Updated my Info.plist
infoplist
5. Made sure that my App ID in Apple Developer Program included Push Notifications and the correct certificate
cert
6. Added every piece of code I could related to the UserNotifications.IUNUserNotificationCenterDelegate, IMessagingDelegate interfaces
7. Retrieved my token from

Firebase.InstanceID.InstanceId.Notifications.ObserveTokenRefresh((sender, e) =>
            {
                var token = Messaging.SharedInstance.FcmToken;

                if (!string.IsNullOrEmpty(token))
                {

                    //AppData.Instance.TokenMobile = newToken;
                }
            });
  1. Went back to Firebase console, created a test push and tried to send it both via "Invia messagio di prova" ("Send message") which made me specify a single token and via publishing.
    not1
    not2

My AppDelegate.cs

    public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate, UserNotifications.IUNUserNotificationCenterDelegate, IMessagingDelegate
    {
        //
        // This method is invoked when the application has loaded and is ready to run. In this 
        // method you should instantiate the window, load the UI into it and then make the window
        // visible.
        //
        // You have 17 seconds to return from this method, or iOS will terminate your application.
        public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
            global::Xamarin.Forms.Forms.Init();
            global::Xamarin.Forms.FormsMaterial.Init();
            LoadApplication(new App());

            Firebase.Core.App.Configure();
            // For iOS 10 data message (sent via FCM)
            Messaging.SharedInstance.Delegate = this;

            if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
            {
                // For iOS 10 display notification (sent via APNS)
                UNUserNotificationCenter.Current.Delegate = this;

                // iOS 10 or later
                var authOptions = UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound;
                UNUserNotificationCenter.Current.RequestAuthorization(authOptions, (granted, error) =>
                {
                    if (granted)
                    {
                        InvokeOnMainThread(() =>
                        {
                            UIApplication.SharedApplication.RegisterForRemoteNotifications();
                        });
                    }
                });
            }
            else
            {
                // iOS 9 or before
                var allNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound;
                var settings = UIUserNotificationSettings.GetSettingsForTypes(allNotificationTypes, null);
                UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
            }

            UIApplication.SharedApplication.RegisterForRemoteNotifications();

            // Handle token as you wish
            Firebase.InstanceID.InstanceId.Notifications.ObserveTokenRefresh((sender, e) =>
            {
                var token = Messaging.SharedInstance.FcmToken;

                if (!string.IsNullOrEmpty(token))
                {

                    //AppData.Instance.TokenMobile = newToken;
                }
            });


            UIApplication.SharedApplication.StatusBarHidden = true;
            UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;

            return base.FinishedLaunching(app, options);
        }

        public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)
        {
        }

        public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
        {
        }

        [Export("messaging:didRefreshRegistrationToken:")]
        public void DidReceiveRegistrationToken(Messaging messaging, string fcmToken)
        {
            Console.WriteLine($"Firebase registration token: {fcmToken}");

            // TODO: If necessary send token to application server.
            // Note: This callback is fired at each app startup and whenever a new token is generated.
        }

        public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)
        {
        }

        public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
        {
            //Tested
            //Messaging.SharedInstance.ApnsToken = deviceToken;
        }

        public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
        {
            var userInfo = notification.Request.Content.UserInfo;

            // With swizzling disabled you must let Messaging know about the message, for Analytics
            // Messaging.SharedInstance.AppDidReceiveMessage (userInfo);

            // Print full message.
            Console.WriteLine(userInfo);

            // Change this to your preferred presentation option
            completionHandler(UNNotificationPresentationOptions.None);
        }

        public void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action boh)
        {

        }

        [Export("messaging:didReceiveMessage:")]
        public void DidReceiveMessage(Messaging messaging, RemoteMessage message)
        {

        }
    }

I also added this key to the Entitlment.plist file:

<dict>
    <key>aps-environment</key>
    <string>development</string>
</dict>   

At this point I was expecting to receive something in my app, but I can't receive anything.
I put some breakpoints in each callback I tried to implement but none of them is getting called.
If it can be helpful, not even "DidReceiveRegistrationToken" method is getting called.

1 Answers

Solved the problem.
I don't know exactly why but I found in my GoogleService-Info.plist a different API Key from the one specified in the Firebase Console. I updated the key and I'm now receiving notifications.

Related