Flutter中如何移除或设置应用图标上的本地通知角标
Flutter应用通知角标移除与自定义方案(基于flutter_local_notifications)
由于flutter_local_notifications本身未提供统一的跨平台角标操作API,需针对Android和iOS分别处理:
Android平台
Android的通知角标依赖Launcher厂商实现,以下是核心处理方式:
1. 移除角标
- 针对Android O+通知渠道:创建渠道时设置
showBadge: false,关闭该渠道下所有通知的角标显示:
const AndroidNotificationChannel channel = AndroidNotificationChannel( 'your_channel_id', '通知渠道名称', description: '渠道描述', importance: Importance.max, showBadge: false, ); await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.createNotificationChannel(channel);
- 单条通知隐藏角标:发送通知时指定
badgeIconType: BadgeIconType.None:
await flutterLocalNotificationsPlugin.show( 0, '通知标题', '通知内容', NotificationDetails( android: AndroidNotificationDetails( channel.id, channel.name, channelDescription: channel.description, badgeIconType: BadgeIconType.None, ), ), );
- 清除已有角标:调用
cancelAll()清除所有通知,角标会自动消失;或针对指定通知ID调用cancel(notificationId)。
2. 自定义角标数字
Android无统一系统API,需借助第三方插件适配多厂商,推荐使用flutter_app_badger:
- 添加依赖:在
pubspec.yaml中加入flutter_app_badger: ^1.5.0 - 操作角标:
// 设置角标数字 await FlutterAppBadger.updateBadgeCount(3); // 移除角标 await FlutterAppBadger.removeBadge();
iOS平台
iOS角标支持统一,可直接通过flutter_local_notifications配置:
1. 设置角标数字
发送通知时在IOSNotificationDetails中指定badge值:
await flutterLocalNotificationsPlugin.show( 0, '通知标题', '通知内容', NotificationDetails( iOS: IOSNotificationDetails( badge: 4, // 角标显示数字4 ), ), );
2. 移除角标
设置badge: 0即可清除角标,或调用cancelAll()清除所有通知:
await flutterLocalNotificationsPlugin.show( 0, '通知标题', '通知内容', NotificationDetails( iOS: IOSNotificationDetails( badge: 0, ), ), );
注意事项
- iOS需在
Info.plist中配置UIBackgroundModes包含remote-notification(远程通知场景),并确保应用已获取通知权限。 - Android部分厂商(如小米、华为)需在应用系统设置中手动开启「桌面图标角标」权限。
内容的提问来源于stack exchange,提问作者Alper Efe Sahin
相关产品推荐
相关产品推荐

