Rizan Zaky
Rizan Zaky

Reputation: 4672

FirebaseInstanceId.Instance.Token is depricated and returns null in Xamarin.Android using Xamarin.Firebase.Messaging

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.

Upvotes: 11

Views: 10657

Answers (3)

Rizan Zaky
Rizan Zaky

Reputation: 4672

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;

Upvotes: 21

jklemmack
jklemmack

Reputation: 3636

Oh look, FirebaseInstanceId.Instance is deprecated too.

Looks like the more newer way is:

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

Upvotes: 4

Juan Redondo
Juan Redondo

Reputation: 173

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)

Upvotes: 1

Related Questions