簡體   English   中英

適用於iOS的Xamarin Firebase出現fcm 501錯誤

[英]Xamarin Firebase for iOS giving fcm 501 error

我已經為此苦苦掙扎了幾天,並且發現了許多關於該主題的文章,但是似乎沒有什么可以解決我遇到的問題。 我覺得這很簡單,因為我嘗試過幾次縮減項目,並且總是遇到同樣的失敗。 我可以在Android上正常接收Firebase消息,但iOS始終使我失望。

我正在使用帶有iOS 10的手機進行測試。 調試代碼始終會返回令牌,但在連接時會顯示錯誤消息“操作無法完成。 (com.google.fcm錯誤501。)”返回。 我附上了一些示例代碼-這讓我們很沮喪

    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應用,我仍然會獲得令牌-並非我所期望的)。已為此應用創建了證書,並且已重新創建了配置文件並刪除了舊的配置文件。

我不確定還有什么要補充的-有人請讓我擺脫痛苦...

最后到達那里。 其他人需要檢查的內容-將App.Configure()移至connectFCM

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

        Messaging.SharedInstance.ShouldEstablishDirectChannel = true;
    }

並將以下內容添加到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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM