How to Handle Notification When App in Background in Firebase

How to handle notification when app in background in Firebase

Push notification when app is in foreground (using Firebase triggers)

If depends on whether your message contains a data property, a notification property, or both.

If the message only contains a notification property, it'll delivered to your application code if the app is in the foreground, but handled by the system when the app is in the background.

If your message contains a data property, it'll always be delivered to your application code. If it also contains a notification property, that part will be handled by the system when the app is in the background.

For more on this, see the Firebase documentation on message types.

handle push notification (FCM) while app is in background / kill - Android

Solution:

  1. If you are sending push notification from firebase console & your app is in background or killed then MyFirebaseMessagingService won't get called. (This is not mentioned in firebase documentation)

  2. If you are sending push notification from firebase console & your app is in foreground then MyFirebaseMessagingService will get called.

  3. If you are sending push notification from your server(I mean back-end i.e a website or some web based dashboard) & your app is in background or kill or in foreground then MyFirebaseMessagingService will get called.

Firebase onMessageReceived not called when app in background

Navigate to another screen when I click on Firebase push notification either in foreground or background in Flutter

In your code, you are using the flutter_local_notifications plugin and you are creating local notification when you get a push notification from firebase messaging.

Since the notification is not created by firebase messaging so, you are not getting on tap callback in getInitialMessage and onMessageOpenedApp.

In order to get on tap callback on local notifications, you can pass a callback function while initializing flutter_local_notifications

flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: onSelectNotification);

void selectNotification(String payload) async {
if (payload != null) {
debugPrint('notification payload: $payload');
// Here you can check notification payload and redirect user to the respective screen
await Navigator.push(
context,
MaterialPageRoute<void>(builder: (context) => SecondScreen(payload)),
);
}
}

For more, you can check flutter_local_notifications documentation



Related Topics



Leave a reply



Submit