Skip to content

Commit 2af661c

Browse files
notif: Add setup for notification channel migrations
1 parent dcb3b36 commit 2af661c

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

lib/notifications/display.dart

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,27 @@ class NotificationChannelManager {
5252
// settings for the channel -- like "override Do Not Disturb", or "use
5353
// a different sound", or "don't pop on screen" -- their changes get
5454
// reset. So this has to be done sparingly.
55-
//
56-
// If we do this, we should also look for any channel with the old
57-
// channel ID and delete it. See zulip-mobile's `createNotificationChannel`
58-
// in android/app/src/main/java/com/zulipmobile/notifications/NotificationChannelManager.kt .
5955
static Future<void> _ensureChannel() async {
56+
// See if our current-version channel already exists; delete any obsolete
57+
// previous channels.
58+
var found = false;
59+
final channels = await _androidHost.getNotificationChannels();
60+
for (final channel in channels) {
61+
assert(channel != null); // TODO(flutter#97848)
62+
if (channel!.id == kChannelId) {
63+
found = true;
64+
} else {
65+
await _androidHost.deleteNotificationChannel(channel.id);
66+
}
67+
}
68+
69+
if (found) {
70+
// The channel already exists; nothing to do.
71+
return;
72+
}
73+
74+
// The channel doesn't exist. Create it.
75+
6076
await _androidHost.createNotificationChannel(NotificationChannel(
6177
id: kChannelId,
6278
name: 'Messages', // TODO(i18n)

test/model/binding.dart

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -555,15 +555,13 @@ class FakeAndroidNotificationHostApi implements AndroidNotificationHostApi {
555555
List<NotificationChannel> _createdChannels = [];
556556

557557
@override
558-
Future<List<NotificationChannel?>> getNotificationChannels() {
559-
// TODO: implement getNotificationChannels
560-
throw UnimplementedError();
558+
Future<List<NotificationChannel?>> getNotificationChannels() async {
559+
return _createdChannels.toList(growable: false);
561560
}
562561

563562
@override
564-
Future<void> deleteNotificationChannel(String channelId) {
565-
// TODO: implement deleteNotificationChannel
566-
throw UnimplementedError();
563+
Future<void> deleteNotificationChannel(String channelId) async {
564+
_createdChannels.removeWhere((e) => e.id == channelId);
567565
}
568566

569567
@override

0 commit comments

Comments
 (0)