xamarin / GoogleApisForiOSComponents

MIT License
225 stars 161 forks source link

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

Open gordonsaxby opened 3 years ago

gordonsaxby commented 3 years ago

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.

BedirAMS commented 3 years ago

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

catapedamania commented 3 years ago

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 commented 3 years ago

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

gordonsaxby commented 3 years ago

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.

catapedamania commented 3 years ago

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!

Bykiev commented 3 years ago

Will it be ever fixed?

ypcaptain96 commented 2 years ago

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();
    }

`

angelru commented 2 years ago

The same issue, any solution?

PKalok commented 2 years ago

Same issue Any updates or workarounds discovered by anyone?

nodoid commented 2 years ago

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

AlleSchonWeg commented 2 years ago

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

ricomcch commented 1 year ago

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

aritchie commented 1 year ago
  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.

justcivah commented 1 year ago

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

robertev-alchemy commented 1 year ago

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

aritchie commented 1 year ago

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

cwilde123 commented 1 year ago

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 commented 1 year ago

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>
robertev-alchemy commented 1 year ago

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.

— 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: @.**@.>>