Search code examples
firebaseflutterfirebase-cloud-messagingflutter-local-notification

FCM handles IOS notification when app is foreground which is I do not want to


I use fcm and local_notifications for handling push notifications on my Flutter project.

Sending notifications using this payload:

{
  token: body.token,
  notification = {
      title: body.data.title,
      body: body.data.body,
  },
  data: {
      data: JSON.stringify(body.data),
  }
}

So when notification comes

on app terminated: Notification shows up perfectly for both app(ios - android)

on app background: Notification shows up perfectly for both app(ios - android)

on app foreground:

android app: notification comes like a data notification, fcm is not alerting anything as I want it.

ios app: when the notification comes, fcm is displaying an alert which is I do not want to. And also local_notifications is displaying at the same time.

My problem is fcm handles foreground notifications on ios app. When it works that way I can't disable the notification that should not appear on foreground.

pubspec.yaml:

firebase_messaging: ^10.0.4
flutter_local_notifications: ^8.1.1+1

firebase foreground notification options:

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

When I remove firebase foreground notification options or set false all parameters, local_notifications can't display any notifications too.

local_notifications foreground notification options:

instance.resolvePlatformSpecificImplementation<IOSFlutterLocalNotificationsPlugin>()?.requestPermissions(
          alert: true,
          badge: true,
          sound: true,
        );

Thanks.


Solution

  • I found the solution: It was too obvious :D

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

    alert parameter should be false if you do not want FCM to handle foreground alers.