Reputation: 11
I know the firebaseMessaging.configure is deprectated and I am trying to switch to the new format since the OnMessage, OnLaunch and OnResume are all on the same line I was struggling to achieve this I came accross some references but not sure how to combine the three in one like the previous one, not sure if I am supposed to do that
Images bellow for before and after
void configureFirebase(FirebaseMessaging _firebaseMessaging) {
try {
_firebaseMessaging.configure(
onMessage: notificationOnMessage,
onLaunch: notificationOnLaunch,
onResume: notificationOnResume,
/* FirebaseMessaging.onMessage.listen((RemoteMessage message) {
RemoteNotification notification = message.notification;
AndroidNotification android = message.notification?.android;
});
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
print('A new onMessageOpenedApp event was published!');
Navigator.pushNamed(context, '/message',
arguments: MessageArguments(message, true));
});
*/
} catch (e) {
print(CustomTrace(StackTrace.current, message: e));
print(CustomTrace(StackTrace.current, message: 'Error Config Firebase'));
}
}
Future notificationOnResume(Map<String, dynamic> message) async {
print(CustomTrace(StackTrace.current, message: message['data']['id']));
try {
if (message['data']['id'] == "orders") {
settingRepo.navigatorKey.currentState
.pushReplacementNamed('/Pages', arguments: 2);
} else if (message['data']['id'] == "messages") {
settingRepo.navigatorKey.currentState
.pushReplacementNamed('/Pages', arguments: 3);
}
} catch (e) {
print(CustomTrace(StackTrace.current, message: e));
}
}
Future notificationOnLaunch(Map<String, dynamic> message) async {
String messageId = await settingRepo.getMessageId();
try {
if (messageId != message['google.message_id']) {
await settingRepo.saveMessageId(message['google.message_id']);
if (message['data']['id'] == "orders") {
settingRepo.navigatorKey.currentState
.pushReplacementNamed('/Pages', arguments: 2);
} else if (message['data']['id'] == "messages") {
settingRepo.navigatorKey.currentState
.pushReplacementNamed('/Pages', arguments: 3);
}
}
} catch (e) {
print(CustomTrace(StackTrace.current, message: e));
}
}
Future notificationOnMessage(Map<String, dynamic> message) async {
Fluttertoast.showToast(
msg: message['notification']['title'],
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.TOP,
timeInSecForIosWeb: 6,
);
}
}
Upvotes: 0
Views: 1153
Reputation: 7696
Update the code in the try block to this below:
FirebaseMessaging.instance.getInitialMessage().then((message) {
if (message != null) {
return notificationOnLaunch(message.data);
}
});
FirebaseMessaging.onMessage
.listen((RemoteMessage message) => notificationOnMessage(message.data));
FirebaseMessaging.onMessageOpenedApp
.listen((RemoteMessage message) => notificationOnResume(message.data));
Explanation:
onLaunch
.RemoteMessage
] that is called when an incoming FCM payload is received whilst the Flutter instance is in the foreground." This replaces onMessage
.RemoteMessage
] that is called when a user presses a notification message displayed via FCM.
A Stream event will be sent if the app has opened from a background state (not terminated)." This replaces onResume
.Upvotes: 1