0

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

Prabesh
  • 353
  • 1
  • 6
  • 20
  • check this it may help you https://stackoverflow.com/questions/51834864/how-to-save-a-fcm-token-in-android – AskNilesh Aug 20 '18 at 03:34
  • 1
    @NileshRathod it appears to be for Java. But for Xamarin.Android, its similar, but syntactically, a bit, different. Not working out in my case. Thanks Nilesh. – Prabesh Aug 20 '18 at 15:31
  • if you found solution than you can post as an answer so it can help others – AskNilesh Aug 29 '18 at 10:27
  • If you found a solution, please post it as an answer so it can help others. – Denny Sep 03 '18 at 12:42
  • There was a bug in the first preview builds. Simply upgrading the to the newer versions will solve this. Also make sure you install the same version for all, else you will get NullPointers – Pierre May 28 '19 at 05:41

1 Answers1

2
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);