Link to home
Start Free TrialLog in
Avatar of Laurent Bar
Laurent BarFlag for France

asked on

Get Android Token in Xamarin to send notification with Azure Hub

In an Xamarin Forms project under Visual Studio 2017, I can't get the FCM (Firebase Cloud Messaging) token for Android devices (.Droid project) to register it to FCM.

I created an application on the Firebase console, and retrieved the ApplicationId, ApiKey, ProjectId and GcmSenderId.

Here is the code I use in the .Droid project on Xamarin.Forms:

var options = new FirebaseOptions.Builder()
                    .SetApplicationId("xxxx")
                    .SetApiKey("xxxxxxxxxxxxx") 
                    .SetProjectId("xxxxxxxxx")
                    .SetDatabaseUrl("xxxxxxxxxxxxxxx")
                    .SetGcmSenderId("xxxxxxxxxxxx")
                    .Build();

FirebaseApp app = FirebaseApp.InitializeApp(Android.App.Application.Context, options);
String token = FirebaseInstanceId.Instance.Token; // RETURNS NULL
String authorizedEntity = "xxxxxxxxxx";
String scope = "GCM";
String token2 = FirebaseInstanceId.Instance.GetToken(authorizedEntity, scope); // ALSO RETURNS NULL

Open in new window


I do not understand why the GetToken () function returns NULL.
Can you help me please ?

For the UWP project, I can recover it by the following method:
var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync ();
String Token = channel.Uri;

I followed this doc:
https://docs.microsoft.com/fr-fr/azure/app-service-mobile/app-service-mobile-xamarin-forms-get-started-push
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.