适用于iOS的Xamarin Firebase出现fcm 501错误 [英] Xamarin Firebase for iOS giving fcm 501 error

查看:61
本文介绍了适用于iOS的Xamarin Firebase出现fcm 501错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为此苦苦挣扎了几天,并且发现了很多关于该主题的文章,但是似乎没有任何东西可以解决我遇到的问题.我觉得这很简单,因为我尝试过几次缩减项目,并且总是遇到同样的失败.我可以在Android上很好地接收Firebase消息,但iOS始终无法击败我.

I have been struggling with this for several days and have found many other posts on the subject, but nothing seems to fix the problem I have. I have a feeling this might be simple as I have tried this several times with cut down projects and always get the same failure. I can receive Firebase messages fine in Android, but iOS consistently defeats me.

我正在使用带有iOS 10的手机作为测试.调试代码始终会返回令牌,但在连接时会显示错误消息操作无法完成. (com.google.fcm错误501.)"返回.我附上了一些示例代码-令人沮丧地陷入了困境

I am using a phone with iOS 10 as a test. The debug code always gets a token back, but on connecting, an error 'The operation couldn’t be completed. (com.google.fcm error 501.)' is returned. I attach some sample code - this has been bodged around in frustration

    public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate, 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 ();
        LoadApplication (new FirebaseTest.App (""));

        RegisterForNotificationFCM();
        return base.FinishedLaunching (app, options);
    }
    void RegisterForNotificationFCM()
    {
        // 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);
            });

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

        UIApplication.SharedApplication.RegisterForRemoteNotifications();

        Firebase.Analytics.Loader loader1 = new Firebase.Analytics.Loader();
        Firebase.InstanceID.Loader loader2 = new Firebase.InstanceID.Loader();

        //Firebase.Core.App.Configure();

        Firebase.InstanceID.InstanceId.Notifications.ObserveTokenRefresh((sender, e) =>
        {
            var newToken = Firebase.InstanceID.InstanceId.SharedInstance.Token;
            System.Diagnostics.Debug.WriteLine(newToken);

            connectFCM();
        });
    }
    public override void DidEnterBackground(UIApplication uiApplication)
    {
        Messaging.SharedInstance.ShouldEstablishDirectChannel = false;
    }

    public override void OnActivated(UIApplication uiApplication)
    {
        base.OnActivated(uiApplication);
    }

    public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
    {
        Messaging.SharedInstance.ApnsToken = deviceToken;
        connectFCM();
        //Firebase.InstanceID.InstanceId.SharedInstance.SetApnsToken(deviceToken, Firebase.InstanceID.ApnsTokenType.Prod);
    }

    public void DidRefreshRegistrationToken(Messaging msg, string str)
    {
    }

    //Fire when background received notification is clicked
    public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
    {
        //Messaging.SharedInstance.AppDidReceiveMessage(userInfo);
        System.Diagnostics.Debug.WriteLine(userInfo);

        // Generate custom event
        NSString[] keys = { new NSString("Event_type") };
        NSObject[] values = { new NSString("Recieve_Notification") };
        var parameters = NSDictionary<NSString, NSObject>.FromObjectsAndKeys(keys, values, keys.Length);

        // Send custom event
        Firebase.Analytics.Analytics.LogEvent("CustomEvent", parameters);

        if (application.ApplicationState == UIApplicationState.Active)
        {
            System.Diagnostics.Debug.WriteLine(userInfo);
            var aps_d = userInfo["aps"] as NSDictionary;
            var alert_d = aps_d["alert"] as NSDictionary;
            var body = alert_d["body"] as NSString;
            var title = alert_d["title"] as NSString;
            //debugAlert(title, body);
        }
    }

    [Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
    public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
    {
        // Do your magic to handle the notification data
        System.Console.WriteLine(notification.Request.Content.UserInfo);
    }

    // Receive data message on iOS 10 devices.
    public void ApplicationReceivedRemoteMessage(RemoteMessage remoteMessage)
    {
        Console.WriteLine(remoteMessage.AppData);
    }

    private void connectFCM()
    {
        //Messaging.SharedInstance.ShouldEstablishDirectChannel = true;
        //Messaging.SharedInstance.Subscribe("/topics/topic");
        Messaging.SharedInstance.Connect((error) =>
        {
            if (error == null)
            {
                Messaging.SharedInstance.Subscribe("/topics/topic");
            }
            System.Diagnostics.Debug.WriteLine(error != null ? "error occured" : "connect success");
        });
    }

随着您越来越绝望,这些功能已从同一主题的许多其他帖子中删除.我认为Firebase控制台已经设置好了.我已经将iOS作为可运行的Android应用程序添加为应用程序,并添加了在Mac上创建的身份验证密钥. (令人惊讶的是,即使没有将此iOS应用作为Firebase项目的一部分,即使没有此iOS应用,我仍然会获得令牌-并非我所期望的).已为此应用创建了证书,并且已重新创建了配置文件并删除了旧的配置文件.

These functions have been removed from many other posts on the same subject as happens when you get more and more desperate. I think the firebase console has been set up okay. I have added the iOS as an app along side the working Android one and have added an authentication key created on the Mac. (surprisingly I still get a token back even without this iOS app as part of the firebase project - not what I expected) A certificate has been created for this app and the provisioning profile has been recreated and old ones deleted.

我不确定还要添加什么-有人请让我摆脱困境...

I'm not sure what else to add - someone please put me out of my misery...

推荐答案

最后到那里.其他人需要检查的内容-将App.Configure()移至connectFCM

Got there in the end. Things for other people to check on - Moved App.Configure() into connectFCM

    private void connectFCM()
    {
        Firebase.Core.App.Configure();

        Messaging.SharedInstance.ShouldEstablishDirectChannel = true;
    }

并将以下内容添加到info.plist

and added the following to info.plist

<key>NSExceptionDomains</key>
<dict>
    <key>firebaseio.com</key>
    <dict>
        <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
        <true/>
        <key>NSIncludesSubdomains</key>
        <true/>
    </dict>
</dict>
<key>UIBackgroundModes</key>
<array>
    <string>remote-notification</string>
</array>

这篇关于适用于iOS的Xamarin Firebase出现fcm 501错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆