Skip to content

Commit

Permalink
pigeon: Add binding for getNotificationChannels
Browse files Browse the repository at this point in the history
  • Loading branch information
rajveermalviya committed Oct 4, 2024
1 parent 061821c commit 84dba03
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 0 deletions.
21 changes: 21 additions & 0 deletions android/app/src/main/kotlin/com/zulip/flutter/Notifications.g.kt
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,12 @@ private object NotificationsPigeonCodec : StandardMessageCodec() {

/** Generated interface from Pigeon that represents a handler of messages from Flutter. */
interface AndroidNotificationHostApi {
/**
* Corresponds to `androidx.core.app.NotificationManagerCompat.getNotificationChannelsCompat`.
*
* See: https://developer.android.com/reference/kotlin/androidx/core/app/NotificationManagerCompat#getNotificationChannelsCompat()
*/
fun getNotificationChannels(): List<NotificationChannel>
/**
* Corresponds to `androidx.core.app.NotificationManagerCompat.createNotificationChannel`.
*
Expand Down Expand Up @@ -469,6 +475,21 @@ interface AndroidNotificationHostApi {
@JvmOverloads
fun setUp(binaryMessenger: BinaryMessenger, api: AndroidNotificationHostApi?, messageChannelSuffix: String = "") {
val separatedMessageChannelSuffix = if (messageChannelSuffix.isNotEmpty()) ".$messageChannelSuffix" else ""
run {
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.zulip.AndroidNotificationHostApi.getNotificationChannels$separatedMessageChannelSuffix", codec)
if (api != null) {
channel.setMessageHandler { _, reply ->
val wrapped: List<Any?> = try {
listOf(api.getNotificationChannels())
} catch (exception: Throwable) {
wrapError(exception)
}
reply.reply(wrapped)
}
} else {
channel.setMessageHandler(null)
}
}
run {
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.zulip.AndroidNotificationHostApi.createNotificationChannel$separatedMessageChannelSuffix", codec)
if (api != null) {
Expand Down
11 changes: 11 additions & 0 deletions android/app/src/main/kotlin/com/zulip/flutter/ZulipPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ fun toPigeonPerson(person: androidx.core.app.Person): Person {

private class AndroidNotificationHost(val context: Context)
: AndroidNotificationHostApi {
override fun getNotificationChannels(): List<NotificationChannel> {
return NotificationManagerCompat.from(context)
.notificationChannelsCompat
.map { NotificationChannel(
it.id,
it.importance.toLong(),
it.name?.toString(),
it.shouldShowLights()
) }
}

override fun createNotificationChannel(channel: NotificationChannel) {
val notificationChannel = NotificationChannelCompat
.Builder(channel.id, channel.importance.toInt()).apply {
Expand Down
30 changes: 30 additions & 0 deletions lib/host/android_notifications.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,36 @@ class AndroidNotificationHostApi {

final String __pigeon_messageChannelSuffix;

/// Corresponds to `androidx.core.app.NotificationManagerCompat.getNotificationChannelsCompat`.
///
/// See: https://developer.android.com/reference/kotlin/androidx/core/app/NotificationManagerCompat#getNotificationChannelsCompat()
Future<List<NotificationChannel?>> getNotificationChannels() async {
final String __pigeon_channelName = 'dev.flutter.pigeon.zulip.AndroidNotificationHostApi.getNotificationChannels$__pigeon_messageChannelSuffix';
final BasicMessageChannel<Object?> __pigeon_channel = BasicMessageChannel<Object?>(
__pigeon_channelName,
pigeonChannelCodec,
binaryMessenger: __pigeon_binaryMessenger,
);
final List<Object?>? __pigeon_replyList =
await __pigeon_channel.send(null) as List<Object?>?;
if (__pigeon_replyList == null) {
throw _createConnectionError(__pigeon_channelName);
} else if (__pigeon_replyList.length > 1) {
throw PlatformException(
code: __pigeon_replyList[0]! as String,
message: __pigeon_replyList[1] as String?,
details: __pigeon_replyList[2],
);
} else if (__pigeon_replyList[0] == null) {
throw PlatformException(
code: 'null-error',
message: 'Host platform returned null value for non-null return value.',
);
} else {
return (__pigeon_replyList[0] as List<Object?>?)!.cast<NotificationChannel?>();
}
}

/// Corresponds to `androidx.core.app.NotificationManagerCompat.createNotificationChannel`.
///
/// See: https://developer.android.com/reference/androidx/core/app/NotificationManagerCompat#createNotificationChannel(androidx.core.app.NotificationChannelCompat)
Expand Down
5 changes: 5 additions & 0 deletions pigeon/notifications.dart
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ class StatusBarNotification {

@HostApi()
abstract class AndroidNotificationHostApi {
/// Corresponds to `androidx.core.app.NotificationManagerCompat.getNotificationChannelsCompat`.
///
/// See: https://developer.android.com/reference/kotlin/androidx/core/app/NotificationManagerCompat#getNotificationChannelsCompat()
List<NotificationChannel> getNotificationChannels();

/// Corresponds to `androidx.core.app.NotificationManagerCompat.createNotificationChannel`.
///
/// See: https://developer.android.com/reference/androidx/core/app/NotificationManagerCompat#createNotificationChannel(androidx.core.app.NotificationChannelCompat)
Expand Down
6 changes: 6 additions & 0 deletions test/model/binding.dart
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,12 @@ class FakeAndroidNotificationHostApi implements AndroidNotificationHostApi {
}
List<NotificationChannel> _createdChannels = [];

@override
Future<List<NotificationChannel?>> getNotificationChannels() {
// TODO: implement getNotificationChannels
throw UnimplementedError();
}

@override
Future<void> createNotificationChannel(NotificationChannel channel) async {
_createdChannels.add(channel);
Expand Down

0 comments on commit 84dba03

Please sign in to comment.