Hello I'm using Flutter to build my app and I need to to show an alert whenever a new notification is received.
I've been using firebase_messaging 7.0.3
but I run into an error with onBackgroundMessage
. A quick Google search helped me find out that the error I was getting hasn't been fixed yet. However one of the devs posted an update 20 days ago about a new version of the package which fixed that issue.
The new version removed the old onMessage handlers and introduced new ones.
Now they got new event handlers which return streams, but haven't been able to make them fire by using the
.listen()
function. Whenever I receive a notification a get a this: D/FLTFireMsgReceiver(22032): broadcast received for message
printed in the console but the code in the .listen()
doesn't get executed.
Here is a link to an article on Firebase Flutter that is a guide for using the new version of the package.
Here is my code:
...
FirebaseMessaging.onMessage.listen((event) {
// do something
});
FirebaseMessaging.onMessageOpenedApp.listen((event) {
// do something
});
FirebaseMessaging.onBackgroundMessage((message) {
// do something
return;
}
...
A solution I found to get the events to fire was to always call:
await FirebaseMessaging.instance.getToken();
right after the
await Firebase.initializeApp();
Once I call that, the FirebaseMessaging.onMessage.listen
catches the event as expected.
Solution
TL;DR
Add this Pull Request changes in your ide by editing respective files. Send Data notification with High priority.
FCM Payload:
{
"message": {
"token": "fcm_client_token",
"data": {
"title": "Hello",
"body": "Test Message"
},
"android": {
"priority": "high"
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With