【问题标题】:FirebaseMessaging.onMessage.listen not showing notificationFirebaseMessaging.onMessage.listen 不显示通知
【发布时间】:2023-02-20 21:09:13
【问题描述】:

为什么我的 firebase 没有在前台/应用程序打开时显示通知,而是在 func.

我在 main() 里面的 main.dart 上有这个

 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

【问题讨论】:

    标签: flutter firebase


    【解决方案1】:

    您应该使用 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);
    

    });

    【讨论】:

      猜你喜欢
      • 2021-08-21
      • 1970-01-01
      • 2020-07-13
      • 2018-04-11
      • 1970-01-01
      • 1970-01-01
      • 2013-03-31
      • 1970-01-01
      相关资源
      最近更新 更多