Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
323 views
in Technique[技术] by (71.8m points)

ios - 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...