Apt3kStudio / Phone

2 stars 0 forks source link

Implement the Firebase Instance ID Service #32

Closed dioscarr closed 5 years ago

dioscarr commented 5 years ago

Now it’s time to write some code! Add a new C# file to your project (in Visual Studio, right click your project name and select Add > New Item > Class). Give it the name MyFirebaseIIDService.cs and input the following code:

using System;
using Android.App;
using Firebase.Iid;
using Android.Util;

namespace FCMExample
{
    [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);
        }
    }
}

Change the namespace FCMExample above to the namespace of your app.

That’s all there is to it!

The OnTokenRefresh method is invoked when the registration token is created or changed. Because the token is logged to the Output window whenever it’s updated, you’ll be able to see it while the app is running. You’ll enter this token into the Firebase Console when you’re ready to send a test notification to your app.

dioscarr commented 5 years ago

Reference Issue #28