CrossGeeks/FirebasePushNotificationPlugin

CrossFirebasePushNotification.Current.OnTokenRefresh not always firing at app startup

Yahya0007 opened this issue · 1 comments

💬 Questions and Help

Hi

The CrossFirebasePushNotification.Current.OnTokenRefresh is not firing around 1/3 of the time at app startup.

The code is as below.

Thanks

Regards

App.xaml.cs

    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();

            CrossFirebasePushNotification.Current.OnTokenRefresh += Current_OnTokenRefresh;
        }

        private void Current_OnTokenRefresh(object source, FirebasePushNotificationTokenEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine($"Token: {e.Token}");
        }
    }

MainActivity.cs

namespace MyApp.Droid
{
    [Activity(Label = "MyApp", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize )]
    public partial class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        partial void OnPreCreate(Bundle savedInstanceState);

        protected override void OnCreate(Bundle savedInstanceState)
        {
            this.OnPreCreate(savedInstanceState);

            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(savedInstanceState);

            FirebasePushNotificationManager.ProcessIntent(this, Intent);

            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);

            LoadApplication(new App());

            this.OnPostCreate(savedInstanceState);
        }

        protected override void OnNewIntent(Intent intent)
        {
            base.OnNewIntent(intent);

            FirebasePushNotificationManager.ProcessIntent(this, intent);
        }
    }
}

Application.cs in Android project root folder

namespace MyApp.Droid
{
    [Application]
    public class MainApplication : Application
    {
        public override void OnCreate()
        {
            base.OnCreate();

            //Set the default notification channel for your app when running Android Oreo
            if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
            {
                //Change for your default notification channel id here
                FirebasePushNotificationManager.DefaultNotificationChannelId = "FirebasePushNotificationChannel";

                //Change for your default notification channel name here
                FirebasePushNotificationManager.DefaultNotificationChannelName = "General";

                FirebasePushNotificationManager.DefaultNotificationChannelImportance = NotificationImportance.High;
            }

            //If debug you should reset the token each time.
#if DEBUG
            FirebasePushNotificationManager.Initialize(this, true);
#else
            FirebasePushNotificationManager.Initialize(this, false);
#endif

            //Handle notification when app is closed here
            CrossFirebasePushNotification.Current.OnNotificationReceived += (s, p) =>
            {
            };
        }
    }
}

Same problem. Did you find solution?