【问题标题】:Flutter - How to work with FCM onResume and onLaunch methods?Flutter - 如何使用 FCM onResume 和 onLaunch 方法?
【发布时间】:2020-03-25 23:10:15
【问题描述】:

我的问题似乎与firebase_messaging onResume and onLaunch not working 类似,但是我认为该解决方案对我不起作用,因为我已经在尝试访问 data 属性中的字段。

当应用程序运行并且该部分工作正常时,我目前正在向用户显示推送通知。但是,我还想在应用程序处于后台时显示通知,并且当用户单击它时,应该会收到一条警报消息。

如果我这样做,在 onResume 方法中它会起作用,当我打开通知时,我会看到控制台上打印的消息以及警报消息

onResume: (Map<String, dynamic> message) async {
    print("onResume: $message");
    Alert(context: context, title: 'Hi User!').show();
}


但是,如果我尝试访问标题中的数据属性,我确实会看到控制台上打印的消息,但我现在看不到任何警报

onResume: (Map<String, dynamic> message) async {
    print("onResume: $message");
    Alert(context: context, title: message['data']['user']['name']).show();
}

当应用程序在onMessage 属性中运行时,相同的代码可以工作,但是对于onLaunchonResume,我看到了上述行为。以下是来自控制台的日志

W/awesome_projec(13005): Accessing hidden method Landroid/os/WorkSource;->add(I)Z (light greylist, reflection) 
W/awesome_projec(13005): Accessing hidden method Landroid/os/WorkSource;->add(ILjava/lang/String;)Z (light greylist, reflection) 
W/awesome_projec(13005): Accessing hidden method Landroid/os/WorkSource;->size()I (light greylist, reflection) 
W/awesome_projec(13005): Accessing hidden method Landroid/os/WorkSource;->get(I)I (light greylist, reflection) 
W/awesome_projec(13005): Accessing hidden method Landroid/os/WorkSource;->getName(I)Ljava/lang/String; (light greylist, reflection) 
E/FlutterFcmService(13005): Fatal: failed to find callback 
W/FirebaseMessaging(13005): Missing Default Notification Channel metadata in AndroidManifest. Default value will be used. 
E/FlutterFcmService(13005): Fatal: failed to find callback 
I/flutter (13005): onResume: {notification: {}, data: {collapse_key: com.example.awesome_project, google.original_priority: high, google.sent_time: 15751462256, google.delivered_priority: high, google.ttl: 2419200, from: 554610817622, location: {"latitude":24.6351,"longitude":70.2764}, user: {"phoneNumber":"1274545332","name":"Bobby94"}, google.message_id: 0:157514622564xxx}}

【问题讨论】:

  • 您只在 iPhone 或 Android 上试用过吗?
  • @dubace 目前我只在 Android 上尝试
  • @DipanshuJuneja 我赞成这个问题。这里同样的问题。你找到解决办法了吗?
  • 有人找到解决办法了吗?
  • 你解决了我想知道的这个问题,因为我有同样的问题吗?

标签: firebase flutter firebase-cloud-messaging


【解决方案1】:

一种工作方式是: 在payload中放入数据例如:

"data": {
  "click_action": "FLUTTER_NOTIFICATION_CLICK",
  "id": "1",
  "status": "done",
  "message": "My Message",
  "title": "Meu Title"
}

而不是使用${message['data']['message']}' 而不是${message['notification']['body']}'

【讨论】:

    【解决方案2】:

    在 firebaseMessaging 版本 ^4 中,

    click_action 只放入数据,不再在通知中。如果你还是把它放在通知里,你就不能去你想要的另一个页面,

    {
    notification: {
        title: 'Title',
        body: 'Body',
    },
    body :
    {
     click_action :FLUTTER_NOTIFICATION_CLICK,
     message: message from firebase
    }
    
    }
    

    【讨论】:

      【解决方案3】:

      您必须在通知负载中添加新的键值click_action: 'FLUTTER_NOTIFICATION_CLICK'。喜欢关注

      {
          notification: {
              title: 'Title',
              body: 'Body',
              click_action: 'FLUTTER_NOTIFICATION_CLICK'
          }
      }
      

      在活动标签内的清单文件中添加以下代码

        <intent-filter>
                 <action android:name="FLUTTER_NOTIFICATION_CLICK" />
                 <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
      

      【讨论】:

      • 我已经有了这个。接收通知没有问题,只是没有显示警报。
      • @DipanshuJuneja 你在不同的设备上检查过这个吗?
      猜你喜欢
      • 2020-04-22
      • 2020-06-03
      • 2019-09-25
      • 2021-01-06
      • 2018-11-30
      • 2020-09-28
      • 2020-11-14
      • 2020-05-05
      • 2021-10-27
      相关资源
      最近更新 更多