Firebase_messaging onResume 和 onLaunch 回调不会在 flutter 中被调用
Firebase_messaging onResume and onLaunch callbacks are not called in flutter
我想做什么
我想在用户点击通知托盘中的 fcm 通知时导航到特定屏幕。
我的代码
我已按照 pub.dev 中提到的 6.0.9 版本完全相同的说明进行操作。
这是我的实现:
_fcm.configure(
onMessage: (Map<String, dynamic> message) async {
print('@onMessage: $message');
},
onResume: (Map<String, dynamic> message) async {
print('@onResume: $message');
Navigator.push(context,
MaterialPageRoute(builder: (context) => NotificationsScreen()));
},
onLaunch: (Map<String, dynamic> message) async {
print('@onLaunch: $message');
Navigator.push(context,
MaterialPageRoute(builder: (context) => NotificationsScreen()));
});
插件版本: firebase_messaging:^6.0.9
我还添加了这些配置:
app/build.gradle:
implementation 'com.google.firebase:firebase-messaging:20.1.0'
project/build.gradle:
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:'1.3.50'"
classpath 'com.google.gms:google-services:4.3.3'
预期输出
当用户在后台点击通知时,调用 onResume 或 onLaunch 回调并将他重定向到提到的屏幕。
实际输出
onResume 和 onLaunch 没有被调用,而 onMessage 在应用程序处于前台时被正确调用。
如果通知负载未命中 "click_action": "FLUTTER_NOTIFICATION_CLICK" 那么
onclick 和 onresume 功能将不会 called.But onmessage 功能将完美运行。
在终端
中尝试使用此有效载荷
DATA='{"notification": {"body": "this is a body","title": "this is a title"}, "priority": "high", "data": {"click_action": "FLUTTER_NOTIFICATION_CLICK", "route":"booking", "bookingId":"319", "userType":"host", "status": "done"}, "to": "<FCM TOKEN>"}'
curl https://fcm.googleapis.com/fcm/send -H "Content-Type:application/json" -X POST -d "$DATA" -H "Authorization: key=<FCM SERVER KEY>"
FLUTTER_NOTIFICATION_CLICK 必须调用 onresume 和 onmessage
Complete guide for notification handling:
In payload = [
notification: {
title: _title //Title of the notification
data: {
message: msg,
click_action: 'FLUTTER_NOTIFICATION_CLICK'
// Any message that you want to send in notification
screen: 'chat'
}
}
]
//Inside app you configure as:
FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
handleMessaging() {
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
// showNotification(
// message['notification']['title'], message['notification']['body']);
print("onMessage: $message");
print('Data is: '+ message['data']['screen'].toString());
},
onLaunch: (Map<String, dynamic> msg) {
print("Called onLaunch");
print(msg);
print('Data is: '+ msg['data']['screen'].toString());
if(msg['data']['screen'].toString() == 'chat app') {
return //Navigate to chat app
}
return null;
},
onResume: (Map<String, dynamic> msg) {
//(App in background)
// From Notification bar when user click notification we get this event.
// on this event navigate to a particular page.
print(msg);
// // Assuming you will create classes to handle JSON data. :)
// Notification ns = Notification(title: msg['title'], body: msg['body']);
return null;
},
onMessage: (Map<String, dynamic> msg) {
// (App in foreground)
// on this event add new message in notification collection and hightlight the count on bell icon.
// Add notificaion add in local storage and show in a list.
// updataNotification(msg);
print(msg);
},
);
}
我想做什么
我想在用户点击通知托盘中的 fcm 通知时导航到特定屏幕。
我的代码
我已按照 pub.dev 中提到的 6.0.9 版本完全相同的说明进行操作。
这是我的实现:
_fcm.configure(
onMessage: (Map<String, dynamic> message) async {
print('@onMessage: $message');
},
onResume: (Map<String, dynamic> message) async {
print('@onResume: $message');
Navigator.push(context,
MaterialPageRoute(builder: (context) => NotificationsScreen()));
},
onLaunch: (Map<String, dynamic> message) async {
print('@onLaunch: $message');
Navigator.push(context,
MaterialPageRoute(builder: (context) => NotificationsScreen()));
});
插件版本: firebase_messaging:^6.0.9
我还添加了这些配置:
app/build.gradle:
implementation 'com.google.firebase:firebase-messaging:20.1.0'
project/build.gradle:
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:'1.3.50'"
classpath 'com.google.gms:google-services:4.3.3'
预期输出
当用户在后台点击通知时,调用 onResume 或 onLaunch 回调并将他重定向到提到的屏幕。
实际输出
onResume 和 onLaunch 没有被调用,而 onMessage 在应用程序处于前台时被正确调用。
如果通知负载未命中 "click_action": "FLUTTER_NOTIFICATION_CLICK" 那么 onclick 和 onresume 功能将不会 called.But onmessage 功能将完美运行。 在终端
中尝试使用此有效载荷DATA='{"notification": {"body": "this is a body","title": "this is a title"}, "priority": "high", "data": {"click_action": "FLUTTER_NOTIFICATION_CLICK", "route":"booking", "bookingId":"319", "userType":"host", "status": "done"}, "to": "<FCM TOKEN>"}'
curl https://fcm.googleapis.com/fcm/send -H "Content-Type:application/json" -X POST -d "$DATA" -H "Authorization: key=<FCM SERVER KEY>"
FLUTTER_NOTIFICATION_CLICK 必须调用 onresume 和 onmessage
Complete guide for notification handling:
In payload = [
notification: {
title: _title //Title of the notification
data: {
message: msg,
click_action: 'FLUTTER_NOTIFICATION_CLICK'
// Any message that you want to send in notification
screen: 'chat'
}
}
]
//Inside app you configure as:
FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
handleMessaging() {
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
// showNotification(
// message['notification']['title'], message['notification']['body']);
print("onMessage: $message");
print('Data is: '+ message['data']['screen'].toString());
},
onLaunch: (Map<String, dynamic> msg) {
print("Called onLaunch");
print(msg);
print('Data is: '+ msg['data']['screen'].toString());
if(msg['data']['screen'].toString() == 'chat app') {
return //Navigate to chat app
}
return null;
},
onResume: (Map<String, dynamic> msg) {
//(App in background)
// From Notification bar when user click notification we get this event.
// on this event navigate to a particular page.
print(msg);
// // Assuming you will create classes to handle JSON data. :)
// Notification ns = Notification(title: msg['title'], body: msg['body']);
return null;
},
onMessage: (Map<String, dynamic> msg) {
// (App in foreground)
// on this event add new message in notification collection and hightlight the count on bell icon.
// Add notificaion add in local storage and show in a list.
// updataNotification(msg);
print(msg);
},
);
}