How to getdevice token ID from RegisteredForRemoteNotifications

I need to get the device token id when my IOS application start.
the bellow RegisteredForRemoteNotifications is running in the APPdelegate.cs and i am getting the NSData devicetoken. But how to extract the Device Token ID that i can use to push notification to that device. (like s.token in the second script)

public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
                    string str = deviceToken.Description;

        //trying here to extract the token id from devicetoken. I need to save it to my database with no result
        //var tokenStringBase64 = deviceToken.GetBase64EncodedString(NSDataBase64EncodingOptions.None);
                   // NSString nstoken = NSString.FromData(deviceToken, NSStringEncoding.UTF8);
                    //String token = nstoken.ToString();
                    //String aa = (String)deviceToken;
                    //var deviceTokenString = deviceToken.ToString().Replace("<", "").Replace(">", "").Replace(" ", "");

If i go to Firebase console and send notification to all IOS application i can found this token ID from **s.token **in the bellow script

    CrossFirebasePushNotification.Current.OnNotificationReceived += (s, p) =>

i spent 4 to 5 hours and i didnt arrive to get it..


  • LandLuLandLu Member, Xamarin Team Xamurai

    What you got in the OnNotificationReceived is FCM token, not iOS device's device token. Even though FCM's token is corresponding to the latter one, they are different.
    The original code offers the method about how to change the FCM token:

    - (void)application:(UIApplication *)application
        didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
        [FIRMessaging messaging].APNSToken = deviceToken;

    But I've no idea about how to change that using this plugin.
    Why do you want to know the original device token? If you do want to retrieve that in you pcl, you could try to store that in the RegisteredForRemoteNotifications event:

    public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
        var deviceTokenString = deviceToken.ToString().Replace("<", "").Replace(">", "").Replace(" ", "");
        Xamarin.Forms.Application.Current.Properties["Token"] = deviceTokenString;

    And retrieve code like:

    CrossFirebasePushNotification.Current.OnNotificationReceived += (s, p) =>
  • ivanxamarinivanxamarin Member ✭✭

    My code in AppDelegate.cs looks like this:

    public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken) { Firebase.CloudMessaging.Messaging.SharedInstance.SetApnsToken(deviceToken, Firebase.CloudMessaging.ApnsTokenType.Sandbox); // release should be: Firebase.CloudMessaging.ApnsTokenType.Production); }

    and this:

    [Export("messaging:didReceiveRegistrationToken:")] public async void DidReceiveRegistrationToken(Firebase.CloudMessaging.Messaging messaging, string fcmToken) { await RegisterNewToken(fcmToken); }
    receives the FCM token

    How do you format your code so well? I'm having a trouble with that.

Sign In or Register to comment.