【问题标题】:Xamarin Firebase iOS 11 No FCM Token GeneratedXamarin Firebase iOS 11 未生成 FCM 令牌
【发布时间】:2018-02-05 23:01:02
【问题描述】:

我的应用向 firebase 注册推送通知,接收令牌,然后将该令牌作为 webview 加载的 url 的查询字符串参数提供。

虽然代码执行没有报错,但是在iOS 11中token始终为null。

以下代码适用于远程模拟器上的 iOS v9.x 或安装在设备上,但不适用于 iOS v11.x 时的任一环境。

有人经历过吗?有人有有用的建议吗?预先感谢您的帮助,我正在这里拔掉我剩下的头发。

我正在使用以下软件包/版本:

  • Xamarin.Firebase.iOS.CloudMessaging v2.0.4.1
  • Xamarin.Firebase.iOS.Core v4.0.13
  • Xamarin.Firebase.iOS.InstanceId v2.0.8

来自 AppDelegate.cs:

    public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
    {
        // Override point for customization after application launch.
        // If not required for your application you can safely delete this method

        // 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) =>
            {
                System.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();

        //configure firebase analytics.
        App.Configure();

        DoFCMConnect();
        return true;
    }

    private void DoFCMConnect()
    {
        Messaging.SharedInstance.ShouldEstablishDirectChannel = true;

        Console.WriteLine("Connected to FCM");

        //removed when updated to newest firebase version
        //Messaging.SharedInstance.Connect(error => {
        //    if (error != null)
        //    {
        //        System.Console.WriteLine(String.Format("Re-Connection to FCM Failed: {0}", error.ToString()));
        //    }
        //    else
        //    {
        //        System.Console.WriteLine("Re-Connected to FCM.");
        //    }
        //});
    }

来自 WebViewController.cs:

public override void ViewDidLoad()
    {
        string url = "https://www.mydomainname.com"; // NOTE: https secure request is required.

        string token = InstanceId.SharedInstance.Token;
        string fcm = Messaging.SharedInstance.FcmToken;

        if (token == null && fcm != null)
        {
            token = fcm;
        }

        if (String.IsNullOrEmpty(token))
        {
            url += "/login";
        }
        else
        {
            url += "/app-in/{0}"; //page accepts token and associates it to the user after login.
            url = String.Format(url, System.Web.HttpUtility.UrlEncode(token));
        }

        WebView.LoadRequest(new NSUrlRequest(new NSUrl(url)));

    }

字符串 token 和字符串 fcm 都包含 iOS 9 的值,但在 iOS 11 中两者都是 null。

【问题讨论】:

    标签: ios firebase xamarin xamarin.ios


    【解决方案1】:

    这是一个已知问题,请参阅here

    试试 rsattar 提供的步骤。

    • FirebaseInstanceID 降级到 2.0.0

    • UIApplication.shared.registerForRemoteNotifications()放在FinishedLaunching的第一行。

    • 卸载/重新安装您的应用程序。

    【讨论】:

    • 感谢您的回复 - 这确实有很大帮助。我之前曾尝试按照其他帖子的建议进行降级,但我没有做的是:通过 NuGet 删除所有 Firebase.iOS 包,然后重新安装具有最低依赖性的 .CloudMessaging 2.0。希望花絮对其他人有所帮助。再次感谢您的回复,科尔。
    猜你喜欢
    • 1970-01-01
    • 2021-08-22
    • 1970-01-01
    • 2016-09-24
    • 1970-01-01
    • 2020-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多