Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter firebase messaging v9.0.0 - Not firing events

I have an app that was using firebase messaging 7.0.3 and all worked perfectly. But when I migrated to firebase messaging 9.0.0, the push notifications are not being handled.

I know that the app is correctly linked to firebase and cloud messaging because in background I see the push notification comming, the problem is when I click that notification the app don't handle this event. Also, when the app is in foreground, the event of receiving the notification is not being triggered.

Specifically, the functions FirebaseMessaging.onMessage and FirebaseMessaging.onMessageOpenedApp are not working. My code is:

FirebaseMessaging.onMessage.listen((RemoteMessage message) {
  print("notification: message");
}); 
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
  print("notification: resume");
});

The prints are never called, and if I put some more code inside isn't executed too.

I also call FirebaseMessaging.getToken and I can get the token, plus when the app is in background I get the push notification, so is not a link problem with firebase. In the logs, when I receive the push I can see a message saying: Broadcast received for message. So I assumed that the push is arriving in all the occasions, and I am doing something wrong in the code.

I tested all cases in Android and iOS physical devices.

Someone know why this occurs?

like image 622
Santi Avatar asked Mar 10 '21 09:03

Santi


People also ask

How do I use Firebase Cloud Messaging with flutterfire?

Before using Firebase Cloud Messaging, you must first have ensured you have initialized FlutterFire. To create a new Messaging instance, call the instance getter on FirebaseMessaging: Messaging currently only supports usage with the default Firebase App instance. FCM via APNs does not work on iOS Simulators.

How to send notifications using flutter Cloud Messaging?

Now execute flutter run on the terminal, and copy the token. Then navigate to the Firebase console -> Cloud Messaging and click on Send your first message. In the notification composer page, add the notification title, text: And then click on Send Test Message , which will open the following modal:

How to send notifications to users in Firebase?

To be able to send messages, you can retrieve the registration token of each user or you can subscribe a group of users to different topics. After doing that you can either use Firebase Cloud Functions or your own server to be able to send the notifications. FCM has two different messages, notification message and data message.

How do I use FCM in Firebase?

Using FCM, you can notify a client app that new email or other data is available to sync. You can send notification messages to drive user re-engagement and retention. For use cases such as instant messaging, a message can transfer a payload of up to 4 KB to a client app. 1. Make sure to initialize Firebase


1 Answers

I came across this exact same issue after migrating and upgrading to 9.0.0.

There is a minor issue with 9.0.0 plugin. The fix can be found here: https://github.com/FirebaseExtended/flutterfire/issues/4949

To cut a long story short, if you navigate to the definition of the factory RemoteMessage.fromMap() Using Cmd+Click or Ctrl+Click while hovering over the RemoteMessage class with the mouse), in the return statement, change contentAvailable: map['contentAvailable'], to contentAvailable: map['contentAvailable']??false.

Notifications are now working for me again now. This should work for you until the plugin is fixed.

like image 119
AdrianM Avatar answered Nov 15 '22 10:11

AdrianM