Home > Enterprise >  why i Can't render the message notification although broadcast received for message
why i Can't render the message notification although broadcast received for message

Time:09-28

Here , i received message but the data is null although i have set up the data

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'

Here is my code

  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 [It don't display the notification, the simulator][1]

CodePudding user response:

As I said in the comment section, firebase notifications are little bit delayed (experienced not mentioned anywhere) So you may not able to see it. But it will pop in a few minutes (again in my case).

One more reason you didn't get notification, as mentioned in doc

Messages may have a RemoteMessage.Notification instance if they are received while the application is in the foreground, otherwise they will be automatically posted to the notification tray.

You are printing the RemoteMessage.data but you are sending notification from firebase console. RemoteMessage have a nested class RemoteMessage.RemoteNotification. You should have use this class to print the data sent from firebase console like this

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

You can still use many properties from RemoteNotification as mentioned here

  • Related