Firebase_messaging在flutter中无法调用onResume和onLaunch回调函数

5

我想做什么

当用户在通知栏中点击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。


你确定你按照 https://pub.dev/packages/firebase_messaging 的步骤进行了吗? 只需要完成步骤1-4就可以了。 - Qonvex620
是的,我再次检查了一遍。遵循完全相同的步骤。 onMessage回调函数工作得非常好,但在onResume和onLaunch方面则不尽人意,因为它也不会生成任何异常。 - Haroon khan
请问您能否移除Navigator.push吗? - Qonvex620
我认为问题出在导航上。 - Qonvex620
我也已经移除了导航器,只留下了消息,但仍然没有结果。 - Haroon khan
2个回答

9
如果通知负载中缺少 "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>"

FLUTTER_NOTIFICATION_CLICK必须在onResume和onMessage中调用


如何在Firebase控制台中完成?是添加键数据值{"click_action": "FLUTTER_NOTIFICATION_CLICK"}还是直接添加"click_action": "FLUTTER_NOTIFICATION_CLICK"? - LOG_TAG
应该将其添加为其他选项的自定义数据,如下所示:"click_action": "FLUTTER_NOTIFICATION_CLICK"。 - LIONEL VSV
谢谢您针对 iOS 的操作,已经删除文档中提到的第五步,现在它可以正常运行了。 - LOG_TAG

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);
   },
 );
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接