Home > Back-end >  How to properly register FCM in Xamarin.Android targeting API 33
How to properly register FCM in Xamarin.Android targeting API 33

Time:01-24

I want to update my app to support the new requirements of Android API 33 (push notification permission request) but I am not able to get the token, or even get the OnNewToken() to fire.

I do have <uses-permission android:name="android.permission.POST_NOTIFICATIONS"/> in my manifest, I am requesting the permission and allowing it, but every time I try to initialize firebase, this is the error I get in the console:

E/FirebaseInstanceId(25547): Topic sync or token retrieval failed on hard failure exceptions: AUTHENTICATION_FAILED. Won't retry the operation.

This is how I'm requesting the permissions:

if (isNotificationsAllowed == Android.Content.PM.Permission.Denied)
{
    CrossCurrentActivity.Current.Activity.RequestPermissions(new [ {"android.permission.POST_NOTIFICATIONS"}, 0);
                    
    FirebaseApp.InitializeApp(CrossCurrentActivity.Current.AppContext);
    return;
}

This is how I'm trying to get the token:

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

On API 32 and lower, since there's no need to request the permission, the OnNewToken() gets fired and has no issue generating the FCM token.

Also I have to specify that I am trying on an Android Emulator that has Google Play Services (Same emulator with api 30 works just fine).

These are the relevant packages with their versions:

<PackageReference Include="Xamarin.Firebase.Messaging" Version="120.3.0"/>
<PackageReference Include="Xamarin.Forms" Version="4.8.0.1451" />
<PackageReference Include="Xamarin.GooglePlayServices.Base" Version="118.1.1"/>
<PackageReference Include="Xamarin.Google.Dagger" Version="2.27.0"/>

If anyone has any idea, it would be greatly appreciated!

Thanks!

CodePudding user response:

Android 13 introduces a new runtime notification permission and here is the documentation on this : Notification runtime permission.

Althogh the FCM SDK (version 23.0.6 or higher) includes the POST_NOTIFICATIONS permission defined in the manifest (no downside to add it in Manifest), you also have to request the runtime permission from the user. For more info, you could refer to the official Firebase documentation which gives an example how to request permission: Request runtime notification permission on Android 13 .

Hope it works for you.

CodePudding user response:

Well, seems like the solution, for me, was a bit underwhelming. I simply deleted the Emulator with API level 33 and created a new one. Things went smoothly.

  • Related