【问题标题】:Why isn't my Xamarin.iOS app receiving push notifications from Google Firebase?为什么我的 Xamarin.iOS 应用程序没有收到来自 Google Firebase 的推送通知?
【发布时间】:2023-04-07 18:49:01
【问题描述】:

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

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

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

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

  • 撤销我的苹果开发者证书和配置文件,然后制作新的。

  • 我创建了一个新的 Firebase 项目并包含了新的 GoogleService-Info.plist 文件(构建操作 -> BundleResource).. 我认为这不会做任何事情

  • 在我的 info.plist 的后台模式中启用远程通知

欢迎提出任何建议!

    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

【问题讨论】:

  • 我正在使用 Xamarin.Firebase.iOS.CloudMessaging v3.1.2 Xamarin.Firebase.iOS.Core v5.1.8 Xamarin.Firebase.iOS.InstanceID v2.0.0
  • 另外,Firebase 认为消息已经发送成功。
  • 您是否浏览过官方文档以确保您没有错过某些配置和/或设置? github.com/xamarin/GoogleApisForiOSComponents/blob/master/…
  • 我修复了它...基本上我卸载了 Xamarin.Firebase.iOS.Core v5.1.8 Xamarin.Firebase.iOS.InstanceID v2.0.0 nuget 包,一切正常!我也不需要 DidReceiveRemoteNotification 方法或 PresentNotification 方法。
  • 我有接缝问题,连接firebase并返回令牌,但是当我在firebase上测试时不显示通知,我删除包并再次安装问题仍然存在,任何人都可以帮忙我?

标签: firebase xamarin xamarin.forms xamarin.ios firebase-cloud-messaging


【解决方案1】:

问题在于 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-05
    • 2020-05-22
    • 2018-01-18
    • 1970-01-01
    • 2018-06-21
    • 2020-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多