【问题标题】:debug the onLaunch callback in firebase_messaging in Flutter在 Flutter 中调试 firebase_messaging 中的 onLaunch 回调
【发布时间】:2019-06-19 09:30:48
【问题描述】:

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

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

总得有办法调试吧?

【问题讨论】:

  • onLauch 应该在您点击通知并且您的应用正在打开时被调用。您能否详细说明您的需求?
  • 是的。但是在应用程序打开之前,应用程序被终止并且没有调试连接,所以当 onLaunch 被调用时,我怎么知道发生了什么。在我的例子中,应用程序开始执行,正如我所期望的那样,但在那之后,我得到一个 NoSuchMethodError,我需要知道是哪一行导致的。
  • 当然你可以使用日志 (print(.)),但是如果应用程序在后台,onLaunch 也会被调用,这样你就可以将调试器附加到进程中。
  • 根据本页给出的表格,pub.dartlang.org/packages/firebase_messaging onResume 在应用程序处于后台时被调用,你能告诉我 onLaunch 是如何被调用的,或者我怎样才能连接我的调试器。跨度>
  • 另外,我真的不知道如何查看带有 print(.) 消息的日志的方法。如果你能告诉我这是怎么做的,我将不胜感激。

标签: flutter firebase-cloud-messaging


【解决方案1】:

因此,在 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}}

你得到的是 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 我颤抖: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 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}

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']);
}

【讨论】:

    猜你喜欢
    • 2020-06-03
    • 1970-01-01
    • 2019-09-25
    • 2020-09-28
    • 2020-06-30
    • 2020-06-21
    • 2020-03-11
    • 2021-08-04
    • 2023-01-17
    相关资源
    最近更新 更多