How to getdevice token ID from RegisteredForRemoteNotifications

Hey
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;
                    FirebasePushNotificationManager.DidRegisterRemoteNotifications(deviceToken);

        //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) =>
                        {System.Diagnostics.Debug.WriteLine(s.token);};

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

Answers

  • 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: https://firebase.google.com/docs/cloud-messaging/ios/client

    - (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)
    {
        FirebasePushNotificationManager.DidRegisterRemoteNotifications(deviceToken);
    
        var deviceTokenString = deviceToken.ToString().Replace("<", "").Replace(">", "").Replace(" ", "");
        Xamarin.Forms.Application.Current.Properties["Token"] = deviceTokenString;
        Xamarin.Forms.Application.Current.SavePropertiesAsync();
    }
    

    And retrieve code like:

    CrossFirebasePushNotification.Current.OnNotificationReceived += (s, p) =>
    {
        Console.WriteLine(Xamarin.Forms.Application.Current.Properties["Token"]);
    };
    
  • ivanxamarinivanxamarin Member ✭✭

    @XAMARINROCK
    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.