'How to get Firebase Cloud Messaging token in Xamarin?

I have a Xamarin android application and I need to get Firebase Cloud Messaging Token.

I added a file with the following class in my Android Project :

[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
[IntentFilter(new[] { "com.google.firebase.INSTANCE_ID_EVENT" })]
public class MyFirebaseIIDService : FirebaseMessagingService
{
    const string TAG = "MyFirebaseMsgService";
    public override void OnNewToken(string token)
    {
        base.OnNewToken(token);          // << Breakpoint here
        SendRegistrationToServer(token);
    }

    public void SendRegistrationToServer(string token)
    {
        // Do something with the token
    }

}

and my AndroidManifest.xml :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="13" android:versionName="13.1" package="com.MyApp.app" android:installLocation="internalOnly">
    <uses-sdk android:minSdkVersion="18" android:targetSdkVersion="30" />
    
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CAMERA" />

    <application android:label="MyApp" android:icon="@mipmap/launcher_foreground" android:hardwareAccelerated="true" android:debuggable="true">
        <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>
    </application>
</manifest>

I deploy the application to an emulated device but the breakpoint is never reached. The app is deployed though and running.

I was wondering if this code is enough or whether this class has to be referenced somewhere else.

I'm aware that this code is run only once when the application is installed. So every time I uninstall the application before testing again.

Does anyone have an idea how to solve that please ?

Thanks. Cheers,



Solution 1:[1]

If you want to get the Firebase Cloud Messaging Token, you could use the code below.

var FCMtoken = FirebaseMessaging.Instance.GetToken().ToString();

This service MyFirebaseIIDService implements an OnTokenRefresh method that is invoked when the registration token is initially created or changed.

OnTokenRefresh is invoked infrequently: it is used to update the token under the following 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.

You could check the MS docs for more details. https://docs.microsoft.com/en-us/xamarin/android/data-cloud/google-messaging/remote-notifications-with-fcm?tabs=windows#implement-the-firebase-instance-id-service

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1