Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

debug the onLaunch callback in firebase_messaging in Flutter

I am using firebase_messaging in my flutter application, and I want to be able to debug what happens when the onLaunch callback fires.

The problem is that it fires when a notification is received and the app is terminated.

There has to be a way to debug it right?

like image 631
Ayush Shekhar Avatar asked Jan 25 '19 14:01

Ayush Shekhar


Video Answer


1 Answers

So following OP discussion you could debug the onLaunch with print() or debugPrint() function.

You can use the adb command line to get logcat output on terminal like this

$ adb shell 
$ logcat -e "flutter" -v color  

if you have more than one device you could use the -s parameter in order to choose your device.

-e is for filter only log message that has a flutter word inside

-v color is to have a formatted color output

As data message are not supported by Android plugin you could send a notification message in order to have the onLaunch called providing also this data field:

"data": {"click_action": "FLUTTER_NOTIFICATION_CLICK", "id": "1", "status": "done"}

You can send a message like that

{
 "to" : "<your device token>",
 "collapse_key" : "type_a",
 "priority" : "high",
 "notification" : {
     "body" : "Test notification body",
     "title": "Test notification title",
     "sound": "default"
 },
 "data": {"click_action": "FLUTTER_NOTIFICATION_CLICK", "id": "1", "status": "done", "foo":"bar"}
}

The problem is that you get different Map message JSON:

onMessage you get

{notification: {title: Custom sound alert.mp3, body: Test Notification body for custom sound 25/01/2019}, data: {status: done, id: 1, foo: bar, click_action: FLUTTER_NOTIFICATION_CLICK}}

Instead in onLaunch and onResume you get

{collapse_key: com.example.flutterapptestfcmmessaging, google.original_priority: high, google.sent_time: 1548447425689, google.delivered_priority: high, foo: bar, google.ttl: 2419200, from: 945032663190, id: 1, click_action: FLUTTER_NOTIFICATION_CLICK, google.message_id: 0:15484474256938..., status: done}

1-25 21:14:43.802 3445 3491 I flutter : onLaunch type: CastMap<dynamic, dynamic, String, dynamic> 01-25 21:17:11.568 3789 3838 I flutter : onLaunch 01-25 21:17:11.571 3789 3838 I flutter : --->>>> onLaunch {collapse_key: com.example.flutterapptestfcmmessaging, google.original_priority: high, google.sent_time: 1548447425689, google.delivered_priority: high, foo: bar, google.ttl: 2419200, from: 945032663190, id: 1, click_action: FLUTTER_NOTIFICATION_CLICK, google.message_id: 0:15484474256938..., status: done} 01-25 21:17:11.573 3789 3838 I flutter : onLaunch type: CastMap<dynamic, dynamic, String, dynamic> 01-25 21:17:11.574 3789 3838 I flutter : onLaunch foo: bar

I get my printDebug function with adb:

$ logcat -e "onLaunch" -v color   

So in onMessage you can get the foo field like that

print("onMessage foo: ${message['data']['foo']}");

and in onLaunch you can get it like that:

debugPrint("onLaunch foo: " + message['foo']);

UDATE: iOS device

The above debugging session is for an Android device.

On a iOS device in order to get the console output of the device you could use Apple App Configurator 2 or the Console application (from Utilities folder inside your Applications folder):

on onMessage you will receive:

{status: done, google.c.a.e: 1, id: 1, aps: {alert: {title: Test Notification, body: Test Notification at 26/01/2019}}, gcm.message_id: 0:15485106,,,, foo: bar, click_action: FLUTTER_NOTIFICATION_CLICK}

and on onResume and onLaunch:

{status: done, google.c.a.e: 1, id: 1, aps: {alert: {title: Test Notification, body: Test Notification at 26/01/2019}}, gcm.message_id: 0:15485109..., foo: bar, click_action: FLUTTER_NOTIFICATION_CLICK}

They are the same, so I suggest to check the platform before getting your custom data in onMessage.

For that you could use dart.io library Platform class:

if (Platform.isAndroid) {
  print("onMessage Android foo: ${message['data']['foo']}");
} else if (Platform.isIOS) {
  debugPrint("onMessage iOS foo: " + message['foo']);
}
like image 175
shadowsheep Avatar answered Oct 05 '22 10:10

shadowsheep