因此,在 OP 讨论之后,您可以使用 print() 或 debugPrint() 函数调试 onLaunch。
您可以像这样使用 adb 命令行在终端上获取 logcat 输出
$ adb shell
$ logcat -e "flutter" -v color
如果您有多个设备,您可以使用-s 参数来选择您的设备。
-e 仅用于过滤内部带有颤振词的日志消息
-vcolor 是有一个格式化的颜色输出
由于 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}}
你得到的是 onLaunch 和 onResume
{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 我颤抖:onLaunch 类型:
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:
high, foo: bar, google.ttl: 2419200, from: 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
富:酒吧
我通过adb 获得了我的printDebug 函数:
$ 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}
onResume 和 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}
它们是相同的,所以我建议在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']);
}