Xamarin.iOS Firebase不接收推送通知,但接收令牌

时间:2019-10-21 08:43:26

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

我正在Xamarin.Forms项目中设置推送通知。我已经为Xamarin.Forms.Android完成了所有工作,并且可以正常工作,但iOS部分出现了很多麻烦。这甚至不是我第一次这样做,但是我仍然不知道发生了什么。

我做了什么:
1.随附了Nuget Xamarin.Firebase.iOS.CloudMessaging v3.1.2(这不是最新版本,但由于库错误,最新版本甚至无法构建)
2.创建Firebase应用程序并按照常规设置,上传我的.p12以获得推送通知并添加我的团队ID。
p12
3.添加了我的GoogleService-Info.plist并将其BuildAction设置为“ BundleResource”
4.更新了我的Info.plist
infoplist
5.确保我在Apple Developer Program中的App ID包括推送通知和正确的证书
cert
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”(“发送消息”)发送,这使我指定了单个令牌并通过发布。
    not1
    not2

我的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”方法。

1 个答案:

答案 0 :(得分:0)

解决了问题。
我不知道为什么,但是我在GoogleService-Info.plist中找到了与Firebase控制台中指定的API密钥不同的API密钥。我更新了密钥,现在正在接收通知。