Nero
Nero

Reputation: 1577

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

Upvotes: 4

Views: 6407

Answers (3)

Mohammad Kurjieh
Mohammad Kurjieh

Reputation: 1153

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.

Upvotes: 1

blaneyneil
blaneyneil

Reputation: 3232

You're thinking about it from the wrong angle. Firebase messaging is always going to trigger your onMessage, it's up to you how you handle that internally.

I'd suggest using a state manager - Mobx, ChangeNotifier, a file, whatever - to keep track of what screen the user is on. When the user enters your chat screen, write that to your state manager (also "un-write" that upon your dispose() method if necessary).

Then, in the method that handles onMessage, check for the user's current screen. If they're on the chat screen then exit, if not, execute the overlay.

Upvotes: 0

Mukul
Mukul

Reputation: 1155

If your onMessage does not have code to handle the notifications, then they shouldn't appear.

If You want to convert your onMessage notification, in a overlay or In App Notification then it would be better.

I was also facing the same Issue for showing the notification, and found a workaround for that, We can show the In App Notifications in our App

Instead of showing the notification code in your onMessage method, you can show a overlay, like instagram shows on the top when a notification arrives,

And to append the message to direct chat room, I used some backend help.

  onMessage: (Map<String, dynamic> message) async {
    showOverlayNote(message);
  },

And now you can show the overlay as

  showOverlayNote(message){
    //your code here for showing the overlay
  }

You Can get a Detail on how to use overlay for In App Notificaions from this link

This guy have explained the use of this In App Notifications better, So Please see the above link.

Upvotes: 0

Related Questions