Skip to content

Commit 2696bc1

Browse files
notif: Add setup for notification channel migrations
1 parent 6852ab4 commit 2696bc1

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
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: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -528,18 +528,6 @@ class FakeFlutterLocalNotificationsPlugin extends Fake implements FlutterLocalNo
528528
}
529529

530530
class FakeAndroidNotificationHostApi implements AndroidNotificationHostApi {
531-
@override
532-
Future<List<NotificationChannel?>> getNotificationChannels() {
533-
// TODO: implement getNotificationChannels
534-
throw UnimplementedError();
535-
}
536-
537-
@override
538-
Future<void> deleteNotificationChannel(String channelId) {
539-
// TODO: implement deleteNotificationChannel
540-
throw UnimplementedError();
541-
}
542-
543531
/// Consume the log of calls made to [createNotificationChannel].
544532
///
545533
/// This returns a list of the arguments to all calls made
@@ -551,6 +539,16 @@ class FakeAndroidNotificationHostApi implements AndroidNotificationHostApi {
551539
}
552540
List<NotificationChannel> _createdChannels = [];
553541

542+
@override
543+
Future<List<NotificationChannel?>> getNotificationChannels() async {
544+
return _createdChannels.toList(growable: false);
545+
}
546+
547+
@override
548+
Future<void> deleteNotificationChannel(String channelId) async {
549+
_createdChannels.removeWhere((e) => e.id == channelId);
550+
}
551+
554552
@override
555553
Future<void> createNotificationChannel(NotificationChannel channel) async {
556554
_createdChannels.add(channel);

0 commit comments

Comments
 (0)