为什么我无法呈现消息通知,尽管收到了消息广播

问题描述 投票:0回答:2

这里,我收到消息,但数据为空,尽管我已经设置了数据

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());
}

here is my Messaging [1]:https://i.stack.imgur.com/TWjtP.png [模拟器不显示通知][1]

android ios flutter dart firebase-cloud-messaging
2个回答
0
投票

正如我在评论部分所说,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
中的许多属性,如此处

所述

0
投票

查看我对类似问题的回答。

我使用由

overlay_support: ^2.1.0
包提供支持的应用内通知小部件解决了这个问题。

您可以 在应用程序上显示通知

我希望这有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.