首页 > 解决方案 > 为什么我的 Xamarin.iOS 应用程序没有收到来自 Google Firebase 的推送通知?

问题描述

我正在尝试使用 Google Firebase 向我的 Xamarin Forms App for iOS 发送推送通知...我没有收到通知

要发送通知,我只需将令牌从输出窗口中复制出来,将我的应用程序放在后台,然后将令牌粘贴到 Firebase 通知构建器中。

我收到了 FCM 令牌,所以我的应用肯定在与 Firebase 对话。

到目前为止,我已经尝试过:

任何建议表示赞赏!

    public override bool FinishedLaunching(UIApplication app, NSDictionary 
    options)
    {   

        global::Xamarin.Forms.Forms.Init();

        UIApplication.SharedApplication.RegisterForRemoteNotifications();

        Firebase.Core.App.Configure();

        _connection = DependencyService.Get<ISQLiteDb>().GetConnection();

        LoadApplication(application: new V5KitchenApp.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);
                var token = Messaging.SharedInstance.FcmToken ?? "";
                Console.WriteLine($"FCM token: {token}");
                SendTokenToServer(token);
            });

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

        return base.FinishedLaunching(app, options);
    }

这是我覆盖 DidReceiveRemoteNotification 的方法

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

这就是我呈现通知的方式

    void PresentNotification(NSDictionary dict)
    {
        NSDictionary aps = dict.ObjectForKey(new NSString("aps")) as NSDictionary;
        var msg = string.Empty;
        if (aps.ContainsKey(new NSString("alert")))
        {
            msg = (aps[new NSString("alert")] as NSString).ToString();
        }

        if (string.IsNullOrEmpty(msg))
        {
            msg = "(unable to parse)";
        }

        MessagingCenter.Send<object, string>(this, App.NotificationReceivedKey, msg);
    }

我正在使用 Xamarin.Firebase.iOS.CloudMessaging v3.1.2 Xamarin.Firebase.iOS.Core v5.1.8 Xamarin.Firebase.iOS.InstanceID v2.0.0

标签: firebasexamarinxamarin.formsxamarin.iosfirebase-cloud-messaging

解决方案


问题是我使用了下一个版本的 Firebase 版本:

Xamarin.Firebase.iOS.InstanceID 2.0
Xamarin.Firebase.iOS.CloudMessaging 2.0
Xamarin.Firebase.iOS.Core 4.0.3
Xamarin.Firebase.iOS.Analtics 4.0.2

推荐阅读