Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 557
Rizan Zaky Avatar asked Jan 27 '20 12:01

Rizan Zaky


People also ask

How to get the token of a firebase instance?

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;}

Is the firebaseinstanceid gettoken () method deprecated?

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.

How to remove firebaseinstanceidservice from Android?

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:

What is the latest version of Xamarin Firebase messaging?

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.


3 Answers

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;
like image 122
Rizan Zaky Avatar answered Nov 09 '22 03:11

Rizan Zaky


Oh look, FirebaseInstanceId.Instance is deprecated too.

Looks like the more newer way is:

var token = await FirebaseMessaging.Instance.GetToken();
like image 43
jklemmack Avatar answered Nov 09 '22 03:11

jklemmack


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)

like image 40
Juan Redondo Avatar answered Nov 09 '22 02:11

Juan Redondo