Skip to content

Commit b96d191

Browse files
notif [nfc]: Replace intentPayload with AndroidIntent.extras
1 parent 48d138f commit b96d191

File tree

6 files changed

+123
-50
lines changed

6 files changed

+123
-50
lines changed

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

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,32 @@ data class NotificationChannel (
8787
}
8888
}
8989

90+
/**
91+
* Corresponds to `android.content.Intent`
92+
*
93+
* See:
94+
* https://developer.android.com/reference/android/content/Intent
95+
*
96+
* Generated class from Pigeon that represents data sent in messages.
97+
*/
98+
data class AndroidIntent (
99+
val extras: Map<String?, String?>
100+
101+
) {
102+
companion object {
103+
@Suppress("LocalVariableName")
104+
fun fromList(__pigeon_list: List<Any?>): AndroidIntent {
105+
val extras = __pigeon_list[0] as Map<String?, String?>
106+
return AndroidIntent(extras)
107+
}
108+
}
109+
fun toList(): List<Any?> {
110+
return listOf(
111+
extras,
112+
)
113+
}
114+
}
115+
90116
/**
91117
* Corresponds to `android.app.PendingIntent`.
92118
*
@@ -96,11 +122,7 @@ data class NotificationChannel (
96122
*/
97123
data class PendingIntent (
98124
val requestCode: Long,
99-
/**
100-
* A value set on an extra on the Intent, and passed to
101-
* the on-notification-opened callback.
102-
*/
103-
val intentPayload: String,
125+
val intent: AndroidIntent,
104126
/**
105127
* A combination of flags from [PendingIntent.flags], and others associated
106128
* with `Intent`; see Android docs for `PendingIntent.getActivity`.
@@ -112,15 +134,15 @@ data class PendingIntent (
112134
@Suppress("LocalVariableName")
113135
fun fromList(__pigeon_list: List<Any?>): PendingIntent {
114136
val requestCode = __pigeon_list[0].let { num -> if (num is Int) num.toLong() else num as Long }
115-
val intentPayload = __pigeon_list[1] as String
137+
val intent = __pigeon_list[1] as AndroidIntent
116138
val flags = __pigeon_list[2].let { num -> if (num is Int) num.toLong() else num as Long }
117-
return PendingIntent(requestCode, intentPayload, flags)
139+
return PendingIntent(requestCode, intent, flags)
118140
}
119141
}
120142
fun toList(): List<Any?> {
121143
return listOf(
122144
requestCode,
123-
intentPayload,
145+
intent,
124146
flags,
125147
)
126148
}
@@ -325,35 +347,40 @@ private object NotificationsPigeonCodec : StandardMessageCodec() {
325347
}
326348
130.toByte() -> {
327349
return (readValue(buffer) as? List<Any?>)?.let {
328-
PendingIntent.fromList(it)
350+
AndroidIntent.fromList(it)
329351
}
330352
}
331353
131.toByte() -> {
332354
return (readValue(buffer) as? List<Any?>)?.let {
333-
InboxStyle.fromList(it)
355+
PendingIntent.fromList(it)
334356
}
335357
}
336358
132.toByte() -> {
337359
return (readValue(buffer) as? List<Any?>)?.let {
338-
Person.fromList(it)
360+
InboxStyle.fromList(it)
339361
}
340362
}
341363
133.toByte() -> {
342364
return (readValue(buffer) as? List<Any?>)?.let {
343-
MessagingStyleMessage.fromList(it)
365+
Person.fromList(it)
344366
}
345367
}
346368
134.toByte() -> {
347369
return (readValue(buffer) as? List<Any?>)?.let {
348-
MessagingStyle.fromList(it)
370+
MessagingStyleMessage.fromList(it)
349371
}
350372
}
351373
135.toByte() -> {
352374
return (readValue(buffer) as? List<Any?>)?.let {
353-
Notification.fromList(it)
375+
MessagingStyle.fromList(it)
354376
}
355377
}
356378
136.toByte() -> {
379+
return (readValue(buffer) as? List<Any?>)?.let {
380+
Notification.fromList(it)
381+
}
382+
}
383+
137.toByte() -> {
357384
return (readValue(buffer) as? List<Any?>)?.let {
358385
StatusBarNotification.fromList(it)
359386
}
@@ -367,34 +394,38 @@ private object NotificationsPigeonCodec : StandardMessageCodec() {
367394
stream.write(129)
368395
writeValue(stream, value.toList())
369396
}
370-
is PendingIntent -> {
397+
is AndroidIntent -> {
371398
stream.write(130)
372399
writeValue(stream, value.toList())
373400
}
374-
is InboxStyle -> {
401+
is PendingIntent -> {
375402
stream.write(131)
376403
writeValue(stream, value.toList())
377404
}
378-
is Person -> {
405+
is InboxStyle -> {
379406
stream.write(132)
380407
writeValue(stream, value.toList())
381408
}
382-
is MessagingStyleMessage -> {
409+
is Person -> {
383410
stream.write(133)
384411
writeValue(stream, value.toList())
385412
}
386-
is MessagingStyle -> {
413+
is MessagingStyleMessage -> {
387414
stream.write(134)
388415
writeValue(stream, value.toList())
389416
}
390-
is Notification -> {
417+
is MessagingStyle -> {
391418
stream.write(135)
392419
writeValue(stream, value.toList())
393420
}
394-
is StatusBarNotification -> {
421+
is Notification -> {
395422
stream.write(136)
396423
writeValue(stream, value.toList())
397424
}
425+
is StatusBarNotification -> {
426+
stream.write(137)
427+
writeValue(stream, value.toList())
428+
}
398429
else -> super.writeValue(stream, value)
399430
}
400431
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ private class AndroidNotificationHost(val context: Context)
8787
// FlutterLocalNotificationsPlugin, which handles receiving the Intent.
8888
// TODO take care of receiving the notification-opened Intent ourselves
8989
action = "SELECT_NOTIFICATION"
90-
putExtra("payload", it.intentPayload)
90+
it.intent.extras.forEach { (k, v) -> putExtra(k!!, v!!) }
9191
},
9292
it.flags.toInt())
9393
) }

lib/host/android_notifications.g.dart

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,44 @@ class NotificationChannel {
6363
}
6464
}
6565

66+
/// Corresponds to `android.content.Intent`
67+
///
68+
/// See:
69+
/// https://developer.android.com/reference/android/content/Intent
70+
class AndroidIntent {
71+
AndroidIntent({
72+
required this.extras,
73+
});
74+
75+
Map<String?, String?> extras;
76+
77+
Object encode() {
78+
return <Object?>[
79+
extras,
80+
];
81+
}
82+
83+
static AndroidIntent decode(Object result) {
84+
result as List<Object?>;
85+
return AndroidIntent(
86+
extras: (result[0] as Map<Object?, Object?>?)!.cast<String?, String?>(),
87+
);
88+
}
89+
}
90+
6691
/// Corresponds to `android.app.PendingIntent`.
6792
///
6893
/// See: https://developer.android.com/reference/android/app/PendingIntent
6994
class PendingIntent {
7095
PendingIntent({
7196
required this.requestCode,
72-
required this.intentPayload,
97+
required this.intent,
7398
required this.flags,
7499
});
75100

76101
int requestCode;
77102

78-
/// A value set on an extra on the Intent, and passed to
79-
/// the on-notification-opened callback.
80-
String intentPayload;
103+
AndroidIntent intent;
81104

82105
/// A combination of flags from [PendingIntent.flags], and others associated
83106
/// with `Intent`; see Android docs for `PendingIntent.getActivity`.
@@ -86,7 +109,7 @@ class PendingIntent {
86109
Object encode() {
87110
return <Object?>[
88111
requestCode,
89-
intentPayload,
112+
intent,
90113
flags,
91114
];
92115
}
@@ -95,7 +118,7 @@ class PendingIntent {
95118
result as List<Object?>;
96119
return PendingIntent(
97120
requestCode: result[0]! as int,
98-
intentPayload: result[1]! as String,
121+
intent: result[1]! as AndroidIntent,
99122
flags: result[2]! as int,
100123
);
101124
}
@@ -311,27 +334,30 @@ class _PigeonCodec extends StandardMessageCodec {
311334
if (value is NotificationChannel) {
312335
buffer.putUint8(129);
313336
writeValue(buffer, value.encode());
314-
} else if (value is PendingIntent) {
337+
} else if (value is AndroidIntent) {
315338
buffer.putUint8(130);
316339
writeValue(buffer, value.encode());
317-
} else if (value is InboxStyle) {
340+
} else if (value is PendingIntent) {
318341
buffer.putUint8(131);
319342
writeValue(buffer, value.encode());
320-
} else if (value is Person) {
343+
} else if (value is InboxStyle) {
321344
buffer.putUint8(132);
322345
writeValue(buffer, value.encode());
323-
} else if (value is MessagingStyleMessage) {
346+
} else if (value is Person) {
324347
buffer.putUint8(133);
325348
writeValue(buffer, value.encode());
326-
} else if (value is MessagingStyle) {
349+
} else if (value is MessagingStyleMessage) {
327350
buffer.putUint8(134);
328351
writeValue(buffer, value.encode());
329-
} else if (value is Notification) {
352+
} else if (value is MessagingStyle) {
330353
buffer.putUint8(135);
331354
writeValue(buffer, value.encode());
332-
} else if (value is StatusBarNotification) {
355+
} else if (value is Notification) {
333356
buffer.putUint8(136);
334357
writeValue(buffer, value.encode());
358+
} else if (value is StatusBarNotification) {
359+
buffer.putUint8(137);
360+
writeValue(buffer, value.encode());
335361
} else {
336362
super.writeValue(buffer, value);
337363
}
@@ -343,18 +369,20 @@ class _PigeonCodec extends StandardMessageCodec {
343369
case 129:
344370
return NotificationChannel.decode(readValue(buffer)!);
345371
case 130:
346-
return PendingIntent.decode(readValue(buffer)!);
372+
return AndroidIntent.decode(readValue(buffer)!);
347373
case 131:
348-
return InboxStyle.decode(readValue(buffer)!);
374+
return PendingIntent.decode(readValue(buffer)!);
349375
case 132:
350-
return Person.decode(readValue(buffer)!);
376+
return InboxStyle.decode(readValue(buffer)!);
351377
case 133:
352-
return MessagingStyleMessage.decode(readValue(buffer)!);
378+
return Person.decode(readValue(buffer)!);
353379
case 134:
354-
return MessagingStyle.decode(readValue(buffer)!);
380+
return MessagingStyleMessage.decode(readValue(buffer)!);
355381
case 135:
356-
return Notification.decode(readValue(buffer)!);
382+
return MessagingStyle.decode(readValue(buffer)!);
357383
case 136:
384+
return Notification.decode(readValue(buffer)!);
385+
case 137:
358386
return StatusBarNotification.decode(readValue(buffer)!);
359387
default:
360388
return super.readValueOfType(type, buffer);

lib/notifications/display.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ class NotificationDisplayManager {
179179
// TODO is setting PendingIntentFlag.updateCurrent OK?
180180
// (That's a legacy of `flutter_local_notifications`.)
181181
flags: PendingIntentFlag.immutable | PendingIntentFlag.updateCurrent,
182-
intentPayload: jsonEncode(dataJson),
182+
intent: AndroidIntent(
183+
extras: <String, String>{'payload': jsonEncode(dataJson)}),
183184
// TODO this doesn't set the Intent flags we set in zulip-mobile; is that OK?
184185
// (This is a legacy of `flutter_local_notifications`.)
185186
),

pigeon/notifications.dart

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,27 @@ class NotificationChannel {
3636
final Int64List? vibrationPattern;
3737
}
3838

39+
/// Corresponds to `android.content.Intent`
40+
///
41+
/// See:
42+
/// https://developer.android.com/reference/android/content/Intent
43+
class AndroidIntent {
44+
AndroidIntent({required this.extras});
45+
46+
final Map<String?, String?> extras;
47+
}
48+
3949
/// Corresponds to `android.app.PendingIntent`.
4050
///
4151
/// See: https://developer.android.com/reference/android/app/PendingIntent
4252
class PendingIntent {
4353
/// Corresponds to `PendingIntent.getActivity`.
44-
PendingIntent({required this.requestCode, required this.intentPayload, required this.flags});
54+
///
55+
/// See: https://developer.android.com/reference/android/app/PendingIntent#getActivity(android.content.Context,%20int,%20android.content.Intent,%20int)
56+
PendingIntent({required this.requestCode, required this.intent, required this.flags});
4557

4658
final int requestCode;
47-
48-
/// A value set on an extra on the Intent, and passed to
49-
/// the on-notification-opened callback.
50-
// TODO replace intentPayload with a more direct wrapping of the underlying API
51-
final String intentPayload;
59+
final AndroidIntent intent;
5260

5361
/// A combination of flags from [PendingIntent.flags], and others associated
5462
/// with `Intent`; see Android docs for `PendingIntent.getActivity`.

test/notifications/display_test.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@ void main() {
196196
..contentIntent.which((it) => it.isNotNull()
197197
..requestCode.equals(expectedId)
198198
..flags.equals(expectedIntentFlags)
199-
..intentPayload.equals(jsonEncode(data.toJson()))),
199+
..intent.which((it) => it
200+
..extras.deepEquals({'payload': jsonEncode(data.toJson())}))),
200201
(it) => it.isA<AndroidNotificationHostApiNotifyCall>()
201202
..id.equals(NotificationDisplayManager.notificationIdAsHashOf(expectedGroupKey))
202203
..tag.equals(expectedGroupKey)
@@ -895,9 +896,13 @@ extension on Subject<AndroidNotificationHostApiNotifyCall> {
895896
Subject<String?> get smallIconResourceName => has((x) => x.smallIconResourceName, 'smallIconResourceName');
896897
}
897898

899+
extension on Subject<AndroidIntent> {
900+
Subject<Map<String?,String?>> get extras => has((x) => x.extras, 'extras');
901+
}
902+
898903
extension on Subject<PendingIntent> {
899904
Subject<int> get requestCode => has((x) => x.requestCode, 'requestCode');
900-
Subject<String> get intentPayload => has((x) => x.intentPayload, 'intentPayload');
905+
Subject<AndroidIntent> get intent => has((x) => x.intent, 'intent');
901906
Subject<int> get flags => has((x) => x.flags, 'flags');
902907
}
903908

0 commit comments

Comments
 (0)