如何在 Flutter & Firebase 的前台打印 (message.data)
How to print(message.data) on foreground at Flutter & Firebase
我正在为我的 Android APP 添加 Cloud Messaging 功能,但无法在前台截取 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)
我是不是漏掉了什么?
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 或前台或后台。
从这个 issue 看来,它似乎是最新版本 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}');
}
});
我正在为我的 Android APP 添加 Cloud Messaging 功能,但无法在前台截取 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)
我是不是漏掉了什么?
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 或前台或后台。
从这个 issue 看来,它似乎是最新版本 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}');
}
});