Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Firebase how to control notification not to show when the app is in foreground

I had a flutter app with a chat room feature. Firebase messaging is used to push remote notification. I want to hide the notification popup if the app is at foreground and it happens to be at the exact chat room where that coming notification belongs to.

This is a common behavior when you're chatting with someone. The message appends to chat room directly instead of popup.

My question is how to interfere the remote notification on receiving? I used onMessage callback on notification received event. But it always popups no matter the app is at foreground or background or termiated.

In firebase React Native version, it has an onNotification callback which does exactly what I want. But there is no equivalence for flutter. My research leads me to send Data and Notification type of message. In such a way, I need to keep app status synchronized with my backend server (Am I right?).

Any help or suggestion is welcome. Thank you.

P.S. firebase_messaging 8.0.0-dev.10

like image 432
Nero Avatar asked Dec 05 '25 06:12

Nero


1 Answers

Update:

As mentioned by op, the problem was fixed when using to the latest stable version (7.0.3). Then the cause is a bug in the new dev release.

Related GitHub issue, Link


Original Answer:

Judging by the issues that you described, the problem that you are running into might be because you are on the dev channel.

Did you try to use the latest stable? (7.0.3 at the time of writing). It might be a bug in the dev channel.

If the problem persists, try sending a data message instead of the notification message. This will stop firebase from sending a notification on the device. But be aware that you will have to display a notification manually. This way you will have control over the display of the notifications based on the screen and wouldn't have to keep the state in sync with the server as you were mentioning.

But this might not work on iOS since according to the docs, onMessage() will be triggered when the app becomes in foreground.

Check the link above for more details on how the notification message and the data message gets handled on both Android and iOS.

like image 73
Mohammad Kurjieh Avatar answered Dec 07 '25 22:12

Mohammad Kurjieh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!