TobiasBuchholz / Plugin.Firebase

Wrapper around the native Android and iOS Firebase Xamarin SDKs
MIT License
211 stars 49 forks source link

Platform.Uno, .Net 7.0 and IOS - Token received but not push notifications #192

Closed MCAleckson closed 1 year ago

MCAleckson commented 1 year ago

My Platform.UNO Android app using this plugin for Firebase both receives a Firebase token and successfully receives push notifications.

However, in IOS, I only get the Firebase token as displayed during the firing of the TokenChanged event. The NotificationReceived event never fires when I send a notification through Firebase via Postman.

One thing's for sure - I'm clearly confused on registering the IOS/Platform.Uno app for notifications as the "UNUserNotificationCenter.Current.Delegate" shows in this IOS code to initialize Firebase and set the event handlers.

Thanks in advance for any help you can give.


   private void InitializeIOSFirebase()
   {
       try
       {
           if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))                   // For iOS 10 display notification (sent via APNS)
           {
              // UNUserNotificationCenter.Current.Delegate = this;     // THIS FAILS TO COMPILE

               var authOptions = UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound;
               UNUserNotificationCenter.Current.RequestAuthorization(authOptions, (granted, error) => {
                   Console.WriteLine(granted);
               });
           }
           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();

           CrossFirebase.Initialize();
           FirebaseCloudMessagingImplementation.Initialize();
           CrossFirebaseCloudMessaging.Current.CheckIfValidAsync();
           CrossFirebaseCloudMessaging.Current.SubscribeToTopicAsync("General");
           CrossFirebaseCloudMessaging.Current.SubscribeToTopicAsync("/topics/all");

           CrossFirebaseCloudMessaging.Current.TokenChanged += (s, p) =>
           {
               System.Diagnostics.Debug.WriteLine($"TOKEN RECIEVED EVENT: {p.Token}");
           };

           CrossFirebaseCloudMessaging.Current.Error += (s, p) =>
           {
               try
               {
                   System.Diagnostics.Debug.WriteLine($"PUSH NOTIFICATION ERROR EVENT: {p.Message}");

               }
               catch (Exception ex)
               {
                   Console.WriteLine(ex.Message);
               }
           };

           CrossFirebaseCloudMessaging.Current.NotificationReceived += (s, p) =>
           {
               try
               {
                   System.Diagnostics.Debug.WriteLine("PUSH NOTIFICATION RECEIVED EVENT: ");
               }
               catch (Exception ex)
               {
                   Console.WriteLine(ex.Message);
               }
           };
       }

       catch (Exception ex) 
       {
           Console.WriteLine(ex.Message);
       }

MCAleckson commented 1 year ago

I found my problem.

In his Legacy documentation (which applies to my case since though I'm using .Net7 I'm not using MAUI) Tobias says:

"Take a look at the documentation for the Xamarin.Firebase.iOS.CloudMessaging packages, because Plugin.Firebase's code is abstracted but still very similar."

Indeed.

I was not properly using Messaging.SharedInstance... as those documents depict.

For instance,

    public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)
    {
        // If you are receiving a notification message while your app is in the background,
        // this callback will not be fired till the user taps on the notification launching the application.
        // TODO: Handle data of notification

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

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

So I'll echo here - if you're trying to use Tobias' plugins with Xamarin or Platform.Uno or something other than Maui - pay close attention to the documentation in the Legacy branch and to the other documents linked there.

Thanks!