GoogleApisForiOSComponents icon indicating copy to clipboard operation
GoogleApisForiOSComponents copied to clipboard

Xamarin.Firebase.iOS.CloudMessaging fails on Messaging.SharedInstance.Delegate

Open gordonsaxby opened this issue 3 years ago • 19 comments

I have tried many different versions of sample / example code, but I cannot get Cloud Messaging to work on iOS (14.5).

The line

Messaging.SharedInstance.Delegate = this

Always fails with SharedInstance being null.

To test, I created a new Xamarin Forms project (gives a basic app with a sample list page), I added the Nuget package Xamarin.Firebase.iOS.CloudMessaging version 4.7.1.

I added the following code to AppDelegate: ` public override bool FinishedLaunching(UIApplication app, NSDictionary options) { try { global::Xamarin.Forms.Forms.Init();

            Firebase.Core.App.Configure();

            LoadApplication(new App());

            // Register your app for remote notifications.
            if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
            {
                // iOS 10 or later
                var authOptions = UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound;
                UNUserNotificationCenter.Current.RequestAuthorization(authOptions, (granted, error) => {
                    Console.WriteLine(granted);
                });

                // For iOS 10 display notification (sent via APNS)
                UNUserNotificationCenter.Current.Delegate = this;

                // For iOS 10 data message (sent via FCM)
                Messaging.SharedInstance.Delegate = this;
            }
            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();

            return true;
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
    }

` The above code fails on the line "Messaging.SharedInstance.Delegate = this;". If I run it on a simulator on a Mac then the code works OK (although notifications don't actually work on the simulator)

Visual Studio 2019 v16.10.0 on Windows Xamarin Forms v5.0.0.2012 Testing on iPhone 12 Mini connected via Hot Reload / iTunes.

gordonsaxby avatar Jun 01 '21 10:06 gordonsaxby

I am trying to get notifications working for iOS and I am having this same issue. Please let me know if you were able to find a solution. I created a stack overflow question for this issue: https://stackoverflow.com/questions/67928375/xamarin-firebase-ios-cloudmessaging-fails-on-messaging-sharedinstance-delegate

BedirAMS avatar Jun 10 '21 20:06 BedirAMS

This is driving me nuts. VS 2019 v16.10.2 on Windows Xamarin Forms v5.0.0.2012

If I do it on the Simulator the SharedInstance is a FIRMessaging. But testing against a physical iPhone 7 and it's null. WTF?

catapedamania avatar Jun 21 '21 12:06 catapedamania

p.s. and I checked on the Simulator. It Messaging.SharedInstance gets instantiated by the Firebase.Core.App.Configure() call.

catapedamania avatar Jun 21 '21 14:06 catapedamania

This is driving me nuts. VS 2019 v16.10.2 on Windows Xamarin Forms v5.0.0.2012

If I do it on the Simulator the SharedInstance is a FIRMessaging. But testing against a physical iPhone 7 and it's null. WTF?

Are you developing on Windows? I found that it just wouldn't work with my iPhone plugged into my Windows laptop. I now have a MacBook and it works fine (same code) when compiled / deployed / debugged from the MacBook!?

I am actually now using the CrossGeeks solution rather than this and the Android equivalent, but I think the issue is the same.

gordonsaxby avatar Jun 21 '21 15:06 gordonsaxby

Yes, Windows. I've already had to buy an iPhone because notifications aren't supported on the Simulator. I really don't want to have to buy a MacBook as well!

catapedamania avatar Jun 21 '21 16:06 catapedamania

Will it be ever fixed?

Bykiev avatar Aug 02 '21 12:08 Bykiev

Can you try this code snippet, basically refer these two interfaces IUNUserNotificationCenterDelegate, IMessagingDelegate

`public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate, IUNUserNotificationCenterDelegate, IMessagingDelegate { public override bool FinishedLaunching(UIApplication app, NSDictionary options) { global::Xamarin.Forms.Forms.Init(); ConstantHelper.Notification.DeviceId = GetDeviceId();

        LoadApplication(new App());

        Firebase.Core.App.Configure();
        RegisterForRemoteNotifications();
        Messaging.SharedInstance.Delegate = this;
        ConstantHelper.PersistedProperties.NotificationToken = Messaging.SharedInstance.FcmToken ?? "";
        return base.FinishedLaunching(app, options);

    }

}

void RegisterForRemoteNotifications() { // Register your app for remote notifications. if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0)) {

            // For iOS 10 display notification (sent via APNS)
            UNUserNotificationCenter.Current.Delegate = this;
            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();
    }

`

ypcaptain96 avatar Oct 25 '21 04:10 ypcaptain96

The same issue, any solution?

angelru avatar Nov 09 '21 00:11 angelru

Same issue Any updates or workarounds discovered by anyone?

PKalok avatar Jan 21 '22 22:01 PKalok

Has there been any update on this bug? It's still here in 8.10.0.1 when you try and build and run from Windows

nodoid avatar Sep 15 '22 11:09 nodoid

Hi @Redth , Is somebody maintainer for this repository? And could give a statement. Is this a Hot Restart bug or a another bug?

AlleSchonWeg avatar Sep 21 '22 12:09 AlleSchonWeg

After a year, still, no one has ever workaround or fixed this issue. I have the same issue during building on Mac and a real iPhone...

Visual Studio 8.10.16 Xamarin.Firebase.iOS.CloudMessaging 8.10.0.2

ricomcch avatar Oct 27 '22 16:10 ricomcch

  1. You don't actually need to set the delegate anymore. You can see how I get the push tokens here:

https://github.com/shinyorg/shiny/blob/master/src/Shiny.Push.FirebaseMessaging/Platforms/iOS/PushManager.cs

  1. If you still want the delegate and SharedInstance is null, it means you haven't configured the firebase app instance properly. Also shown in the code I posted.

  2. Lastly, if you're "still" waiting for fixes - this is open source, you can always fork and fix the bindings for yourself. It isn't a huge amount of code.

aritchie avatar Oct 27 '22 17:10 aritchie

Really, no updates? @aritchie can you please update the link? It doesn't work anymore

justcivah avatar Apr 10 '23 19:04 justcivah

I think this is the PushManager @aritchie was trying to link to: https://github.com/shinyorg/shiny/blob/master/src/Shiny.Push/Platforms/Apple/PushManager.cs

robertev-alchemy avatar Apr 13 '23 19:04 robertev-alchemy

This is the link: https://github.com/shinyorg/firebase/blob/main/src/Shiny.Push.FirebaseMessaging/Platforms/iOS/FirebasePushProvider.cs

aritchie avatar Apr 13 '23 19:04 aritchie

Thats not the issue, the issue is that the shared instance is null when compiling through windows, on remote mac it works fine. the google service plist file seems to only be detected in remote mac

cwilde123 avatar Apr 19 '23 07:04 cwilde123

Make sure the link attribute is included when adding your google service plist file for MAUI at least. If not it wont find the google service plist file even when compiling through remote mac. It still doesnt compile on windows but this makes it work on remote mac on physical iphone device. Ignore VS saying build action is none when looking at UI, it's still a bundled resource.

<ItemGroup>
	<BundleResource Include="Platforms\iOS\GoogleService-Info.plist" Link="GoogleService-Info.plist" />
</ItemGroup>

cwilde123 avatar Apr 19 '23 07:04 cwilde123

Question: Right now, using the latest tools and MAC for compilation and VS 2022 and physical iPhone is working to be able to deploy from Windows to iPhone (building on MAC) and use Firebase plugin to initialize, get deviceid, and send notifications to the device? Or must the deploy be from MAC?

I already have that bundleresource in my csproj and it crashes in initialization at runtime on physical iPhone device both deployment methods.

After battling this problem with no luck for 1 week - I threw in the towel, hired a SWIFT developer that was able to write the whole app, including notifications, in just 3 days. I would have preferred to use MAUI.

From: Christian Wilde @.> Sent: Wednesday, April 19, 2023 3:47 AM To: xamarin/GoogleApisForiOSComponents @.> Cc: Robert Evans @.>; Comment @.> Subject: Re: [xamarin/GoogleApisForiOSComponents] Xamarin.Firebase.iOS.CloudMessaging fails on Messaging.SharedInstance.Delegate (#486)

Make sure the link attribute is included when adding your google service plist file for MAUI at least. If not it wont find the google service plist file even when compiling through remote mac. It still doesnt compile on windows but this makes it work on remote mac on physical iphone device. Ignore VS saying build action is none when looking at UI, it's still a bundled resource.

    <BundleResource Include="Platforms\iOS\GoogleService-Info.plist" Link="GoogleService-Info.plist" />

— Reply to this email directly, view it on GitHubhttps://github.com/xamarin/GoogleApisForiOSComponents/issues/486#issuecomment-1514287343, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AZLHFDYAO72ZBVSCRX22TPDXB6J7VANCNFSM454LSTTA. You are receiving this because you commented.Message ID: @.@.>>

robertev-alchemy avatar Apr 21 '23 12:04 robertev-alchemy