Skip to content

Commit 78863cb

Browse files
notif: Add setup for notification channel migrations
1 parent 7ffe44c commit 78863cb

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