我想做什么
当用户在通知托盘中点击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回调并将其重定向到上述屏幕。
实际输出
当应用程序在前台时,正确调用onMessage时,不会调用onResume和onLaunch。
答案 0 :(得分:8)
如果通知有效负载缺少“ click_action”:“ FLUTTER_NOTIFICATION_CLICK”,则 onclick和onresume函数不会被调用,但是onmessage函数会完美运行。 在终端中尝试使用此有效载荷
fetchTopStreamsSuccess
必须调用FLUTTER_NOTIFICATION_CLICK来调用onresume和onmessage
答案 1 :(得分:0)
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);
},
);
}