在 Flutter 中的 firebase_messaging 中调试 onLaunch 回调

Ayu*_*har 7 flutter firebase-cloud-messaging

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

问题是它会在收到通知并且应用程序终止时触发。

必须有一种方法来调试它吗?

sha*_*eep 11

因此,在 OP 讨论之后,您可以调试onLaunchwithprint()debugPrint()函数。

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

$ adb shell 
$ logcat -e "flutter" -v color  
Run Code Online (Sandbox Code Playgroud)

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

-e 仅用于过滤内部包含颤振词的日志消息

-v 颜色是有格式化的颜色输出

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

"data": {"click_action": "FLUTTER_NOTIFICATION_CLICK", "id": "1", "status": "done"}
Run Code Online (Sandbox Code Playgroud)

你可以发送这样的消息

{
 "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"}
}
Run Code Online (Sandbox Code Playgroud)

问题是你得到不同的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}}
Run Code Online (Sandbox Code Playgroud)

相反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}
Run Code Online (Sandbox Code Playgroud)

1-25 21:14:43.802 3445 3491 I 颤振:onLaunch 类型:CastMap<动态,动态,字符串,动态> 01-25 21:17:11.568 3789 3838 I 颤振:onLaunch 01-2715 138 137我扑:--->>>> onLaunch {collapse_key: com.example.flutterapptestfcmmessaging, google.original_priority: high, google.sent_time: 1548447425689, google.delivered_priority: high, foo: bar, google.ttl: 20, 2419 945032663190, id: 1, click_action: FLUTTER_NOTIFICATION_CLICK, google.message_id: 0:15484474256938 ..., status: done} 01-25 21:17:11.573 3789 < 383 3789 < 3789<L>动态映射,动态映射:动态映射> 01-25 21:17:11.574 3789 3838 我扑:onLaunch foo:bar

我得到我的printDebug功能adb

$ logcat -e "onLaunch" -v color   
Run Code Online (Sandbox Code Playgroud)

所以在onMessage你可以得到这样的 foo 字段

print("onMessage foo: ${message['data']['foo']}");
Run Code Online (Sandbox Code Playgroud)

onLaunch你可以得到这样的:

debugPrint("onLaunch foo: " + message['foo']);
Run Code Online (Sandbox Code Playgroud)

UDATE: iOS 设备

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

在 iOS 设备上,为了获得设备的控制台输出,您可以使用Apple App Configurator 2Console应用程序(来自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}
Run Code Online (Sandbox Code Playgroud)

并在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}
Run Code Online (Sandbox Code Playgroud)

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

为此,您可以使用dart.ioPlatform类:

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


归档时间:

查看次数:

5415 次

最近记录:

5 年,1 月 前