4

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
  • 1,555
  • 1
  • 13
  • 28

3 Answers3

1

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.

Mohammad Kurjieh
  • 1,043
  • 7
  • 16
  • thanks. i already try data and notification type of messages. But that don’t work neither. – Nero Jan 11 '21 at 01:14
  • Did you try to use the latest stable version? – Mohammad Kurjieh Jan 11 '21 at 01:14
  • 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 Jan 11 '21 at 10:35
  • I marked this as answer. Could you please highlight the cause in your answer? Thanks. – Nero Jan 11 '21 at 10:37
  • 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 :-) – Mohammad Kurjieh Jan 11 '21 at 10:55
  • a related github issue is created here https://github.com/FirebaseExtended/flutterfire/issues/4619 – Nero Jan 11 '21 at 11:21
  • Thanks I added the GitHub Link – Mohammad Kurjieh Jan 11 '21 at 12:12
0

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.

Mukul
  • 1,020
  • 6
  • 12
  • 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 Jan 02 '21 at 01:45
  • 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 Jan 02 '21 at 01:47
0

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.

blaneyneil
  • 3,122
  • 13
  • 14
  • Yes, understand that onMessage is always triggered. But can I choose not to show the notification popup / heads up notification? – Nero Jan 09 '21 at 07:47