0

So, "FirebaseInstanceIdService" gives a warning it is depreciated and obsolete.

"OnTokenRefresh()" doesn't work anymore, and we should instead use:

   public void onNewToken(String s) 
   {
        Log.d("FCM_TOKEN", s);
        // save in SharedPreference for future use
   }

But - in order to get the token in MainActivity, I've previously used:

FirebaseInstanceId.Instance.Token

This is also deprecated, and I could only find Java solution for this:

FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener( MyActivity.this,  new OnSuccessListener<InstanceIdResult>() {
 @Override
 public void onSuccess(InstanceIdResult instanceIdResult) {
       String token = instanceIdResult.getToken();
       Log.d("FCM_TOKEN",token);
 }
});

Could you please let me know how can I get the current token in C# (Xamarin)?


Question #2: is this OK implementation for OnNewToken() in C#:

public class MessagingService : FirebaseMessagingService
{
    const string TAG = "MyFirebaseMsgService";
    public override void OnNewToken(string token)
    {
        Log.Debug(TAG, "Refreshed token: " + token);
        SendToServer(token);
    }

}

Thanks a bunch!

Cleptus
  • 3,446
  • 4
  • 28
  • 34

1 Answers1

-2

1) I'm also using Firebase Cloud Messaging for my project. I don't think FirebaseInstanceId.Instance.Token is deprecated for two reasons. First, I don't have any warnings as you said. Second, FCM Notifications Walkthrough from Microsoft is updated recently, and it does not mention that property is deprecated.

2) FirebaseMessagingService is to handle notifications( or messages) received, not to handle tokens. For example, if your app server is sending a message (how to send a message ) to a client (a device), FirebaseMessagingService is where you receive and handle the message at client. Also, if you right click on the class FirebaseMessagingService -> "Go To Definition", you would see that there isn't a OnNewToken() to override.

Q1 and Q2 come together, I'm guessing you want something like below to retrieve a registration token and send it to your app server.

[Service]
[IntentFilter(new[] { "com.google.firebase.INSTANCE_ID_EVENT" })]
public class MyFirebaseIIDService : FirebaseInstanceIdService
{
    const string TAG = "MyFirebaseIIDService";
    public override void OnTokenRefresh()
    {
        var refreshedToken = FirebaseInstanceId.Instance.Token;
        Log.Debug(TAG, "Refreshed token: " + refreshedToken);
        SendRegistrationToServer(refreshedToken);
    }
    void SendRegistrationToServer(string token)
    {
       // send token to app server  
    }
}    

The code above extracted from the walkthrough. My explanations are brief and incomplete. You should read the walkthrough to have a better understanding.

Fuong
  • 320
  • 3
  • 12
  • 1
    FirebaseInstanceIdService is obsolete/deprecated. That's the warning I'm getting in Visual Studio, after updating all the packages (especially the ones related to Firebase). Please see this link ---> https://forums.xamarin.com/discussion/136542/fcm-token-for-push-notifications-firebaseinstanceid-intance-token-obsolete (Xamarin Forums) ; and this one ---> https://stackoverflow.com/questions/51834864/how-to-save-a-fcm-token-in-android (stackoverflow) – Ucitelj Splinter Sep 16 '18 at 15:27
  • 1
    Firebase is not a Microsoft product, don't trust their documentation for Google's deprecations. – Mark Meeus Apr 09 '19 at 07:43