[英]Hide flutter local notification on foreground
I want to hide notification when app is in foreground but IOS is showing notification even in foreground.我想在应用程序处于前台时隐藏通知,但 IOS 即使在前台也显示通知。 Normally FCM notification are not shown when app is in foreground.
当应用程序处于前台时,通常不会显示 FCM 通知。 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.我有一个适用于 iOS 的解决方案。
When you initialize IOSInitializationSettings
you have those params:当您初始化
IOSInitializationSettings
时,您有这些参数:
presentAlert
: Display an alert when the notification is triggered while app is in the foreground. presentAlert
:当应用程序在前台时触发通知时显示警报。 presentSound
: Play a sound when the notification is triggered while app is in the foreground. presentSound
:当应用程序在前台时触发通知时播放声音。 presentBadge
: Apply the badge value when the notification is triggered while app is in the foreground. presentBadge
:当应用程序在前台时触发通知时应用徽章值。
So if you set those params to false
, notifications should be shown on background and hidden on foreground.因此,如果您将这些参数设置为
false
,则通知应显示在后台并隐藏在前台。
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.如果您不想在前台看到通知,则仅将代码添加到
onBackgroundMessage
处理程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.