我在我的Flutter应用程序中使用firebase_messaging
,并希望能够调试onLaunch
回调触发时发生的情况。
问题是当接收到通知并且应用程序被终止时,它会触发。
一定有一种方法可以进行调试,对吧?
我在我的Flutter应用程序中使用firebase_messaging
,并希望能够调试onLaunch
回调触发时发生的情况。
问题是当接收到通知并且应用程序被终止时,它会触发。
一定有一种方法可以进行调试,对吧?
所以根据讨论,您可以使用print()
或debugPrint()
函数调试onLaunch
。
您可以使用adb命令行在终端上获取logcat输出,例如:
$ adb shell
$ logcat -e "flutter" -v color
如果您有多个设备,可以使用-s
参数来选择您的设备。
-e
是用于仅筛选具有“flutter”单词的日志消息。
-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}}
相比之下,在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 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
我使用adb
函数获取printDebug
:
$ logcat -e "onLaunch" -v color
因此在onMessage
中,您可以像这样获取foo字段
print("onMessage foo: ${message['data']['foo']}");
在onLaunch
中,您可以像这样获取它:
debugPrint("onLaunch foo: " + message['foo']);
更新:iOS设备
上述调试会话是针对安卓设备的。
在iOS设备上,您可以使用Apple App Configurator 2或控制台
应用程序(位于应用程序
文件夹中的实用工具
文件夹内)来获取设备的控制台输出:
在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
库中的Platform
类:
if (Platform.isAndroid) {
print("onMessage Android foo: ${message['data']['foo']}");
} else if (Platform.isIOS) {
debugPrint("onMessage iOS foo: " + message['foo']);
}
onLauch
。 请问您的需求可以详细说明吗? - shadowsheepprint(.)
)来实现这一点,但是如果应用程序在后台运行,onLaunch
也会被调用,因此您可以将调试器附加到进程上。 - shadowsheep