Zenn
💡

【Flutter】push通知がフォアグランド時に通知バーに表示されない?!

2025/04/09に公開

push通知の通知バー

業務で開発中、アプリがフォアグランド時に(アプリを開いている状態)通知バー表示されてない件。

androidとiOS両方で発覚。
firebaseからのメッセージは来ておりなんでかな?と。

下記公式にかいてあったので備忘録でかきこ。
https://firebase.flutter.dev/docs/messaging/notifications/#foreground-notifications

iOSの設定で確認すべきこと。

他の記事等呼んで諸々設定したのに通知バーに通知が来なかった。
よくよく調べると下記をmain.dartにいれてなかったのが問題。


await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
  alert: true,
  badge: true, 
  sound: true,
);

iosはこの設定だけでアプリがフォアグラウンド中でも、iOSの通知バナー(上からひょこっと出てくるやつ)や通知センターに自動で表示される。

android側

問題はandroid側。
記事を読みむと、下記を確認。

Android configuration#
Android handles incoming notifications differently based on a few different factors:

If the application is in the background or terminated, the assigned "Notification Channel" is used to determine how the notification is displayed.
If the application is currently in the foreground, a visible notification is not presented.

翻訳:

  • アプリがバックグラウンドまたは終了状態にある場合、通知は「通知チャンネル」に従って表示されます。アプリがフォアグラウンド中の場合、通知は自動では表示されません。

要は、「Androidでは、アプリがフォアグラウンド中なら通知バーには出ないよ。表示したければ自分で表示してね。」って意味。

なので、ローカル通知で実装してねってことでした。

FCMNotificationListenerをリッスンしてフォアグランド時にの処理にてローカル通知つかってねってことでした。

https://pub.dev/packages/flutter_local_notifications#firebase-messaging-integration

通知こなくてハマりマスタ。

以上!!!

Discussion

ログインするとコメントできます