FirebasePushNotificationPlugin icon indicating copy to clipboard operation
FirebasePushNotificationPlugin copied to clipboard

OnNotificationReceived not called when app is closed/killed/terminated

Open stavroaudi opened this issue 3 years ago • 11 comments

🐛 Bug Report

I'm not sure if this is by design from Firebase, but OnNotificationReceived() is called when app is in the foreground and background, but not when the app is killed. I want to store data that is in the message onto the app's database, but it doesn't seem to save it.

Expected behavior

OnNotificationReceived to be called when the app is closed/killed/terminated.

Reproduction steps

My sample notification is:

{ "to": "/topics/testtopic", "content_available": true, "data": { "testdata1": "123", "testdata2": "456", "testdata3": "789" }, "priority": "high" }

I have OnNotificationReceived in my app.xaml.cs.

Configuration

Version: 3.3.10

Platform:

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

stavroaudi avatar Sep 10 '21 07:09 stavroaudi

In my project.notification is still received even when the application is closed.but i use localpush notification // Push message received event CrossFirebasePushNotification.Current.OnNotificationReceived += (s, p) => { NotificationCenter.Current.Show(new NotificationRequest() { BadgeNumber = 1, Description = p.Data["title"].ToString(), Title = p.Data["content"].ToString() }); ; System.Diagnostics.Debug.WriteLine(DateTime.Now.ToString()+":Received"+JsonConvert.SerializeObject(p.Data)); };

khoatbd55 avatar Sep 18 '21 16:09 khoatbd55

Can you show an example payload where this works for you? Does it also work on Android? If so can you show where MessageReceived is triggered because I got it to work on iOS but not Android, even when putting on MainApplication class OnMessageReceived.

stavroaudi avatar Sep 18 '21 20:09 stavroaudi

in app.xaml.cs share code

// Push message received event CrossFirebasePushNotification.Current.OnNotificationReceived += (s, p) => { NotificationCenter.Current.Show(new NotificationRequest() { BadgeNumber = 1, Description = p.Data["title"].ToString(), Title = p.Data["content"].ToString() }); ; System.Diagnostics.Debug.WriteLine(DateTime.Now.ToString()+":Received"+JsonConvert.SerializeObject(p.Data)); };

in mainapplication.cs android [Application] public class MainApplication : Application { public MainApplication(IntPtr handle, JniHandleOwnership transer) : base(handle, transer) { }

public override void OnCreate() { base.OnCreate();

//Set the default notification channel for your app when running Android Oreo if (Build.VERSION.SdkInt >= 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.Max; }

//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) => { }; } }

in MainActivity.cs android

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

base.OnCreate(savedInstanceState); global::Xamarin.Forms.Forms.Init(this, `savedInstanceState); LoadApplication(new Portable.App()); FirebasePushNotificationManager.ProcessIntent(this, Intent); }

khoatbd55 avatar Sep 19 '21 19:09 khoatbd55

Is that issue resolved? I still get a notification when the app is closed/killed/terminated, but OnNotificationReceived doesn't work.

The following error occurs during background notification. => [FirebaseMessaging] Missing Default Notification Channel metadata in AndroidManifest. Default value will be used.

ALBERT-son avatar Oct 29 '21 07:10 ALBERT-son

@ALBERT-son did you manage to resolve this issue ?

kamilkosek avatar Nov 09 '21 15:11 kamilkosek

any updates on this? Thanks a lot! @ALBERT-son @kamilkosek

cvchris avatar Jan 14 '22 01:01 cvchris

Same question - when the app is minimized I get the notifications, but when the app is in the foreground the normal notification doesn't show, also when the app is killed the notifications don't show.

rezamohamed avatar Jan 17 '22 07:01 rezamohamed

This is expected behavior from firebase if using an notificaiton message. If you want OnNotificationReceived to be hit use a data message. https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages

LevyJonkman avatar Jan 25 '22 10:01 LevyJonkman

This is expected behavior from firebase if using an notificaiton message. If you want OnNotificationReceived to be hit use a data message.

https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages

I am actually using a data message and it is working when the app is in background. Though, when the app is killed (from task manager), it doesn't hit the OnNotificationOpened method.

cvchris avatar Jan 25 '22 10:01 cvchris

We also face the similar issues on iOS and Android. On our end seems like following:

Android foreground: notification is not received at all Android background: notification is received, event is fired properly (after tapping on notification) Android killed: notification is received, event is fired properly (after tapping on notification)

iOS foreground: notification is received, event is fired properly (after tapping on notification) iOS: background: notification is received, event is fired properly (after tapping on notification) iOS killed: notification is not received at all

We are using notification message combined with data message, as Firebase docs recommends:

Use notification messages when you want FCM to handle displaying a notification on your client app's behalf. Use data messages when you want to process the messages on your client app.

FCM can send a notification message including an optional data payload. In such cases, FCM handles displaying the notification payload, and the client app handles the data payload.

Any ideas? Any thoughts how this could be fixed?

psmoq avatar Jan 27 '22 11:01 psmoq