CrossFirebasePushNotification.Current.OnNotificationReceived android event not firing

Viewed 1748

I can successfully send notifications to my app using the Google Firebase console, but the open event never fires (Android emulator running in Visual Studio 2019)

This is my MainApplication.cs file, simplified for brevity:

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

        public override void OnCreate()
        {
            base.OnCreate();
            CrossCurrentActivity.Current.Init(this);
            
             // these events fire as expected
            CrossFirebasePushNotification.Current.OnTokenRefresh += (s, p) =>
            {
            };
            CrossFirebasePushNotification.Current.OnNotificationReceived += (s, p) =>
            {
            };
            // this doesn't
            CrossFirebasePushNotification.Current.OnNotificationOpened += (s, p) =>
            {
            };

I'm using the FirebasePushNotificationsPlugin and my code is based on the samples that they provide.

Xamarin and mobile development is completely new to me and because I'm not sure where the issue lies, I'm reluctant to post reams of code, most of which might be irrelevant. However as some of the events the work and others don't, that seems a good place to start.

UPDATE - for clarity I've added some additional info:

I also have a SplashActivity.cs which has the MainLauncher = true attribute:

public override void OnCreate(Bundle savedInstanceState, PersistableBundle persistentState)
{
     base.OnCreate(savedInstanceState, persistentState);
     Log.Debug(TAG, "SplashActivity.OnCreate");
    
     FirebasePushNotificationManager.ProcessIntent(this, Intent);
}
    
    ...
    
protected override void OnNewIntent(Intent intent)
{
      base.OnNewIntent(intent);
      FirebasePushNotificationManager.ProcessIntent(this, intent);
}

There is also MainActivity.cs which has this:

protected override void OnCreate(Bundle bundle)
{
     TabLayoutResource = Resource.Layout.Tabbar;
     ToolbarResource = Resource.Layout.Toolbar;

     base.OnCreate(bundle);

     Xamarin.Essentials.Platform.Init(this, bundle);
     global::Xamarin.Forms.Forms.Init(this, bundle);
     LoadApplication(new App());
}
2 Answers

currently this is how it works for me add in the main application inside the onCreate this:

     CrossCurrentActivity.Current.Init(this);
     //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 = "DefaultChannel";
    
                    //Change for your default notification channel name here
                    FirebasePushNotificationManager.DefaultNotificationChannelName = "General";
                }
    
                //If debug you should reset the token each time.
    #if DEBUG
                FirebasePushNotificationManager.Initialize(this, true);
    #else
                    FirebasePushNotificationManager.Initialize(this, false);
    #endif
                CrossFirebasePushNotification.Current.OnNotificationReceived += (s, p) =>
                {
                   
                };
            }
        }
    }

and in the main activity this:

protected override void OnCreate(Bundle savedInstanceState)
        {
            FirebasePushNotificationManager.ProcessIntent(this, Intent);
        }

        protected override void OnNewIntent(Intent intent)
        {
            base.OnNewIntent(intent);
            FirebasePushNotificationManager.ProcessIntent(this, intent);
        }
Related