How to receive custom data when app is terminated through push notification using firebase_messaging?
Solution 1
This is the format that your payload should take with data included. It does work as this is how I have gotten it to work in my project. Additionally to get everything to work correctly, there is an answer here that might be helpful for general project setup:
{
notification: {
title: 'A title',
body: 'The body.'
},
data: {
// your data here
},
android: {
ttl: 3600 * 1000,
notification: {
click_action: 'FLUTTER_NOTIFICATION_CLICK'
}
},
// The below is for iOS specific properties
// apns: {
// payload: {
// aps: {
// badge: 42
// }
// }
// },
tokens: [] // or a single 'token'
}
Solution 2
I think, you already getting data onLaunch, just need to wait a little bit. I wrote it because I came across the same thing.
Can you try this,
firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async { .. },
onResume: (Map<String, dynamic> message) async { .. },
onLaunch: (Map<String, dynamic> message) async {
WidgetsBinding.instance.addPostFrameCallback((_) {
Timer(const Duration(seconds: 7), () {
// your process...
});
});
},
);
Also in my situation, i used it like that, and solved.
onLaunch: (Map<String, dynamic> message) async {
WidgetsBinding.instance.addPostFrameCallback((ignored) {
Timer.periodic(const Duration(seconds: 1), (timer) {
if (CurrentUser.currentUser != null) {
timer.cancel(); // important **
// redirect process with post details inside data
if (message['data']['type'] == NotifTypes.NEW_POST)
goGroupAndPost(Map.from(message['data']));
}
});
});
},
Comments
-
Stefano Saitta over 1 year
As in title, what is the current workaround in order to receive custom data when the user click on a notification when the app is terminated?
Seems like on Android is not possible to receive a data message in the onLaunch (which is the ideal way) On IOS i hadn't tried yet since i'm facing this issue first.
Any clue?
Additional infos: The notification that i'm sending through a
firebase cloud function
are in this form:"message":{ "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...", "notification":{ "title":"Portugal vs. Denmark", "body":"great match!" }, "data" : { "Nick" : "Mario", "Room" : "PortugalVSDenmark" } } }
https://firebase.google.com/docs/cloud-messaging/concept-options
on the
onResume
i'm able to perform an action, but theonLaunch
seems to not be called.