Forum Xamarin Xamarin.iOS

GoogleApisForiOSComponents, CloudMessaging sample -- Messaging.SharedInstance always null

patcarutherspatcaruthers USMember ✭✭✭

I have implemented my apps AppDelegate.cs following the AppDelegate.cs in the sample, however when i run it Messaging.SharedInstance is null. Does anyone know where/how that instance is supposed to be created?

Answers

  • Pedro_PazPedro_Paz Member ✭✭

    Same problem here. Im using Xamarin.Firebase.IOS.CloudMessaging version 4.1.1.1

  • azamabdulrahimazamabdulrahim Member

    Call this first:
    Firebase.Core.App.Configure();

  • odinhausodinhaus Member ✭✭
    edited July 29

    I have the same issue, and I am calling App.Configure() first.

    I actually am using the AppDelegate class as presented in GoogleApisForiOSComponents CloudMessaging samples

    Both of these SharedInstance instances are null:

    Firebase.CloudMessaging.Messaging.SharedInstance.Delegate = this;
    InstanceId.SharedInstance.GetInstanceId(InstanceIdResultHandler);

    I'm pretty dead in the water if the component author's own sample code won't work.

    Im on latest VS 2019, Latest XCode on build system, and am deploying to an actual iOS iPhone (not a simulator).

    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
            {
                global::Xamarin.Forms.Forms.Init();
                NotificationSink = Xamarin.Forms.DependencyService.Get<Mobile.Services.INotificationSink>();
                LoadApplication(new App());
    
                Firebase.Core.App.Configure();
    
                // 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();
    
                Firebase.CloudMessaging.Messaging.SharedInstance.Delegate = this;
    
                InstanceId.SharedInstance.GetInstanceId(InstanceIdResultHandler);
    
                return true;
            }
    
  • xamarinzeroxamarinzero Member ✭✭

    Did anyone figure out why Messaging.SharedInstance always null?
    In my case, Messaging.SharedInstance is NOT null on simulator...FCM token gets created.
    On the device it is always null. So app crashes at Messaging.SharedInstance.Delegate = this;

    Please let me know if you found the solution. thanks.

  • xamarinzeroxamarinzero Member ✭✭

    I tried the solution by JoshuaTanton in the following link, but that didnt help me.

    https://forums.xamarin.com/discussion/181967/xamarin-firebase-ios-cloudmessaging-messaging-sharedinstance-is-null

  • xamarinzeroxamarinzero Member ✭✭

    When I run the same code against the simulator it runs ok. Messaging.SharedInstance is not null.
    I am not sure if that says something....

Sign In or Register to comment.