Skip to content

Commit 84dba03

Browse files
pigeon: Add binding for getNotificationChannels
1 parent 061821c commit 84dba03

File tree

5 files changed

+73
-0
lines changed

5 files changed

+73
-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
@@ -402,6 +402,12 @@ private object NotificationsPigeonCodec : StandardMessageCodec() {
402402

403403
/** Generated interface from Pigeon that represents a handler of messages from Flutter. */
404404
interface AndroidNotificationHostApi {
405+
/**
406+
* Corresponds to `androidx.core.app.NotificationManagerCompat.getNotificationChannelsCompat`.
407+
*
408+
* See: https://developer.android.com/reference/kotlin/androidx/core/app/NotificationManagerCompat#getNotificationChannelsCompat()
409+
*/
410+
fun getNotificationChannels(): List<NotificationChannel>
405411
/**
406412
* Corresponds to `androidx.core.app.NotificationManagerCompat.createNotificationChannel`.
407413
*
@@ -469,6 +475,21 @@ interface AndroidNotificationHostApi {
469475
@JvmOverloads
470476
fun setUp(binaryMessenger: BinaryMessenger, api: AndroidNotificationHostApi?, messageChannelSuffix: String = "") {
471477
val separatedMessageChannelSuffix = if (messageChannelSuffix.isNotEmpty()) ".$messageChannelSuffix" else ""
478+
run {
479+
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.zulip.AndroidNotificationHostApi.getNotificationChannels$separatedMessageChannelSuffix", codec)
480+
if (api != null) {
481+
channel.setMessageHandler { _, reply ->
482+
val wrapped: List<Any?> = try {
483+
listOf(api.getNotificationChannels())
484+
} catch (exception: Throwable) {
485+
wrapError(exception)
486+
}
487+
reply.reply(wrapped)
488+
}
489+
} else {
490+
channel.setMessageHandler(null)
491+
}
492+
}
472493
run {
473494
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.zulip.AndroidNotificationHostApi.createNotificationChannel$separatedMessageChannelSuffix", codec)
474495
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
@@ -43,6 +43,17 @@ fun toPigeonPerson(person: androidx.core.app.Person): Person {
4343

4444
private class AndroidNotificationHost(val context: Context)
4545
: AndroidNotificationHostApi {
46+
override fun getNotificationChannels(): List<NotificationChannel> {
47+
return NotificationManagerCompat.from(context)
48+
.notificationChannelsCompat
49+
.map { NotificationChannel(
50+
it.id,
51+
it.importance.toLong(),
52+
it.name?.toString(),
53+
it.shouldShowLights()
54+
) }
55+
}
56+
4657
override fun createNotificationChannel(channel: NotificationChannel) {
4758
val notificationChannel = NotificationChannelCompat
4859
.Builder(channel.id, channel.importance.toInt()).apply {

lib/host/android_notifications.g.dart

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,36 @@ class AndroidNotificationHostApi {
375375

376376
final String __pigeon_messageChannelSuffix;
377377

378+
/// Corresponds to `androidx.core.app.NotificationManagerCompat.getNotificationChannelsCompat`.
379+
///
380+
/// See: https://developer.android.com/reference/kotlin/androidx/core/app/NotificationManagerCompat#getNotificationChannelsCompat()
381+
Future<List<NotificationChannel?>> getNotificationChannels() async {
382+
final String __pigeon_channelName = 'dev.flutter.pigeon.zulip.AndroidNotificationHostApi.getNotificationChannels$__pigeon_messageChannelSuffix';
383+
final BasicMessageChannel<Object?> __pigeon_channel = BasicMessageChannel<Object?>(
384+
__pigeon_channelName,
385+
pigeonChannelCodec,
386+
binaryMessenger: __pigeon_binaryMessenger,
387+
);
388+
final List<Object?>? __pigeon_replyList =
389+
await __pigeon_channel.send(null) as List<Object?>?;
390+
if (__pigeon_replyList == null) {
391+
throw _createConnectionError(__pigeon_channelName);
392+
} else if (__pigeon_replyList.length > 1) {
393+
throw PlatformException(
394+
code: __pigeon_replyList[0]! as String,
395+
message: __pigeon_replyList[1] as String?,
396+
details: __pigeon_replyList[2],
397+
);
398+
} else if (__pigeon_replyList[0] == null) {
399+
throw PlatformException(
400+
code: 'null-error',
401+
message: 'Host platform returned null value for non-null return value.',
402+
);
403+
} else {
404+
return (__pigeon_replyList[0] as List<Object?>?)!.cast<NotificationChannel?>();
405+
}
406+
}
407+
378408
/// Corresponds to `androidx.core.app.NotificationManagerCompat.createNotificationChannel`.
379409
///
380410
/// See: https://developer.android.com/reference/androidx/core/app/NotificationManagerCompat#createNotificationChannel(androidx.core.app.NotificationChannelCompat)

pigeon/notifications.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,11 @@ class StatusBarNotification {
155155

156156
@HostApi()
157157
abstract class AndroidNotificationHostApi {
158+
/// Corresponds to `androidx.core.app.NotificationManagerCompat.getNotificationChannelsCompat`.
159+
///
160+
/// See: https://developer.android.com/reference/kotlin/androidx/core/app/NotificationManagerCompat#getNotificationChannelsCompat()
161+
List<NotificationChannel> getNotificationChannels();
162+
158163
/// Corresponds to `androidx.core.app.NotificationManagerCompat.createNotificationChannel`.
159164
///
160165
/// See: https://developer.android.com/reference/androidx/core/app/NotificationManagerCompat#createNotificationChannel(androidx.core.app.NotificationChannelCompat)

test/model/binding.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,12 @@ class FakeAndroidNotificationHostApi implements AndroidNotificationHostApi {
554554
}
555555
List<NotificationChannel> _createdChannels = [];
556556

557+
@override
558+
Future<List<NotificationChannel?>> getNotificationChannels() {
559+
// TODO: implement getNotificationChannels
560+
throw UnimplementedError();
561+
}
562+
557563
@override
558564
Future<void> createNotificationChannel(NotificationChannel channel) async {
559565
_createdChannels.add(channel);

0 commit comments

Comments
 (0)