Reputation: 2014
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 the onLaunch
seems to not be called.
Upvotes: 9
Views: 2951
Reputation: 3017
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']));
}
});
});
},
Upvotes: 2
Reputation: 3973
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'
}
Upvotes: 2
Reputation: 1647
Telling if your Activity is in the foreground (and reacting differently) can be a little tricky. My favorite way is to register a local BroadcastReceiver
in the Activity and then do a sendBroadcast
from the service. You will get true from that method if your receiver is running (meaning your app is in the foreground, and false otherwise.
You can see an example of this (using GCM, but the logic is the same for FCM) Source
Also this is a great example MyFirebaseMessagingService
Upvotes: 0