在Flutter中调试firebase_messaging中的onLaunch回调

时间:2019-01-25 14:19:20

标签: flutter firebase-cloud-messaging

我在flutter应用程序中使用firebase_messaging,并且希望能够调试onLaunch回调触发时发生的情况。

问题在于,当收到通知并终止应用程序时会触发。

必须要有一种正确的调试方法?

1 个答案:

答案 0 :(得分:1)

因此,在OP讨论之后,您可以使用onLaunchprint()函数调试debugPring()

您可以使用adb命令行在这样的终端上获取logcat输出

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

如果您拥有多个设备,则可以使用-s参数来选择您的设备。

-e仅用于过滤器日志消息,该消息内部带有乱码

-v颜色应具有格式化的颜色输出

由于Android插件不支持数据消息,因此您可以发送notification消息,以便调用onLaunch并同时提供以下data字段:

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

您可以发送类似的消息

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

问题是您得到了不同的Map 消息 JSON:

onMessage得到

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

代替onLaunchonResume会得到

{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我扑动:开启动类型:   CastMap 01-25 21:17:11.568 3789   3838我扑动:onLaunch 01-25 21:17:11.571 3789 3838我扑动:   --- >>>> onLaunch {collapse_key:com.example.flutterapptestfcmmessaging,google.original_priority:   高,google.sent_time:1548447425689,google.delivered_priority:   高,foo:bar,google.ttl:2419200,来自:945032663190,ID:1   click_action:FLUTTER_NOTIFICATION_CLICK,google.message_id:   0:15484474256938 ...,状态:已完成} 01-25 21:17:11.573   3789 3838我扑动:onLaunch类型:CastMap 01-25 21:17:11.574 3789 3838我扑动:onLaunch   foo:bar

我已使用printDebug获得了adb函数:

$ logcat -e "onLaunch" -v color   

因此,在onMessage中,您可以获取foo字段

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

,然后在onLaunch中可以这样获得它:

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

UDATE: iOS设备

上述调试会话适用于Android设备。

在iOS设备上,要获取设备的控制台输出,可以使用Apple App Configurator 2 Console应用程序(从您内部的Utilities文件夹中Applications文件夹):

onMessage上,您将收到:

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

以及onResumeonLaunch上:

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

它们是相同的,所以我建议在onMessage中获取自定义数据之前先检查平台。

为此,您可以使用dart.io library Platform类:

if (Platform.isAndroid) {
  print("onMessage Android foo: ${message['data']['foo']}");
} else if (Platform.isIOS) {
  debugPrint("onMessage iOS foo: " + message['foo']);
}