简体   繁体   English

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

[英]Flutter - How to work with FCM onResume and onLaunch methods?

My issue seems similar to this firebase_messaging onResume and onLaunch not working however I don't think the solution work for me since I'm already trying to access the fields in data property.我的问题似乎类似于此firebase_messaging onResume and onLaunch not working但是我认为该解决方案对我不起作用,因为我已经在尝试访问数据属性中的字段。

I'm currently displaying a push notification to users when the app is running and that part is working fine.当应用程序正在运行并且该部分工作正常时,我目前正在向用户显示推送通知。 However I also want to show a notification when the app is in the background and when the user clicks on it, they should be greeted with an alert message.但是我也想在应用程序在后台时显示通知,当用户点击它时,他们应该收到一条警告消息。

In the onResume method if I do this, it works and when I open the notification I see the message printed on the console and also the Alert message在 onResume 方法中,如果我这样做,它会起作用,当我打开通知时,我会看到打印在控制台上的消息以及警报消息

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


However, if I try to access the data property in the title, I do see the message printed on the console but I don't see any Alert now但是,如果我尝试访问标题中的数据属性,我会看到控制台上打印的消息,但我现在看不到任何警报

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

The same piece of code works when the app is running in the onMessage property however for both onLaunch and onResume I see the above described behavior.当应用程序在onMessage属性中运行时,同一段代码有效,但是对于onLaunchonResume ,我看到了上述行为。 Below are the logs from the console以下是来自控制台的日志

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

You have to add new key value click_action: 'FLUTTER_NOTIFICATION_CLICK' in notification payload.您必须在通知负载中添加新的键值click_action: 'FLUTTER_NOTIFICATION_CLICK' like following喜欢关注

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

Also add following code on manifest file inside activity tag还在活动标签内的清单文件中添加以下代码

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

in firebaseMessaging version ^4,在 firebaseMessaging 版本 ^4 中,

the click_action only put in data, on in notification anymore. click_action 仅放入数据,不再通知。 If you still put it in notification, you can not go another page you want,如果你仍然把它放在通知中,你就不能转到你想要的另一个页面,

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

}

One way to work is: in the payload put into data for example:一种工作方式是:在有效负载中放入数据,例如:

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

Than use ${message['data']['message']}' instead of ${message['notification']['body']}' .比使用${message['data']['message']}'代替${message['notification']['body']}'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter FCM:如何在电话通过 FCM 终止时注销用户 - Flutter FCM: How to logout a user when phone is terminated through FCM 如何为非活跃用户获取 FCM 令牌? [扑] - How to the get FCM token for inactive users? [Flutter] Flutter Firebase push onLaunch 在应用程序刷新时调用多次 - Flutter Firebase push onLaunch called multiple times on app refresh flutter FCM 订阅主题似乎不起作用 - flutter FCM subscribe to a topic doesn't seem to work 如何在后台 FCM/Flutter 中保留 AndroidNotificationDetails - How keep AndroidNotificationDetails in background FCM/Flutter 后台 FCM 消息适用于模拟器,但不适用于设备。 Flutter - “Dart_LookupLibrary:找不到库‘package:my_fcm.dart’。” - Background FCM messages work on emulator but not on device. Flutter - "Dart_LookupLibrary: library 'package:my_fcm.dart' not found." flutter:fcm android 推送通知在发布模式下不起作用,在调试模式下起作用 - flutter: fcm android push notifications doesn't work in release mode and work in debug mode 如何从 fcm static 方法 flutter 访问上下文 - How to access context from fcm static method flutter Flutter - 如何像 whatsapp 一样在通知栏外显示 FCM 通知 - Flutter - How to display FCM notification outside notification bar like whatsapp flutter中的FCM自定义通知图标 - FCM Custom Notification Icon in flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM