4

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?

Filippo Auletta
  • 135
  • 4
  • 14
  • Are you getting `unsuccessful` or `successful` FirebaseApp initialization? https://stackoverflow.com/a/42159446/4984832 – SushiHangover Oct 16 '17 at 13:13
  • In MainActivity I execute: `FirebaseApp.InitializeApp(this);` but it returns this Java Exception: "Java.Lang.IllegalStateException: Default FirebaseApp is not initialized in this process . Make sure to call Firebase…" – Filippo Auletta Oct 17 '17 at 09:25
  • 5
    I've solved the problem making the Clean of the Solution. Incredible. – Filippo Auletta Oct 17 '17 at 10:36
  • 2
    I found uninstalling the application and cleaning the solution to work for me. – kidshaw Mar 31 '18 at 08:12
  • 2
    Anyone facing this issue, try to "Clean" the solution, which involve manually delete \obj\debug or \obj\release folder out of your project. – Felix Oct 24 '18 at 05:13

4 Answers4

3

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.

Benoit Canonne
  • 445
  • 6
  • 6
  • Is this still a bug requiring the starting of the activity as described? Or has this been fixed in subsequent Xamarin.Android updates? In my personal tests as of this writing, I haven't needed to do the above code but it would be helpful for future searches on this topic to know whether the workaround is still needed or not. – Architekt May 16 '18 at 23:35
  • 1
    I dont think you've to start the service everytime manually. It happens autmatically. Theres a bug with FCM in xamarin that will get resolved when you uninstall the app,remove bin and debug folder and rebuild your app – Manu Mohan Sep 06 '18 at 06:59
2

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.

cvanbeek
  • 1,796
  • 21
  • 30
1

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.

scopchanov
  • 7,966
  • 10
  • 40
  • 68
Jesse G
  • 11
  • 2
0

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 Gappsenter image description here

Mina Fawzy
  • 20,852
  • 17
  • 133
  • 156