Har*_*han 4 callback flutter firebase-cloud-messaging
我想做的事
当用户点击通知托盘中的 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()));
});
Run Code Online (Sandbox Code Playgroud)
插件版本: firebase_messaging:^6.0.9
我还添加了这些配置:
应用程序/build.gradle:
implementation 'com.google.firebase:firebase-messaging:20.1.0'
项目/build.gradle:
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:'1.3.50'"
classpath 'com.google.gms:google-services:4.3.3'
Run Code Online (Sandbox Code Playgroud)
预期输出
当用户点击背景中的通知时,将调用 onResume 或 onLaunch 回调并将其重定向到上述屏幕。
实际产量
当应用程序在前台时正确调用 onMessage 时,不会调用 onResume 和 onLaunch。
如果通知负载未命中 "click_action": "FLUTTER_NOTIFICATION_CLICK" 则不会调用 onclick 和 onresume 函数。但 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>"
Run Code Online (Sandbox Code Playgroud)
FLUTTER_NOTIFICATION_CLICK 必须调用 onresume 和 onmessage
| 归档时间: |
|
| 查看次数: |
3598 次 |
| 最近记录: |