FirebaseMessaging.onMessage.listen未显示通知

r7xajy2e  于 2023-02-25  发布在  其他
关注(0)|答案(2)|浏览(146)

为什么我的firebase没有在前台/应用程序打开时显示通知,而是打印了func中的所有通知。
我在main上有这个.dart inside main()

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}');
    }
    FlutterAppBadger.updateBadgeCount(1);
  });

在调试控制台上

D/FLTFireMsgReceiver( 4139): broadcast received for message
I/flutter ( 4139): Got a message whilst in the foreground!
I/flutter ( 4139): Message data: {}
I/flutter ( 4139): Message also contained a notification: Instance of 'RemoteNotification'
2
W/FirebaseMessaging( 4139): Unable to log event: analytics library is missing
gxwragnw

gxwragnw1#

你应该使用像https://pub.dev/packages/flutter_local_notifications这样的包或者其他的东西来在屏幕上显示你的通知。下面是一个关于onListen方法的flutter_local_notifications的简单例子。

FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print('Got a message whilst in the foreground!');
print('Message data: ${message.data}');

if (message.notification != null) {
  flutterLocalNotificationsPlugin.show(
      notification.hashCode,
      notification.title,
      notification.body,
      NotificationDetails(
          android: AndroidNotificationDetails(
            channel.id,
            channel.name,
          ),
          iOS: const IOSNotificationDetails()),
    );
  print('Message also contained a notification: ${message.notification}');
}
FlutterAppBadger.updateBadgeCount(1);

});

qni6mghb

qni6mghb2#

当应用程序在前台时,默认情况下不会显示可见通知。这取决于应用程序来处理消息,因为在大多数情况下,显示通知没有意义,因为用户已经在使用该应用程序。
有关详细信息,请参阅前台和通知消息。

相关问题