Skip to content

Commit d6626a8

Browse files
pigeon: Add binding for getNotificationChannels
1 parent 701763d commit d6626a8

File tree

5 files changed

+72
-0
lines changed

5 files changed

+72
-0
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,12 @@ interface AndroidNotificationHostApi {
408408
* See: https://developer.android.com/reference/androidx/core/app/NotificationManagerCompat#createNotificationChannel(androidx.core.app.NotificationChannelCompat)
409409
*/
410410
fun createNotificationChannel(channel: NotificationChannel)
411+
/**
412+
* Corresponds to `androidx.core.app.NotificationManagerCompat.getNotificationChannelsCompat`.
413+
*
414+
* See: https://developer.android.com/reference/kotlin/androidx/core/app/NotificationManagerCompat#getNotificationChannelsCompat()
415+
*/
416+
fun getNotificationChannels(): List<NotificationChannel>
411417
/**
412418
* Corresponds to `android.app.NotificationManager.notify`,
413419
* combined with `androidx.core.app.NotificationCompat.Builder`.
@@ -487,6 +493,21 @@ interface AndroidNotificationHostApi {
487493
channel.setMessageHandler(null)
488494
}
489495
}
496+
run {
497+
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.zulip.AndroidNotificationHostApi.getNotificationChannels$separatedMessageChannelSuffix", codec)
498+
if (api != null) {
499+
channel.setMessageHandler { _, reply ->
500+
val wrapped: List<Any?> = try {
501+
listOf(api.getNotificationChannels())
502+
} catch (exception: Throwable) {
503+
wrapError(exception)
504+
}
505+
reply.reply(wrapped)
506+
}
507+
} else {
508+
channel.setMessageHandler(null)
509+
}
510+
}
490511
run {
491512
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.zulip.AndroidNotificationHostApi.notify$separatedMessageChannelSuffix", codec)
492513
if (api != null) {

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ private class AndroidNotificationHost(val context: Context)
5353
NotificationManagerCompat.from(context).createNotificationChannel(notificationChannel)
5454
}
5555

56+
override fun getNotificationChannels(): List<NotificationChannel> {
57+
return NotificationManagerCompat.from(context)
58+
.notificationChannelsCompat
59+
.map { NotificationChannel(
60+
id = it.id,
61+
importance = it.importance.toLong(),
62+
name = it.name?.toString(),
63+
lightsEnabled = it.shouldShowLights()
64+
) }
65+
}
66+
5667
@SuppressLint(
5768
// If permission is missing, `notify` will throw an exception.
5869
// 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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,36 @@ class AndroidNotificationHostApi {
400400
}
401401
}
402402

403+
/// Corresponds to `androidx.core.app.NotificationManagerCompat.getNotificationChannelsCompat`.
404+
///
405+
/// See: https://developer.android.com/reference/kotlin/androidx/core/app/NotificationManagerCompat#getNotificationChannelsCompat()
406+
Future<List<NotificationChannel?>> getNotificationChannels() async {
407+
final String __pigeon_channelName = 'dev.flutter.pigeon.zulip.AndroidNotificationHostApi.getNotificationChannels$__pigeon_messageChannelSuffix';
408+
final BasicMessageChannel<Object?> __pigeon_channel = BasicMessageChannel<Object?>(
409+
__pigeon_channelName,
410+
pigeonChannelCodec,
411+
binaryMessenger: __pigeon_binaryMessenger,
412+
);
413+
final List<Object?>? __pigeon_replyList =
414+
await __pigeon_channel.send(null) as List<Object?>?;
415+
if (__pigeon_replyList == null) {
416+
throw _createConnectionError(__pigeon_channelName);
417+
} else if (__pigeon_replyList.length > 1) {
418+
throw PlatformException(
419+
code: __pigeon_replyList[0]! as String,
420+
message: __pigeon_replyList[1] as String?,
421+
details: __pigeon_replyList[2],
422+
);
423+
} else if (__pigeon_replyList[0] == null) {
424+
throw PlatformException(
425+
code: 'null-error',
426+
message: 'Host platform returned null value for non-null return value.',
427+
);
428+
} else {
429+
return (__pigeon_replyList[0] as List<Object?>?)!.cast<NotificationChannel?>();
430+
}
431+
}
432+
403433
/// Corresponds to `android.app.NotificationManager.notify`,
404434
/// combined with `androidx.core.app.NotificationCompat.Builder`.
405435
///

pigeon/notifications.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ abstract class AndroidNotificationHostApi {
160160
/// See: https://developer.android.com/reference/androidx/core/app/NotificationManagerCompat#createNotificationChannel(androidx.core.app.NotificationChannelCompat)
161161
void createNotificationChannel(NotificationChannel channel);
162162

163+
/// Corresponds to `androidx.core.app.NotificationManagerCompat.getNotificationChannelsCompat`.
164+
///
165+
/// See: https://developer.android.com/reference/kotlin/androidx/core/app/NotificationManagerCompat#getNotificationChannelsCompat()
166+
List<NotificationChannel> getNotificationChannels();
167+
163168
/// Corresponds to `android.app.NotificationManager.notify`,
164169
/// combined with `androidx.core.app.NotificationCompat.Builder`.
165170
///

test/model/binding.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,11 @@ class FakeAndroidNotificationHostApi implements AndroidNotificationHostApi {
559559
_createdChannels.add(channel);
560560
}
561561

562+
@override
563+
Future<List<NotificationChannel?>> getNotificationChannels() async {
564+
return _createdChannels.toList(growable: false);
565+
}
566+
562567
/// Consume the log of calls made to [notify].
563568
///
564569
/// This returns a list of the arguments to all calls made

0 commit comments

Comments
 (0)