Skip to content

Commit 8ff71a2

Browse files
notif: Add setup for notification channel migrations
1 parent ac4c34e commit 8ff71a2

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
@@ -540,15 +540,13 @@ class FakeAndroidNotificationHostApi implements AndroidNotificationHostApi {
540540
List<NotificationChannel> _createdChannels = [];
541541

542542
@override
543-
Future<List<NotificationChannel?>> getNotificationChannels() {
544-
// TODO: implement getNotificationChannels
545-
throw UnimplementedError();
543+
Future<List<NotificationChannel?>> getNotificationChannels() async {
544+
return _createdChannels.toList(growable: false);
546545
}
547546

548547
@override
549-
Future<void> deleteNotificationChannel(String channelId) {
550-
// TODO: implement deleteNotificationChannel
551-
throw UnimplementedError();
548+
Future<void> deleteNotificationChannel(String channelId) async {
549+
_createdChannels.removeWhere((e) => e.id == channelId);
552550
}
553551

554552
@override

0 commit comments

Comments
 (0)