Flutter-如何使用FCM onResume和onLaunch方法?

时间:2019-11-30 21:06:43

标签: firebase flutter firebase-cloud-messaging

我的问题似乎与此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();
        }


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

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}}

3 个答案:

答案 0 :(得分:0)

您必须在通知有效负载中添加新的键值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>

答案 1 :(得分:0)

在FirebaseMessaging版本^ 4中,

click_action仅将数据放入通知中。如果仍将其放在通知中,则无法转到所需的其他页面,

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

}

答案 2 :(得分:0)

一种工作方式是: 在有效载荷中放入数据,例如:

data.frame(ind = ages, val = pmin(pmax(ages, min_HB), max_HB))

比起使用"data": { "click_action": "FLUTTER_NOTIFICATION_CLICK", "id": "1", "status": "done", "message": "My Message", "title": "Meu Title" } 而不是${message['data']['message']}'