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.
As "FirebaseInstanceId.Instance.Token" became obsolete, for getting the token you can use the following: 1. Implement in your class Android.Gms.Tasks.IOnSuccessListener - if you also extend Java.Lang.Object, you can remove the Dispose () and Handle {get;}
The FirebaseInstanceId getToken () method is deprecated. This method is deprecated. In favour of getInstanceId (). You do not need to use FirebaseInstanceIdService to receive a token.
You can safely remove FirebaseInstanceIdService service Now we need to @Override onNewToken () get Token in "FirebaseMessagingService". In AndroidManifest.xml Get Token in your Activity : .getToken (); is also deprecated if you need to get token in your activity then use as following:
The current release version of Xamarin.Firebase.Messaging is bound to the older APIs (11.4.2). There is a preview release which binds version 17.1.0 of the Firebase Cloud Messaging APIs. We will update this guide and published the new version when the Xamarin.Android bindings are promoted out of pre-release.
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;
Oh look, FirebaseInstanceId.Instance
is deprecated too.
Looks like the more newer way is:
var token = await FirebaseMessaging.Instance.GetToken();
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With