Skip to content

Commit 7102382

Browse files
notif: Delete older channels
1 parent e85d6c5 commit 7102382

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

lib/notifications/display.dart

+25-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,31 @@ class NotificationChannelManager {
5353
// in android/app/src/main/java/com/zulipmobile/notifications/NotificationChannelManager.kt .
5454
static Future<void> _ensureChannel() async {
5555
final plugin = ZulipBinding.instance.notifications;
56-
await plugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
57-
?.createNotificationChannel(AndroidNotificationChannel(
56+
final androidPlugin = plugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>();
57+
if (androidPlugin == null) {
58+
// TODO(log)
59+
return;
60+
}
61+
62+
// Delete any older channels.
63+
var found = false;
64+
final channels = await androidPlugin.getNotificationChannels();
65+
if (channels != null) {
66+
for (final channel in channels) {
67+
if (channel.id == kChannelId) {
68+
found = true;
69+
} else {
70+
await androidPlugin.deleteNotificationChannel(channel.id);
71+
}
72+
}
73+
}
74+
if (found) {
75+
// The channel already exists, nothing to do.
76+
return;
77+
}
78+
79+
// The channel doesn't exists, create it.
80+
await androidPlugin.createNotificationChannel(AndroidNotificationChannel(
5881
kChannelId,
5982
'Messages', // TODO(i18n)
6083
importance: Importance.high,

test/model/binding.dart

+12
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,18 @@ class FakeAndroidFlutterLocalNotificationsPlugin extends Fake implements Android
475475
Future<void> createNotificationChannel(AndroidNotificationChannel notificationChannel) async {
476476
_createdChannels.add(notificationChannel);
477477
}
478+
479+
@override
480+
Future<List<AndroidNotificationChannel>?> getNotificationChannels() async {
481+
return _createdChannels;
482+
}
483+
484+
@override
485+
Future<void> deleteNotificationChannel(String channelId) async {
486+
_createdChannels.removeWhere((channel) {
487+
return channel.id == channelId;
488+
});
489+
}
478490
}
479491

480492
class FakeIOSFlutterLocalNotificationsPlugin extends Fake implements IOSFlutterLocalNotificationsPlugin {

0 commit comments

Comments
 (0)