Prabesh
Prabesh

Reputation: 373

Xamarin.Android - FirebaseInstanceIdService is deprecated and OnNewToken() not working

I want to implement OnNewToken() method from FirebaseMessagingService in my Xamarin Android project that uses Firebase Cloud Messaging since FirebaseInstanceIdService and OnTokenRefreshed() are deprecated.

I tried this

    [Service]
    [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
    public class MessagingService : FirebaseMessagingService
    {
        const string TAG = "MyFirebaseMsgService";
        public override void OnNewToken(string token)
        {
            Log.Debug(TAG, "Refreshed token: " + token);
            SendRegistrationToServer(token);
        }

        public void SendRegistrationToServer(string token)
        {
            ...
        }

    }

But my OnNewToken() is not hit. What am I doing wroing. Do i need to include anything in my Android manifest file? I cannot find any documents specific to Xamarin Android about this update. Please help. Thank you so much.

Also, please provide method to get token from Activity as well. Thanks

Upvotes: 0

Views: 3124

Answers (1)

Antonio D. Stan
Antonio D. Stan

Reputation: 21

public class MyActivity : AppCompatActivity, Android.Gms.Tasks.IOnSuccessListener
{
        public void OnSuccess(Java.Lang.Object result)
        {
                var token = result.Class.GetMethod("getToken").Invoke(result).ToString();
                Log.Debug(TAG, "InstanceID token: " + token);
        }
}

and simply call

FirebaseInstanceId.Instance.GetInstanceId().AddOnSuccessListener(this);

Upvotes: 2

Related Questions