jbassking10
jbassking10

Reputation: 803

Xamarin Android Firebase refresh token

I implemented Firebase messaging into our Xamarin Android app. The very first time the app was run it called OnTokenRefresh() as excepted. I uninstalled the app and re-ran the debugger which reinstalled the app. This time though, OnTokenRefresh was not called. In fact, I haven't been able to get it to be called a second time for a single device.

I tried making a token service to get the token but it always throws an exception. The method is very simple:

 [assembly: 
 Xamarin.Forms.Dependency(typeof(MyApp.Droid.Services.NotificationToken))]
  namespace MyApp.Droid.Services
 {
 class NotificationToken : INotificationToken
 {
    public string GetToken()
    {            
        var token = "";
        if (Firebase.Iid.FirebaseInstanceId.Instance != null)
            token = Firebase.Iid.FirebaseInstanceId.Instance.Token;
        return token;
     }
   }
}

The exception is thrown on the check if Instance is null.

 [GoogleAccountDataServiceImpl] getToken() -> BAD_AUTHENTICATION. Account: <ELLIDED:-119322310>, App: com.google.android.gms, Service: oauth2:https://www.googleapis.com/auth/gcm

I'm calling the service well after the main activity has been created. It's called after the user logs into our app.

I have the google-services.json included in our Android project. I'm pretty certain it's being used since the initial time the app was installed it created the token and called OnTokenRefresh.

     [Service]
[IntentFilter(new[] { "com.google.firebase.INSTANCE_ID_EVENT" })]
public class MyFirebaseIIDService : FirebaseInstanceIdService
{
    const string TAG = "MyFirebaseIIDService";

    public override void OnCreate()
    {
        base.OnCreate();
        //FirebaseInstanceId.Instance.GetToken();
    }
    public override void OnTokenRefresh()
    {
        var refreshedToken = FirebaseInstanceId.Instance.Token;
        App.NotificationToken.Token = refreshedToken;
        Log.Debug(TAG, "Refreshed token: " + refreshedToken);
    }        
}

Upvotes: 0

Views: 1981

Answers (1)

DFalconer
DFalconer

Reputation: 11

In Visual Studio 2017 you can disable the following preference to ensure that the Firebase Token is refreshed between deployments to your device. This was learned through trial and error.

Tools > Options > Xamarin > Android > Preserve Application Data/Cache between deployments

Hope this helps.

Upvotes: 1

Related Questions