Plugin.FirebasePushNotification not refreshing token

Stesvis 1,041 Reputation points
2021-01-26T04:30:16.823+00:00

I am using the FirebasePushNotification plugin in my xamarin forms app, and I followed all the instruction for the set up.
https://github.com/CrossGeeks/FirebasePushNotificationPlugin

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:

[Application]
    public class MainApplication : Application
    {
        public MainApplication(IntPtr handle, JniHandleOwnership transer) : base(handle, transer)
        {
        }

        public override void OnCreate()
        {
            base.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()
        {
            base.OnStart();

            //----------------------- Firebase Push Notifications -----------------------//
            CrossFirebasePushNotification.Current.RegisterForPushNotifications();

            // 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...

Thanks

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

Xamarin
Xamarin
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
    2021-01-26T22:09:30.493+00:00

    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
    2023-03-31T15:29:22.6466667+00:00

    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