在Flutter中调试firebase_messaging中的onLaunch回调函数

8

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

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

一定有一种方法可以进行调试,对吧?


当您点击通知并打开应用程序时,应调用onLauch。 请问您的需求可以详细说明吗? - shadowsheep
当然,你可以使用日志(print(.))来实现这一点,但是如果应用程序在后台运行,onLaunch也会被调用,因此您可以将调试器附加到进程上。 - shadowsheep
根据此页面上给出的表格 https://pub.dartlang.org/packages/firebase_messaging ,当应用程序在后台运行时,将调用onResume方法,请问如何调用onLaunch方法或如何附加调试器? - Ayush Shekhar
另外,我真的不知道如何通过print(.)消息查看日志的方法。如果您能告诉我如何做到这一点,我将非常感激。 - Ayush Shekhar
所以,我用一个实时例子更深入地了解了你的情况,并写了一个回答,它可以总结我们的讨论和使用日志进行调试的方法,而不需要调试附加程序,并解释了为什么你在onLaunch中的消息映射中遇到问题。再见! - shadowsheep
显示剩余2条评论
1个回答

12

所以根据讨论,您可以使用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}}

相比之下,在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 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}

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库中的Platform类:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接