Reputation: 4672
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
Reputation: 4672
UPDATE
GetInstanceId<InstanceIdResult>()
is also deprecated in favour of FirebaseMessaging.getToken()
for FCM Token and FirebaseInstallations.getId()
for Instance Identifier,
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 theType
Java.Lang.Object
. Do,token.ToString()
to get thestring
value.
ORIGINAL
FirebaseInstanceId.Instance.Token
is deprecated in favour of GetInstanceId<InstanceIdResult>()
,
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
Reputation: 3636
Oh look, FirebaseInstanceId.Instance
is deprecated too.
Looks like the more newer way is:
var token = await FirebaseMessaging.Instance.GetToken();
Upvotes: 4
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>
This works for me (not sure if both are mandatory)
Upvotes: 1