Reputation: 135
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.
google-services.json
google-services.json
in my Xamarin Android Project and set the Build Action to GoogleServicesJsonI 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>
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?
Upvotes: 4
Views: 3278
Reputation: 21462
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
Upvotes: 1
Reputation: 11
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.
Upvotes: 1
Reputation: 455
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.
Upvotes: 3
Reputation: 1961
According to this document, (see section titled: Implement the Firebase Instance ID Service) OnTokenRefresh is only called under a few circumstances:
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.
Upvotes: 2