FirebasePushNotificationPlugin
FirebasePushNotificationPlugin copied to clipboard
OnNotificationReceived not called when app is closed/killed/terminated
🐛 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
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)); };
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.
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); }
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 did you manage to resolve this issue ?
any updates on this? Thanks a lot! @ALBERT-son @kamilkosek
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.
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
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.
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?