Plugin.FirebasePushNotification not refreshing token

Stesvis 1,041 Reputation points

I am using the FirebasePushNotification plugin in my xamarin forms app, and I followed all the instruction for the set up.

This is the third app where i use it, but it's never firing the OnTokenRefresh event in the .net standard project (App.xaml.cs). It works fine in all the other apps.
However, the event fires only if i implement in the MainApplication class:

    public class MainApplication : Application
        public MainApplication(IntPtr handle, JniHandleOwnership transer) : base(handle, transer)

        public override void OnCreate()

            FirebasePushNotificationManager.Initialize(this, true);

            CrossFirebasePushNotification.Current.OnTokenRefresh += Current_OnTokenRefresh;

        private void Current_OnTokenRefresh(object source, FirebasePushNotificationTokenEventArgs e)
            // this one fires

In App.xaml.cs (where I need it), it doesn't:

protected override async void OnStart()

            //----------------------- Firebase Push Notifications -----------------------//

            // token is refreshed
            CrossFirebasePushNotification.Current.OnTokenRefresh += Current_OnTokenRefresh; // *********** never fires ***************

            // Push message received event: while the app is in foreground
            CrossFirebasePushNotification.Current.OnNotificationReceived += Current_OnNotificationReceived;

            // Push message opened event: while the app ins in background
            CrossFirebasePushNotification.Current.OnNotificationOpened += Current_OnNotificationOpened;

            // Push message action tapped event
            CrossFirebasePushNotification.Current.OnNotificationAction += Current_OnNotificationAction;

What could the cause be?
I am running out of ideas, i tried in android only so far, emulator and device...


PS: I would post the question on the github page, but i almost never get an answer there so i am asking here.

A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,301 questions
{count} votes

Accepted answer
  1. Stesvis 1,041 Reputation points

    After a bunch of testing I figured it out.
    I had the MainActivity and also a SplashActivity.

    If I set MainActivity with MainLauncher = true and initialize the plugin there it works.
    If I set MainActivty with MainLauncher = false and SplashActivity with MainLauncher = true and initialize the plugin there, it doesn't work.

    So I combined the two, got rid of SplashActivity and set MainActivity with MainLauncher = true and now it works.

1 additional answer

Sort by: Most helpful
  1. Leone Boavendura 0 Reputation points

    It seems like if we subscribe to OnTokenRefresh before calling FirebasePushNotificationManager.Initialize it works.

    Please notice that App.xaml.cs is called after MainApplication.cs and perhaps that's causing the issue.

    I haven't tested enough, but it seems to have solved it for me.

    0 comments No comments