首次在我的Xamarin.iOS应用中注册时未设置FCM Messaging.SharedInstance.FcmToken

问题描述 投票:0回答:2

我们正在使用Xamarin.Firebase.iOS.CloudMessaging来获取推送通知。

除了一个怪癖,它工作正常。

首次安装该应用程序时,会调用RegisteredForRemoteNotifications,但未设置Messaging.SharedInstance.Fcmtoken

[随后运行该应用程序时,它已设置。

我们需要FCM令牌,以便可以将其发送到我们的后端服务器。

如何在首次安装时获得FCM令牌?

我的代码是:

    public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
    {
        // NOTE that the token passed in here is the Apple APNS token.
        // We need the Firebase FCM Token -  this is what is used to manage token sending in Firebase.

        Messaging.SharedInstance.ApnsToken = deviceToken;

        // Why is this empty the first time the app runs?
        var fcmToken = Messaging.SharedInstance.FcmToken;

        Debug.WriteLine($"RegisteredForRemoteNotifications token:'{deviceToken}'");

        // This method can be called with no FCM Token set - so only do something when we've got the token
        if (!string.IsNullOrEmpty(fcmToken))
        {
            Debug.WriteLine($"RegisteredForRemoteNotifications fcmtoken:'{Messaging.SharedInstance.FcmToken}'");

            // Subscribe to a 'news' topic so we can send to just those subscribed to this topic
            Messaging.SharedInstance.Subscribe("news");

            // TODO - we will need to send the fcmToken to the back-end server, with the user details, so that the back-end server
            // can then manage notifications and send targetted Notifications to the specific user (e.g. a warning that their policy is expiring soon).
            //SendPushNotificationTokenToTempcoverServer(fcmToken);
        }

    }
c# firebase xamarin.ios firebase-cloud-messaging
2个回答
0
投票

解决方案是将AppDelegate类标记为IMessagingDelegate。完成此操作后,将调用DidReceiveRegistrationToken方法,而不是RegisteredForRemoteNotifications方法。

具有正确填充为token参数的FCM令牌。


0
投票

对我来说解决方案是Messaging.SharedInstance.AutoInitEnabled = true;

© www.soinside.com 2019 - 2024. All rights reserved.