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.

  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭

    @ivanxamarin - I think the solution is to mark the AppDelegate class as an IMessagingDelegate. Once this is done, the DidReceiveRegistrationToken method is then called, instead of the RegisteredForRemoteNotifications method.

    This has the FCM token properly populated as the token argument.

  • ivanxamarinivanxamarin Member ✭✭

    @JamesLavery

    Yes, you have to implement IMessagingDelegate, but it isn't enough. The usual problem with this (and mine also was the first time I was using firebase for notifications on iOS) is that you get NSData token in RegisteredForRemoteNotifications, but it's not a firebase FCM token. You have to add this code to send the token to Firebase:

    Firebase.CloudMessaging.Messaging.SharedInstance.SetApnsToken(
    deviceToken, 
    Firebase.CloudMessaging.ApnsTokenType.Sandbox); 
    // release should be: Firebase.CloudMessaging.ApnsTokenType.Production);
    

    and the real trouble is that you can't receive the FCM token after that. When you look the swift and objectiveC code it looks pretty much straightforward, but in Xamarin after a lot of trouble I found the key part and it was this:

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

    and only like that I managed to receive the FCM token.

    I understood that was also the @XAMARINROCK 's problem here.

  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭

    That's interesting, I didn't need to do anything with sandbox and production. I think that this is because I'm using an APNs Authentication Key, not Certificates.

    All I needed to do was:

    // This callback is fired whenever a new token is generated - in order for it to be called, the AppDelegate must be an IMessagingDelegate
    [Export("messaging:didReceiveRegistrationToken:")]
    public void DidReceiveRegistrationToken(Messaging messaging, string token)
    {
        messaging.ApnsToken = token;
    }
    
  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭

    @ivanxamarin said:

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

    Hmm... my Notifications aren't working suddenly...

    Where is RegisterNewToken above defined? I may be missing something.

  • ivanxamarinivanxamarin Member ✭✭
    edited February 6
    > > @JamesLavery said:
    > > Hmm... my Notifications aren't working suddenly...
    > >
    > > Where is RegisterNewToken above defined? I may be missing something.
    >

    RegisterNewToken is just my method where I send fcm token to backend and it is saved in database with user. Later backend sends notification to users using fcm. Here, the problem was that the AppDelegate didn't receive the fcm token and I knew that I had the same problem first time which was resolved with Export attribute.
  • ivanxamarinivanxamarin Member ✭✭

    @JamesLavery said:
    That's interesting, I didn't need to do anything with sandbox and production. I think that this is because I'm using an APNs Authentication Key, not Certificates.

    All I needed to do was:

    // This callback is fired whenever a new token is generated - in order for it to be called, the AppDelegate must be an IMessagingDelegate
    [Export("messaging:didReceiveRegistrationToken:")]
    public void DidReceiveRegistrationToken(Messaging messaging, string token)
    {
        messaging.ApnsToken = token;
    }
    

    Yeah, ok, that might be true. I switched to Auth key later on, maybe I do have some code extra there.

  • ivanxamarinivanxamarin Member ✭✭
    edited February 6

    @JamesLavery

    Ok, I think I get it now. My code sets the ApnsToken because of this:

    Swizzling disabled: mapping your APNs token and registration token

    If you have disabled method swizzling, you'll need to explicitly map your APNs token to the FCM registration token. Override the methods didRegisterForRemoteNotificationsWithDeviceToken to retrieve the APNs token, and then set FIRMessaging's APNSToken property:

    source: https://firebase.google.com/docs/cloud-messaging/ios/client

    Don't ask why the swizzling is disabled. :)

  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭
    edited February 6
    Yes, worked it out just before leaving work this afternoon. (Been out night mountain biking - just back in).

    See my other thread
    https://forums.xamarin.com/discussion/149936/fcm-on-ios-messaging-sharedinstance-fcmtoken-empty-when-app-first-installs-runs

    For details of how I sorted it. It now works well!
Sign In or Register to comment.