简体   繁体   中英

Hide flutter local notification on foreground

I want to hide notification when app is in foreground but IOS is showing notification even in foreground. Normally FCM notification are not shown when app is in foreground. Here is my code

await flutterLocalNotificationsPlugin
        .resolvePlatformSpecificImplementation<
            AndroidFlutterLocalNotificationsPlugin>()
        ?.createNotificationChannel(channel);

    AndroidInitializationSettings androidInitializationSettings =
        AndroidInitializationSettings('@mipmap/ic_launcher');
    IOSInitializationSettings iosInitializationSettings =
        IOSInitializationSettings();

    InitializationSettings initializationSettings = InitializationSettings(
        android: androidInitializationSettings, iOS: iosInitializationSettings);

    flutterLocalNotificationsPlugin.initialize(initializationSettings,
        onSelectNotification: onSelectNotification);

    final bool? result = await flutterLocalNotificationsPlugin
        .resolvePlatformSpecificImplementation<
            IOSFlutterLocalNotificationsPlugin>()
        ?.requestPermissions(
          alert: true,
          badge: true,
          sound: true,
        );

I have a solution for iOS.

When you initialize IOSInitializationSettings you have those params:

presentAlert : Display an alert when the notification is triggered while app is in the foreground. presentSound : Play a sound when the notification is triggered while app is in the foreground. presentBadge : Apply the badge value when the notification is triggered while app is in the foreground.

So if you set those params to false , notifications should be shown on background and hidden on foreground.

Just need to add this line

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

If you don't want to see the notifications in foreground, then add your code to only the onBackgroundMessage handler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM