Firebase_messaging onResume 和 onLaunch 回调不会在 Flutter 中调用

Posted

技术标签:

【中文标题】Firebase_messaging onResume 和 onLaunch 回调不会在 Flutter 中调用【英文标题】:Firebase_messaging onResume and onLaunch callbacks are not called in flutter 【发布时间】:2020-06-03 18:59:08 【问题描述】:

我想做的事

当用户点击通知托盘中的 fcm 通知时,我想导航到特定屏幕。

我的代码

对于 6.0.9 版,我已按照 pub.dev 中提到的完全相同的说明进行操作。

这是我的实现:

_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 在应用程序处于前台时被正确调用。

【问题讨论】:

您确定您按照pub.dev/packages/firebase_messaging 中的步骤操作了吗?它只是从第 1 步到第 4 步,它已经很好了 是的,我再次检查了一遍。遵循完全相同的步骤。 onMessage 回调可以完美运行,但是 onResume 和 onLaunch 很糟糕,因为它也不会产生任何异常。 你能删除 Navigator.push 吗? 我认为问题出在导航上 我也删除了导航器,只是留下了消息,但仍然没有结果。 【参考方案1】:

如果通知负载错过“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"? 它应该作为附加选项自定义数据添加为“click_action”:“FLUTTER_NOTIFICATION_CLICK” 感谢ios删除文档中提到的第5步,它已经开始工作了【参考方案2】:
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);
   ,
 );

【讨论】:

以上是关于Firebase_messaging onResume 和 onLaunch 回调不会在 Flutter 中调用的主要内容,如果未能解决你的问题,请参考以下文章

firebase_messaging 如何清除通知?

firebase_messaging:^4.0.0+4 不工作

使用颤振 firebase_messaging 插件发送通知声音

firebase_messaging/unknown:Flutter IOS 出现未知错误

使用 firebase_messaging 自定义通知(Android 和 IOS)

无法使用 firebase_messaging 在 iOS 上的 Flutter 应用中订阅主题