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:
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)
If you are sending push notification from firebase console & your app is in foreground then MyFirebaseMessagingService will get called.
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
Android: Rotate Image in Imageview by an Angle
Android Gradle Error When Sync Style Attribute Not Found
Android Volley Basicnetwork.Performrequest() Unexpected Response Code 404
Gradle Error: Could Not Execute Build Using Gradle Distribution
How to Fix Attempt to Invoke Interface Method on a Null Object Reference
How to Start Android Application on Device Boot
How to Show the Number Keyboard on an Edittext in Android
Decrease Image Size Without Losing Its Quality in Android
Flutter:Renderbox Was Not Laid Out: Renderrepaintboundary#58C65 Relayoutboundary=Up1 Needs-Paint
Java.Io.Ioexception: Permission Denied Cannot Create File
Access Ro.Serialno from Native in Android 8
How to Refresh a Previous Activity After the Back Button Is Pressed
How to Measure Distance to Object With Camera
How to Make a New Line or Tab in <String> Xml (Eclipse/Android)