Skip to content

Commit e16885b

Browse files
notif [nfc]: Replace intentPayload with AndroidIntent.extras
1 parent cfce389 commit e16885b

File tree

6 files changed

+115
-42
lines changed

6 files changed

+115
-42
lines changed

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

Lines changed: 48 additions & 17 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
}
@@ -266,25 +288,30 @@ private object NotificationsPigeonCodec : StandardMessageCodec() {
266288
}
267289
130.toByte() -> {
268290
return (readValue(buffer) as? List<Any?>)?.let {
269-
PendingIntent.fromList(it)
291+
AndroidIntent.fromList(it)
270292
}
271293
}
272294
131.toByte() -> {
273295
return (readValue(buffer) as? List<Any?>)?.let {
274-
InboxStyle.fromList(it)
296+
PendingIntent.fromList(it)
275297
}
276298
}
277299
132.toByte() -> {
278300
return (readValue(buffer) as? List<Any?>)?.let {
279-
Person.fromList(it)
301+
InboxStyle.fromList(it)
280302
}
281303
}
282304
133.toByte() -> {
283305
return (readValue(buffer) as? List<Any?>)?.let {
284-
MessagingStyleMessage.fromList(it)
306+
Person.fromList(it)
285307
}
286308
}
287309
134.toByte() -> {
310+
return (readValue(buffer) as? List<Any?>)?.let {
311+
MessagingStyleMessage.fromList(it)
312+
}
313+
}
314+
135.toByte() -> {
288315
return (readValue(buffer) as? List<Any?>)?.let {
289316
MessagingStyle.fromList(it)
290317
}
@@ -298,26 +325,30 @@ private object NotificationsPigeonCodec : StandardMessageCodec() {
298325
stream.write(129)
299326
writeValue(stream, value.toList())
300327
}
301-
is PendingIntent -> {
328+
is AndroidIntent -> {
302329
stream.write(130)
303330
writeValue(stream, value.toList())
304331
}
305-
is InboxStyle -> {
332+
is PendingIntent -> {
306333
stream.write(131)
307334
writeValue(stream, value.toList())
308335
}
309-
is Person -> {
336+
is InboxStyle -> {
310337
stream.write(132)
311338
writeValue(stream, value.toList())
312339
}
313-
is MessagingStyleMessage -> {
340+
is Person -> {
314341
stream.write(133)
315342
writeValue(stream, value.toList())
316343
}
317-
is MessagingStyle -> {
344+
is MessagingStyleMessage -> {
318345
stream.write(134)
319346
writeValue(stream, value.toList())
320347
}
348+
is MessagingStyle -> {
349+
stream.write(135)
350+
writeValue(stream, value.toList())
351+
}
321352
else -> super.writeValue(stream, value)
322353
}
323354
}

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: 43 additions & 15 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
}
@@ -248,21 +271,24 @@ class _PigeonCodec extends StandardMessageCodec {
248271
if (value is NotificationChannel) {
249272
buffer.putUint8(129);
250273
writeValue(buffer, value.encode());
251-
} else if (value is PendingIntent) {
274+
} else if (value is AndroidIntent) {
252275
buffer.putUint8(130);
253276
writeValue(buffer, value.encode());
254-
} else if (value is InboxStyle) {
277+
} else if (value is PendingIntent) {
255278
buffer.putUint8(131);
256279
writeValue(buffer, value.encode());
257-
} else if (value is Person) {
280+
} else if (value is InboxStyle) {
258281
buffer.putUint8(132);
259282
writeValue(buffer, value.encode());
260-
} else if (value is MessagingStyleMessage) {
283+
} else if (value is Person) {
261284
buffer.putUint8(133);
262285
writeValue(buffer, value.encode());
263-
} else if (value is MessagingStyle) {
286+
} else if (value is MessagingStyleMessage) {
264287
buffer.putUint8(134);
265288
writeValue(buffer, value.encode());
289+
} else if (value is MessagingStyle) {
290+
buffer.putUint8(135);
291+
writeValue(buffer, value.encode());
266292
} else {
267293
super.writeValue(buffer, value);
268294
}
@@ -274,14 +300,16 @@ class _PigeonCodec extends StandardMessageCodec {
274300
case 129:
275301
return NotificationChannel.decode(readValue(buffer)!);
276302
case 130:
277-
return PendingIntent.decode(readValue(buffer)!);
303+
return AndroidIntent.decode(readValue(buffer)!);
278304
case 131:
279-
return InboxStyle.decode(readValue(buffer)!);
305+
return PendingIntent.decode(readValue(buffer)!);
280306
case 132:
281-
return Person.decode(readValue(buffer)!);
307+
return InboxStyle.decode(readValue(buffer)!);
282308
case 133:
283-
return MessagingStyleMessage.decode(readValue(buffer)!);
309+
return Person.decode(readValue(buffer)!);
284310
case 134:
311+
return MessagingStyleMessage.decode(readValue(buffer)!);
312+
case 135:
285313
return MessagingStyle.decode(readValue(buffer)!);
286314
default:
287315
return super.readValueOfType(type, buffer);

lib/notifications/display.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ class NotificationDisplayManager {
173173
// TODO is setting PendingIntentFlag.updateCurrent OK?
174174
// (That's a legacy of `flutter_local_notifications`.)
175175
flags: PendingIntentFlag.immutable | PendingIntentFlag.updateCurrent,
176-
intentPayload: jsonEncode(dataJson),
176+
intent: AndroidIntent(
177+
extras: <String, String>{'payload': jsonEncode(dataJson)}),
177178
// TODO this doesn't set the Intent flags we set in zulip-mobile; is that OK?
178179
// (This is a legacy of `flutter_local_notifications`.)
179180
),

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
@@ -179,7 +179,8 @@ void main() {
179179
..contentIntent.which((it) => it.isNotNull()
180180
..requestCode.equals(expectedId)
181181
..flags.equals(expectedIntentFlags)
182-
..intentPayload.equals(jsonEncode(data.toJson()))),
182+
..intent.which((it) => it
183+
..extras.deepEquals({'payload': jsonEncode(data.toJson())}))),
183184
(it) => it.isA<AndroidNotificationHostApiNotifyCall>()
184185
..id.equals(NotificationDisplayManager.notificationIdAsHashOf(expectedGroupKey))
185186
..tag.equals(expectedGroupKey)
@@ -672,9 +673,13 @@ extension on Subject<AndroidNotificationHostApiNotifyCall> {
672673
Subject<String?> get smallIconResourceName => has((x) => x.smallIconResourceName, 'smallIconResourceName');
673674
}
674675

676+
extension on Subject<AndroidIntent> {
677+
Subject<Map<String?,String?>> get extras => has((x) => x.extras, 'extras');
678+
}
679+
675680
extension on Subject<PendingIntent> {
676681
Subject<int> get requestCode => has((x) => x.requestCode, 'requestCode');
677-
Subject<String> get intentPayload => has((x) => x.intentPayload, 'intentPayload');
682+
Subject<AndroidIntent> get intent => has((x) => x.intent, 'intent');
678683
Subject<int> get flags => has((x) => x.flags, 'flags');
679684
}
680685

0 commit comments

Comments
 (0)