Xamarin Firebase iOS 11无生成FCM令牌

时间:2018-02-05 23:01:02

标签: ios firebase xamarin xamarin.ios

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

虽然代码已执行且未报告任何失败,但在iOS 11中令牌始终为空。

以下代码适用于远程模拟器上的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)));

    }

字符串标记和字符串fcm都包含iOS 9的值,但在iOS 11中都为空。

1 个答案:

答案 0 :(得分:1)

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

尝试使用rsattar提供的步骤。

  • FirebaseInstanceID降级为2.0.0

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

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