如何在 Flutter & Firebase 的前台打印(message.data)



我正在添加云消息功能到我的安卓应用程序,我无法收到消息。前景数据被截断。

示例为

FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print('Got a message whilst in the foreground!');
print('Message data: ${message.data}');
if (message.notification != null) {
print('Message also contained a notification: ${message.notification}');
}
});

应该打印第一行Got a message whilst in the foreground!...,但是我得到

D/FLTFireMsgReceiver( XXXX): broadcast received for message
W/com.XXXXXX.XXXXXX( XXXX): Accessing hidden method Landroid/os/WorkSource;->add(I)Z (greylist,test-api, reflection, allowed)
W/com.XXXXXX.XXXXXX( XXXX): Accessing hidden method Landroid/os/WorkSource;->add(ILjava/lang/String;)Z (greylist,test-api, reflection, allowed)
W/com.XXXXXX.XXXXXX( XXXX): Accessing hidden method Landroid/os/WorkSource;->get(I)I (greylist, reflection, allowed)
W/com.XXXXXX.XXXXXX( XXXX): Accessing hidden method Landroid/os/WorkSource;->getName(I)Ljava/lang/String; (greylist, reflection, allowed)

我错过了什么吗?

从这个问题来看,这似乎是firebase_messaging最新版本的错误,当前的解决方法是在您收听消息之前添加一行来获取令牌。

更新你的代码如下:

await FirebaseMessaging.instance.getToken(); // Add this line
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print('Got a message whilst in the foreground!');
print('Message data: ${message.data}');
if (message.notification != null) {
print('Message also contained a notification: ${message.notification}');
}
});
FirebaseMessaging.onMessage.listen((message) async {
if (message.notification != null) {
print(
'Message contained a notification, with the following:nTitle: ${message.notification?.title}nBody: ${message.notification?.body}');
}
return;
}).onData((data) {
print('Got a DATA message whilst in the FOREGROUND!');
print('data is: ${data.data}');
});

基本上你需要使用onData的onMessage或前景或背景。

相关内容

  • 没有找到相关文章

最新更新