Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase_messaging onResume and onLaunch callbacks are not called in flutter

What I want to do

I want to navigate to specific screen when the user taps on fcm notification in the notification tray.

My Code

I've followed the exact same instructions as mentioned on pub.dev for version 6.0.9.

Here is my implementation:

_fcm.configure(
        onMessage: (Map<String, dynamic> message) async {
          print('@onMessage: $message');
        },
        onResume: (Map<String, dynamic> message) async {
          print('@onResume: $message');
          Navigator.push(context,
              MaterialPageRoute(builder: (context) => NotificationsScreen()));
        },
        onLaunch: (Map<String, dynamic> message) async {
          print('@onLaunch: $message');
          Navigator.push(context,
              MaterialPageRoute(builder: (context) => NotificationsScreen()));
        });

Plugin version: firebase_messaging: ^6.0.9

I've also added these configurations:

app/build.gradle:

implementation 'com.google.firebase:firebase-messaging:20.1.0'

project/build.gradle:

classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:'1.3.50'"
classpath 'com.google.gms:google-services:4.3.3'

Expected output

When the users taps notification in the backgroud, onResume or onLaunch callback is called and he's redirected to the mentioned screen.

Actual output

onResume and onLaunch are not called while onMessage is called properly when the app is in foreground.

like image 395
Haroon khan Avatar asked Dec 31 '22 07:12

Haroon khan


1 Answers

If the notification payload misses "click_action": "FLUTTER_NOTIFICATION_CLICK" then onclick and onresume functions will not be called.But onmessage function will work perfectly. Try with this payload in terminal

DATA='{"notification": {"body": "this is a body","title": "this is a title"}, "priority": "high", "data": {"click_action": "FLUTTER_NOTIFICATION_CLICK",  "route":"booking", "bookingId":"319", "userType":"host", "status": "done"}, "to": "<FCM TOKEN>"}'
curl https://fcm.googleapis.com/fcm/send -H "Content-Type:application/json" -X POST -d "$DATA" -H "Authorization: key=<FCM SERVER KEY>"

FLUTTER_NOTIFICATION_CLICK is must to call onresume and onmessage

like image 104
LIONEL VSV Avatar answered Jun 01 '23 17:06

LIONEL VSV