Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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}}
like image 859
Dipanshu Juneja Avatar asked Nov 30 '19 21:11

Dipanshu Juneja


People also ask

How do you send push notifications using FCM and flutter one to another device?

To send a notification, go to Firebase Console → Cloud Messaging and click on Send your first message. Then enter the Title and body field. If you wish to send it to a particular device then click on Send test message and enter the FCM registration token. (Enter the current FCM registration token).

How long FCM server can store the notification message and try to deliver?

The value must be a duration from 0 to 2,419,200 seconds (28 days), and it corresponds to the maximum period of time for which FCM stores and attempts to deliver the message.


1 Answers

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>
like image 99
Parth Rajani Avatar answered Sep 23 '22 02:09

Parth Rajani