Skip to content

Commit 5bdd0aa

Browse files
pigeon: Add binding for deleteNotificationChannel
1 parent d6626a8 commit 5bdd0aa

File tree

5 files changed

+83
-1
lines changed

5 files changed

+83
-1
lines changed

android/app/src/main/kotlin/com/zulip/flutter/Notifications.g.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,12 @@ interface AndroidNotificationHostApi {
414414
* See: https://developer.android.com/reference/kotlin/androidx/core/app/NotificationManagerCompat#getNotificationChannelsCompat()
415415
*/
416416
fun getNotificationChannels(): List<NotificationChannel>
417+
/**
418+
* Corresponds to `androidx.core.app.NotificationManagerCompat.deleteNotificationChannel`
419+
*
420+
* See: https://developer.android.com/reference/kotlin/androidx/core/app/NotificationManagerCompat#deleteNotificationChannel(java.lang.String)
421+
*/
422+
fun deleteNotificationChannel(channelId: String)
417423
/**
418424
* Corresponds to `android.app.NotificationManager.notify`,
419425
* combined with `androidx.core.app.NotificationCompat.Builder`.
@@ -508,6 +514,24 @@ interface AndroidNotificationHostApi {
508514
channel.setMessageHandler(null)
509515
}
510516
}
517+
run {
518+
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.zulip.AndroidNotificationHostApi.deleteNotificationChannel$separatedMessageChannelSuffix", codec)
519+
if (api != null) {
520+
channel.setMessageHandler { message, reply ->
521+
val args = message as List<Any?>
522+
val channelIdArg = args[0] as String
523+
val wrapped: List<Any?> = try {
524+
api.deleteNotificationChannel(channelIdArg)
525+
listOf(null)
526+
} catch (exception: Throwable) {
527+
wrapError(exception)
528+
}
529+
reply.reply(wrapped)
530+
}
531+
} else {
532+
channel.setMessageHandler(null)
533+
}
534+
}
511535
run {
512536
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.zulip.AndroidNotificationHostApi.notify$separatedMessageChannelSuffix", codec)
513537
if (api != null) {

android/app/src/main/kotlin/com/zulip/flutter/ZulipPlugin.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ private class AndroidNotificationHost(val context: Context)
6464
) }
6565
}
6666

67+
override fun deleteNotificationChannel(channelId: String) {
68+
NotificationManagerCompat.from(context).deleteNotificationChannel(channelId)
69+
}
70+
6771
@SuppressLint(
6872
// If permission is missing, `notify` will throw an exception.
6973
// Which hopefully will propagate to Dart, and then it's up to Dart code to handle it.

lib/host/android_notifications.g.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,31 @@ class AndroidNotificationHostApi {
430430
}
431431
}
432432

433+
/// Corresponds to `androidx.core.app.NotificationManagerCompat.deleteNotificationChannel`
434+
///
435+
/// See: https://developer.android.com/reference/kotlin/androidx/core/app/NotificationManagerCompat#deleteNotificationChannel(java.lang.String)
436+
Future<void> deleteNotificationChannel(String channelId) async {
437+
final String __pigeon_channelName = 'dev.flutter.pigeon.zulip.AndroidNotificationHostApi.deleteNotificationChannel$__pigeon_messageChannelSuffix';
438+
final BasicMessageChannel<Object?> __pigeon_channel = BasicMessageChannel<Object?>(
439+
__pigeon_channelName,
440+
pigeonChannelCodec,
441+
binaryMessenger: __pigeon_binaryMessenger,
442+
);
443+
final List<Object?>? __pigeon_replyList =
444+
await __pigeon_channel.send(<Object?>[channelId]) as List<Object?>?;
445+
if (__pigeon_replyList == null) {
446+
throw _createConnectionError(__pigeon_channelName);
447+
} else if (__pigeon_replyList.length > 1) {
448+
throw PlatformException(
449+
code: __pigeon_replyList[0]! as String,
450+
message: __pigeon_replyList[1] as String?,
451+
details: __pigeon_replyList[2],
452+
);
453+
} else {
454+
return;
455+
}
456+
}
457+
433458
/// Corresponds to `android.app.NotificationManager.notify`,
434459
/// combined with `androidx.core.app.NotificationCompat.Builder`.
435460
///

pigeon/notifications.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,11 @@ abstract class AndroidNotificationHostApi {
165165
/// See: https://developer.android.com/reference/kotlin/androidx/core/app/NotificationManagerCompat#getNotificationChannelsCompat()
166166
List<NotificationChannel> getNotificationChannels();
167167

168+
/// Corresponds to `androidx.core.app.NotificationManagerCompat.deleteNotificationChannel`
169+
///
170+
/// See: https://developer.android.com/reference/kotlin/androidx/core/app/NotificationManagerCompat#deleteNotificationChannel(java.lang.String)
171+
void deleteNotificationChannel(String channelId);
172+
168173
/// Corresponds to `android.app.NotificationManager.notify`,
169174
/// combined with `androidx.core.app.NotificationCompat.Builder`.
170175
///

test/model/binding.dart

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,12 @@ class FakeFlutterLocalNotificationsPlugin extends Fake implements FlutterLocalNo
543543
}
544544

545545
class FakeAndroidNotificationHostApi implements AndroidNotificationHostApi {
546+
/// Lists currently active channels, result is aggregated from calls made to
547+
/// [createNotificationChannel] and [deleteNotificationChannel],
548+
/// order of creation is preserved.
549+
Iterable<NotificationChannel> get activeChannels => _activeChannels.values;
550+
final Map<String, NotificationChannel> _activeChannels = {};
551+
546552
/// Consume the log of calls made to [createNotificationChannel].
547553
///
548554
/// This returns a list of the arguments to all calls made
@@ -557,11 +563,29 @@ class FakeAndroidNotificationHostApi implements AndroidNotificationHostApi {
557563
@override
558564
Future<void> createNotificationChannel(NotificationChannel channel) async {
559565
_createdChannels.add(channel);
566+
_activeChannels[channel.id] = channel;
560567
}
561568

562569
@override
563570
Future<List<NotificationChannel?>> getNotificationChannels() async {
564-
return _createdChannels.toList(growable: false);
571+
return _activeChannels.values.toList(growable: false);
572+
}
573+
574+
/// Consume the log of calls made to [deleteNotificationChannel].
575+
///
576+
/// This returns a list of the arguments to all calls made
577+
/// to [deleteNotificationChannel] since the last call to this method.
578+
List<String> takeDeletedChannels() {
579+
final result = _deletedChannels;
580+
_deletedChannels = [];
581+
return result;
582+
}
583+
List<String> _deletedChannels = [];
584+
585+
@override
586+
Future<void> deleteNotificationChannel(String channelId) async {
587+
_deletedChannels.add(channelId);
588+
_activeChannels.remove(channelId);
565589
}
566590

567591
/// Consume the log of calls made to [notify].

0 commit comments

Comments
 (0)