skip to Main Content

why my firebase not showing notification in Foreground/on application open but printing all those inside the func.

I have this on 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);
  });

on debug console

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

2

Answers


  1. You should use package like https://pub.dev/packages/flutter_local_notifications or something else to show your notification on screen. Here is a simple example of flutter_local_notifications on your onListen method.

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

    });

    Login or Signup to reply.
  2. A visible notification is not shown by default when an application is in foreground. It is up to the application to handle a message as in most cases it doesn’t make sense to show a notification as a user is already using the application.

    See Foreground and Notification messages for details.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search