简体   繁体   中英

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.

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: (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. 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. 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,

the click_action only put in data, on in notification anymore. 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']}' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM