Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter + FCM: onBackgroundMessage vs onMessageOpenedApp

Recently I've been working on connecting flutter with FCM. While reading firebase docs I red:

Handle background messages by registering a onBackgroundMessage handler.

,but later reading flutterfire docs I've found:

The firebase-messaging package provides two ways to handle this interaction:

  1. (...)
  2. onMessageOpenedApp: A Stream which posts a RemoteMessage when the application is opened from a background state.

I understood, that first function is to handle notification when app is in background (whenever user interacted with it or not) and 2nd one is to handle tapping on notification when app is in background. Correct me if that's wrong.

But if I'm right about it, then wouldn't using both functions cause redundancy? onBackgroundMessage runs independently (as separate isolate), so if user taps on notification when app is closed, would both functions be fired?

Right now I use flutter_local_notifications to save notifications that were not interacted with (and app was in background/terminated) and check for them when app is started/resumed by user. If I was correct in above conclusion as well, then should I check inside onMessageOpenedApp if clicked notification was already handled by onBackgroundMessage (and thus saved in local storage)?

Please, help me clarify that matter.
Thank you.

like image 592
Belzebub1410 Avatar asked Aug 03 '26 07:08

Belzebub1410


1 Answers

If a message was handled by onBackgroundMessage, it won't cause a notification to appear (unless you manually do so, e.g. through flutter_local_notifications), so it won't appear in onMessageOpenedApp.

onMessageOpenedApp is called only when app is in background (but not terminated!) and user taps on notification that was delivered previously (so, since when app is in foreground all messages are sent to onMessage, it had to be delivered when app was in background).

like image 134
mfkw1 Avatar answered Aug 04 '26 20:08

mfkw1