flutter - Flutter - 如何对通知进行分组
问题描述
我正在使用 Flutter firebase 消息接收来自后端的通知,如何对 android https://developer.android.com/training/notify-user/group等通知进行分组?
解决方案
要在 Flutter 中对通知进行分组,您可以使用flutter_local_notifications。这个包有很多功能,其中之一是([Android] 群组通知)。并这样做。
- 首先,您应该设置软件包配置。
- 在您设置包配置的同一位置创建 GroupChannel。
AndroidNotificationChannelGroup channelGroup = AndroidNotificationChannelGroup('com.my.app.alert1', 'mychannel1');
await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.createNotificationChannelGroup(channelGroup);
- 创建一种方法来检查是否有多个活动通知。
void groupNotifications() async {
List<ActiveNotification>? activeNotifications = await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.getActiveNotifications();
if (activeNotifications != null && activeNotifications.length > 0) {
List<String> lines = activeNotifications.map((e) => e.title.toString()).toList();
InboxStyleInformation inboxStyleInformation = InboxStyleInformation(
lines,
contentTitle: "${activeNotifications.length - 1} Updates",
summaryText: "${activeNotifications.length - 1} Updates",
);
AndroidNotificationDetails groupNotificationDetails = AndroidNotificationDetails(
channel.id,
channel.name,
channel.description,
styleInformation: inboxStyleInformation,
setAsGroupSummary: true,
groupKey: channel.groupId,
// onlyAlertOnce: true,
);
NotificationDetails groupNotificationDetailsPlatformSpefics = NotificationDetails(android: groupNotificationDetails);
await flutterLocalNotificationsPlugin.show(0, '', '', groupNotificationDetailsPlatformSpefics);
}
}
注意:每次收到通知时都应该调用 groupNotifications()
推荐阅读
- java - 将 Json 数组映射到 Java 模型
- php - Facebook 即时游戏玩家签名验证失败
- php - WP上下拉菜单+图像滑块的PHP问题
- python - 使 tkinter.Entry 只读但不灰显
- python-3.x - 如何在 python(3.x) 中使用数据透视表来解决这个问题
- laravel-passport - 成功生成令牌,但在使用时仍然未经授权
- java - UML - 未在字段中定义的 Java 类之间的关系
- python - 如何使用按钮在 Tkinter 中创建测验
- c# - 如何在 WPF 上运行异步任务而不挂 gui
- javascript - 删除字符串上的特定多个结束字符