【问题标题】:Xamarin.iOS Firebase not receiving push notifications but receiving tokenXamarin.iOS Firebase 未收到推送通知但收到令牌
【发布时间】:2020-02-17 07:56:03
【问题描述】:

我正在 Xamarin.Forms 项目中设置推送通知。我已经为 Xamarin.Forms.Android 做了所有事情,它可以正常工作,但是我在 iOS 部分遇到了很多麻烦。这甚至不是我第一次这样做,但我仍然无法弄清楚发生了什么。

我做了什么:
1. 包含 Nuget Xamarin.Firebase.iOS.CloudMessaging v3.1.2(不是最新的,但由于库错误,甚至没有构建最新的)
2. 创建 Firebase 应用程序并按照常规设置,上传我的 .p12 用于推送通知并添加我的团队 ID。

3. 添加了我的 GoogleService-Info.plist 并将其 BuildAction 设置为“BundleResource”
4. 更新了我的 Info.plist

5. 确保我在 Apple Developer Program 中的 App ID 包含推送通知和正确的证书

6. 添加了与 UserNotifications.IUNUserNotificationCenterDelegate、IMessagingDelegate 接口相关的所有代码
7. 从

检索我的令牌
Firebase.InstanceID.InstanceId.Notifications.ObserveTokenRefresh((sender, e) =>
            {
                var token = Messaging.SharedInstance.FcmToken;

                if (!string.IsNullOrEmpty(token))
                {

                    //AppData.Instance.TokenMobile = newToken;
                }
            });
  1. 回到 Firebase 控制台,创建了一个测试推送并尝试通过“Invia messagio di prova”(“发送消息”)发送它,这让我指定了一个令牌和通过发布。

我的 AppDelegate.cs

    public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate, UserNotifications.IUNUserNotificationCenterDelegate, IMessagingDelegate
    {
        //
        // This method is invoked when the application has loaded and is ready to run. In this 
        // method you should instantiate the window, load the UI into it and then make the window
        // visible.
        //
        // You have 17 seconds to return from this method, or iOS will terminate your application.
        public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
            global::Xamarin.Forms.Forms.Init();
            global::Xamarin.Forms.FormsMaterial.Init();
            LoadApplication(new App());

            Firebase.Core.App.Configure();
            // For iOS 10 data message (sent via FCM)
            Messaging.SharedInstance.Delegate = this;

            if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
            {
                // For iOS 10 display notification (sent via APNS)
                UNUserNotificationCenter.Current.Delegate = this;

                // iOS 10 or later
                var authOptions = UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound;
                UNUserNotificationCenter.Current.RequestAuthorization(authOptions, (granted, error) =>
                {
                    if (granted)
                    {
                        InvokeOnMainThread(() =>
                        {
                            UIApplication.SharedApplication.RegisterForRemoteNotifications();
                        });
                    }
                });
            }
            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();

            // Handle token as you wish
            Firebase.InstanceID.InstanceId.Notifications.ObserveTokenRefresh((sender, e) =>
            {
                var token = Messaging.SharedInstance.FcmToken;

                if (!string.IsNullOrEmpty(token))
                {

                    //AppData.Instance.TokenMobile = newToken;
                }
            });


            UIApplication.SharedApplication.StatusBarHidden = true;
            UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;

            return base.FinishedLaunching(app, options);
        }

        public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)
        {
        }

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

        [Export("messaging:didRefreshRegistrationToken:")]
        public void DidReceiveRegistrationToken(Messaging messaging, string fcmToken)
        {
            Console.WriteLine($"Firebase registration token: {fcmToken}");

            // TODO: If necessary send token to application server.
            // Note: This callback is fired at each app startup and whenever a new token is generated.
        }

        public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)
        {
        }

        public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
        {
            //Tested
            //Messaging.SharedInstance.ApnsToken = deviceToken;
        }

        public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
        {
            var userInfo = notification.Request.Content.UserInfo;

            // With swizzling disabled you must let Messaging know about the message, for Analytics
            // Messaging.SharedInstance.AppDidReceiveMessage (userInfo);

            // Print full message.
            Console.WriteLine(userInfo);

            // Change this to your preferred presentation option
            completionHandler(UNNotificationPresentationOptions.None);
        }

        public void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action boh)
        {

        }

        [Export("messaging:didReceiveMessage:")]
        public void DidReceiveMessage(Messaging messaging, RemoteMessage message)
        {

        }
    }

我还在 Entitlment.plist 文件中添加了这个密钥:

<dict>
    <key>aps-environment</key>
    <string>development</string>
</dict>   

此时,我希望在我的应用中收到一些东西,但我收不到任何东西。
我在尝试实现的每个回调中都设置了一些断点,但没有一个被调用。
如果有帮助,甚至“DidReceiveRegistrationToken”方法都不会被调用。

【问题讨论】:

标签: c# ios firebase xamarin firebase-cloud-messaging


【解决方案1】:

解决了问题。
我不知道确切原因,但我在 GoogleService-Info.plist 中发现了一个与 Firebase 控制台中指定的不同的 API 密钥。我更新了密钥,现在我正在接收通知。

【讨论】:

  • 我建议您下载一个新的GoogleService-Info.plist,其中包含新的详细信息,并且不要自己手动更改它,因为 API-Key 不是它拥有的唯一信息
  • @FreakyAli 这正是我所做的。我至少实施了 4 次推送通知,但从来没有遇到过这个问题。甚至不知道为什么我的 API Key 发生了变化。
  • 您最近是否将新项目添加到您的 Firebase 项目中?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-23
相关资源
最近更新 更多