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

2,320

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.

Share:
2,320
Nero
Author by

Nero

make a living from coding

Updated on December 26, 2022

Comments

  • Nero
    Nero over 1 year

    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

  • Nero
    Nero over 3 years
    I empty onMessage callback body but still I got popup in IOS. As for android, it's very different from ios since seemingly it requires to convert coming notification to a local notification. Therefore, if we leave onMessage empty, notification won't show up in android.
  • Nero
    Nero over 3 years
    By the way, I noticed that you probably use version 7, but mine is 8.0.0-dev.10. I assume it has slight change your solution work for 7 but not 8. Maybe I would try it later.
  • Nero
    Nero over 3 years
    Yes, understand that onMessage is always triggered. But can I choose not to show the notification popup / heads up notification?
  • Nero
    Nero over 3 years
    thanks. i already try data and notification type of messages. But that don’t work neither.
  • Mohammad Kurjieh
    Mohammad Kurjieh over 3 years
    Did you try to use the latest stable version?
  • Nero
    Nero over 3 years
    I confirmed that my issue described above seems to be a bug in dev channel. I tried the 7.0.3 and it works as expected. when my app running at foreground, no heads-up notification show up and this is a desired behavior. By the way, onMessage is triggered only when app at foreground. onResume and onLaunch both work as intended.
  • Nero
    Nero over 3 years
    I marked this as answer. Could you please highlight the cause in your answer? Thanks.
  • Mohammad Kurjieh
    Mohammad Kurjieh over 3 years
    I updated the answer, I would suggest you to open a new issue at GitHub for the devs to fix. If you do please let me know to link it in the answer. Glad I was able to help you :-)
  • Nero
    Nero over 3 years
    a related github issue is created here github.com/FirebaseExtended/flutterfire/issues/4619
  • Mohammad Kurjieh
    Mohammad Kurjieh over 3 years
    Thanks I added the GitHub Link