这里,我收到消息,但数据为空,尽管我已经设置了数据
D/FLTFireMsgReceiver(26011): broadcast received for message
I/flutter (26011): Got a message whilst in the foreground!
I/flutter (26011): Message data: {}
I/flutter (26011): Message also contained a notification: Instance of 'RemoteNotification'
这是我的代码
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FirebaseMessaging messaging = FirebaseMessaging.instance;
NotificationSettings settings = await messaging.requestPermission(
alert: true,
announcement: false,
badge: true,
carPlay: false,
criticalAlert: false,
provisional: false,
sound: true,
);
print('User granted permission: ${settings.authorizationStatus}');
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.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
runApp(MyApp());
}
正如我在评论部分所说,Firebase 通知有点延迟(没有在任何地方提到过的经历),所以你可能看不到它。但几分钟后它就会弹出(在我的例子中也是如此)。
您没有收到通知的另一个原因,如doc
中所述消息可能有一个 RemoteMessage.Notification 实例,如果它们是 当应用程序位于前台时收到,否则它们 将自动发布到通知托盘。
您正在打印
RemoteMessage.data
,但您正在从 Firebase 控制台发送通知。 RemoteMessage
有一个嵌套类RemoteMessage.RemoteNotification
。您应该使用此类来打印从 firebase 控制台发送的数据,如下所示
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print('Got a message whilst in the foreground!');
if (message.notification != null) {
print('Notification Title: ${message.notification.title}');
print('Notification Body: ${message.notification.body}');
}
});
您仍然可以使用
RemoteNotification
中的许多属性,如此处所述