FirebasePushNotificationPlugin icon indicating copy to clipboard operation
FirebasePushNotificationPlugin copied to clipboard

OnNotificationOpened not hit when app killed on Android

Open cvchris opened this issue 3 years ago • 4 comments

🐛 Bug Report

The notification comes through even when app is killed, but when clicking on notification, it doesn't run OnNotificationOpened method (which is on MainApplication.cs) and the body is:


CrossFirebasePushNotification.Current.OnNotificationOpened += (s, p) =>
            {
                string tag = "aimmsapp";

                Android.Util.Log.Info(tag, "Opened");
                System.Diagnostics.Debug.WriteLine("Opened");
                foreach (var data in p.Data)
                {
                    System.Diagnostics.Debug.WriteLine($"{data.Key} : {data.Value}");
                }


            };

I can see from the device logs that this method is not ran, but when the device is in background, it runs when I click the notification.

The payload sent is:

var pushData = new
                {
                    to = deviceToken,
                    content_available = true,
                    data = new
                    {
                        body = message, 
                        payload = new
                        {
                            android = new
                            {
                                title = title,
                                alert = message,
                                sound = "default",
                                vibrate = true,
                                icon = "appicon"
                            },
                        },
                        extData = extData
                    },
                    priority = "high"
                };

Expected behavior

It should ran OnNotificationOpened method on MainApplication when I click the notification.

Reproduction steps

Kill the app, and send notification. OnNotificationOpened method should not ran when clicking the notification.

Configuration

Bug on Poco X3 NFC and Samsung Galaxy tab A (2016) Version: 3.4.1

Platform:

  • [ ] :iphone: iOS
  • [X] :robot: Android
  • [ ] :checkered_flag: WPF
  • [ ] :earth_americas: UWP
  • [ ] :apple: MacOS
  • [ ] :tv: tvOS
  • [X] :monkey: Xamarin.Forms

cvchris avatar Jan 14 '22 02:01 cvchris

Hello, have you found a solution to this ?

ghost avatar Mar 14 '22 10:03 ghost

No solution yet.

cvchris avatar Mar 16 '22 14:03 cvchris

If you have a splash Activity which is set to MainLauncher = true, Update your Activity like below. `public class SplashActivity : Activity { protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); FirebasePushNotificationManager.ProcessIntent(this,Intent); } protected override void OnNewIntent(Intent intent) { base.OnNewIntent(intent); FirebasePushNotificationManager.ProcessIntent(this,intent); } protected override void OnResume() { base.OnResume(); Task startupWork = new Task(() => { SimulateStartup(); }); startupWork.Start(); }

    async void SimulateStartup()
    {
        await Task.Delay(0000); // Simulate a bit of startup work.
        StartActivity(new Intent(Application.Context, typeof(MainActivity)));
    }
}`

sumityadu07 avatar Mar 18 '22 04:03 sumityadu07

Hello i solved this by moving the event set to the Application constructor instead of the OnStart() method described in the sample

  public App()
    {
	InitializeComponent();
        MainPage = new Views.StartupPage();
        CrossFirebasePushNotification.Current.OnNotificationReceived += async (s, p) =>
			{
				await Task.Run(() =>
				  { 
						  FirebaseClient.Model.Notification Notification = JsonConvert.DeserializeObject<FirebaseClient.Model.Notification>(p.Data["Notification"].ToString());
						  NotificationsViewModel.Default.OnNewNotification(Notification);
					  
				  });
			};
}

it seems that the event is launched too early for the app to catch even though the enableDelayedResponse parameter is set to true

LoadApplication(new App());
FirebasePushNotificationManager.ProcessIntent(this, Intent);

This solved my problem and when user click the notification the appropriate page will appear but only on version 3.3.10 updating to the latest version 3.4.1 broke it again

ghost avatar Mar 21 '22 08:03 ghost