10

I'm working on a Xamarin.Android project using Xamarin.Firebase.Messaging v71.1740 nuget plugin,

I'm trying to get the FCM Token that was already generated in the FirebaseMessagingService extended class method,

public override void OnNewToken(string p0)
{
    base.OnNewToken(p0);

    // use token
}

using,

var token = FirebaseInstanceId.Instance.Token;

But this returns null and it listed as obsolete too.

3 Answers 3

20

UPDATE

GetInstanceId<InstanceIdResult>() is also deprecated in favour of FirebaseMessaging.getToken() for FCM Token and FirebaseInstallations.getId() for Instance Identifier,

enter image description here

So, FirebaseMessaging.getToken() is the recommended way now to get the FCM Token,

This is how you can consume it easily,

var token = await FirebaseMessaging.Instance.GetToken();

and FirebaseInstallations.getId() is the recommended way now to get the Instance Identifier,

This is how you can consume it easily,

var id = await FirebaseInstallations.Instance.GetId();

here, var is of the Type Java.Lang.Object. Do, token.ToString() to get the string value.

ORIGINAL

FirebaseInstanceId.Instance.Token is deprecated in favour of GetInstanceId<InstanceIdResult>(),

enter image description here

So, GetInstanceId<InstanceIdResult>() is the recommended way,

This is how you can consume it,

var instanceIdResult = await FirebaseInstanceId.Instance.GetInstanceId().AsAsync<IInstanceIdResult>();
var token = instanceIdResult.Token;
18
  • I'm trying your solution, but FirebaseInstanceId.Instance is null in my Xamarin Android app... do you have any suggestions please?
    – marcogramy
    Jan 29, 2020 at 15:08
  • 1
    assuming you have done the basic configurations correct, did you run a clean & rebuild after adding google-services.json file and setting it as a GoogleServicesJson?
    – Rizan Zaky
    Jan 29, 2020 at 15:13
  • by basic setups what I meant was, * install Xamarin.Firebase.Messaging plugin in the Xamarin.Android project * include the google-services.json file in the android project root and set its build action GoogleServicesJson * add, <uses-permission android:name="android.permission.INTERNET" /> in the AndroidManifest.xml * make sure Google Play Services are available in the Android phone you're testing * create a notification channel if above android 8 * create a FirebaseMessagingService Service with com.google.firebase.MESSAGING_EVENT IntentFilter
    – Rizan Zaky
    Jan 29, 2020 at 15:19
  • 2
    Finally, I've found the problem... using last version of Xamarin.Firebase.Core (71.1601.0) make my FirebaseInstanceId.Instance null
    – marcogramy
    Feb 3, 2020 at 17:28
  • 3
    Unfortunately it looks like this solution is also deprecated now. Apr 7, 2021 at 13:16
4

Oh look, FirebaseInstanceId.Instance is deprecated too.

Looks like the more newer way is:

var token = await FirebaseMessaging.Instance.GetToken();
1

If you are getting null on FirebaseInstanceId.Instance, when calling

var instanceIdResult = await FirebaseInstanceId.Instance.GetInstanceId().AsAsync<IInstanceIdResult>();

I have to update

Xamarin.Firebase.Messaging to version 71.1740.1

And add this to yourproject.android.csproj

<PropertyGroup> 
    <AndroidManifestMerger>manifestmerger.jar</AndroidManifestMerger> 
</PropertyGroup>

(more info)

This works for me (not sure if both are mandatory)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.