Nantourakis
Nantourakis

Reputation: 147

What is an alternative to the deprecated FirebaseInstanceIdService and OnTokenRefresh?

I noticed FirebaseInstanceIdService has been deprecated, can you please let me know if I'm setting up My FirebaseIID Service and My Firebase Messaging Service correctly? - Thanks for your time!

I'm now extending FireBaseMessagingService and calling the OnNewToken method instead, my code in my MyFirebaseIIDService now looks like this:

[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
class MyFirebaseIIDService : FirebaseMessagingService
{
const string TAG = "MyFirebaseIIDService";
NotificationHub hub;

public override void OnNewToken(string refreshedToken)
{
    base.OnNewToken(refreshedToken);
    Preferences.Set("notification_token", refreshedToken);
    Log.Debug(TAG, "FCM token: " + refreshedToken);
    SendRegistrationToServer(refreshedToken);
}

void SendRegistrationToServer(string token)
{
    // Register with Notification Hubs
    hub = new NotificationHub(Parameter.NotificationHubName,
                              Parameter.ListenConnectionString, this);

    var tags = GetEnabledCategories();
    var regID = hub.Register(token, tags.ToArray()).RegistrationId;
}

The Code in MyFirebaseMessagingService looks like this:

 [Service]
 [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
 class MyFirebaseMessagingService : FirebaseMessagingService
{
const string TAG = "MyFirebaseMsgService";
public const string NOTIFICATION_CHANNEL = "us.henrico.gov";
public override void OnMessageReceived(RemoteMessage message)
{
    if (message.GetNotification() != null)
    {
        // Notification payload, not in use, wouldn't trigger this function when in background
        SendNotification(message.GetNotification().Title, message.GetNotification().Body, message.Data["linktype"], message.Data["linkpage"]);
    }
    else
    {
        // Data payload, triggers this function in foreground or background
        SendNotification(message.Data["title"], message.Data["body"], message.Data["linktype"], message.Data["linkpage"]);
    }

}

Upvotes: 1

Views: 1927

Answers (1)

roshan posakya
roshan posakya

Reputation: 1030

as FirebaseInstanceId is depreciated you could use

  FirebaseMessaging.getInstance().getToken().addOnCompleteListener(task -> {
        System.out.println("Token : "+ task.getResult());
    });

Upvotes: 3

Related Questions