简体   繁体   中英

OnTokenRefresh never called in Xamarin Android

I'm trying to implement remote notifications in my Xamarin Android project. I followed step-by-step directions found in various guides but the OnTokenRefresh method is never called, even the first time, so it looks like my app does not receive any kind of token or even does not make any type of firebase registration.

  1. I have added Firebase to my Android app and downloaded google-services.json
  2. I included google-services.json in my Xamarin Android Project and set the Build Action to GoogleServicesJson
  3. I inserted this code in AndroidManifest.xml

     <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver" android:exported="false" /> <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND"> <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> <category android:name="${applicationId}" /> </intent-filter> </receiver> 
  4. I created a class that extends FirebaseInstanceIdService

     [Service] [IntentFilter(new[] { "com.google.firebase.INSTANCE_ID_EVENT" })] class FirebaseRegistrationService : FirebaseInstanceIdService { const string TAG = "FirebaseRegistrationService"; public override void OnTokenRefresh() { var refreshedToken = FirebaseInstanceId.Instance.Token; System.Diagnostics.Debug.WriteLine(TAG, "Refreshed token: " + refreshedToken); MainActivity.CurrentActivity.RunOnUiThread(() => RegisterDeviceOnServer(refreshedToken)); } public void RegisterDeviceOnServer(string refreshedToken) { // Custom implementation } } 

As I have already said OnTokenRefresh is never called. What I can not understand is: who does the Firebase registration to receive a valid token? Is there a missing directive? Is there a missing method that makes this registration? Why OnTokenRefresh is never called?

You should start the service in your Activity like this :

var intent = new Intent(this, typeof(FirebaseRegistrationService)); StartService(intent);

Also clean the solution once and run the app again. There is a bug in firebase for xamarin.

According to this document , (see section titled: Implement the Firebase Instance ID Service) OnTokenRefresh is only called under a few circumstances:

  • When the app is installed or uninstalled.
  • When the user deletes app data.
  • When the app erases the Instance ID.
  • When the security of the token has been compromised.

In order to trigger OnTokenRefresh, you should first uninstall the app from the device. Then when you reinstall the app and open it for the first time, OnTokenRefresh will be called.

You can avoid cleaning the solution every time by disabling

Preserve application data cache on device between deploys

under

Tools -> Options -> Xamarin -> Android Settings in VS2017 Enterprise.

This made OnTokenRefresh to be called after every redeploy.

if you are using emulator make sure you install google apps as it depends on google play services package exist in your device

if you are using Genymotion you can press install Gapps 在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM