Skip to content

Commit 3fa51c5

Browse files
rajveermalviyagnprice
authored andcommitted
notif [nfc]: Introduce AndroidIntent; replace PendingIntent.intentPayload
1 parent a7812b3 commit 3fa51c5

File tree

6 files changed

+140
-56
lines changed

6 files changed

+140
-56
lines changed

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

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

90+
/**
91+
* Corresponds to `android.content.Intent`
92+
*
93+
* See: https://developer.android.com/reference/android/content/Intent
94+
*
95+
* Generated class from Pigeon that represents data sent in messages.
96+
*/
97+
data class AndroidIntent (
98+
val action: String,
99+
val extras: Map<String?, String?>
100+
101+
) {
102+
companion object {
103+
@Suppress("LocalVariableName")
104+
fun fromList(__pigeon_list: List<Any?>): AndroidIntent {
105+
val action = __pigeon_list[0] as String
106+
val extras = __pigeon_list[1] as Map<String?, String?>
107+
return AndroidIntent(action, extras)
108+
}
109+
}
110+
fun toList(): List<Any?> {
111+
return listOf(
112+
action,
113+
extras,
114+
)
115+
}
116+
}
117+
90118
/**
91119
* Corresponds to `android.app.PendingIntent`.
92120
*
@@ -96,11 +124,7 @@ data class NotificationChannel (
96124
*/
97125
data class PendingIntent (
98126
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,
127+
val intent: AndroidIntent,
104128
/**
105129
* A combination of flags from [PendingIntent.flags], and others associated
106130
* with `Intent`; see Android docs for `PendingIntent.getActivity`.
@@ -112,15 +136,15 @@ data class PendingIntent (
112136
@Suppress("LocalVariableName")
113137
fun fromList(__pigeon_list: List<Any?>): PendingIntent {
114138
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
139+
val intent = __pigeon_list[1] as AndroidIntent
116140
val flags = __pigeon_list[2].let { num -> if (num is Int) num.toLong() else num as Long }
117-
return PendingIntent(requestCode, intentPayload, flags)
141+
return PendingIntent(requestCode, intent, flags)
118142
}
119143
}
120144
fun toList(): List<Any?> {
121145
return listOf(
122146
requestCode,
123-
intentPayload,
147+
intent,
124148
flags,
125149
)
126150
}
@@ -325,35 +349,40 @@ private object NotificationsPigeonCodec : StandardMessageCodec() {
325349
}
326350
130.toByte() -> {
327351
return (readValue(buffer) as? List<Any?>)?.let {
328-
PendingIntent.fromList(it)
352+
AndroidIntent.fromList(it)
329353
}
330354
}
331355
131.toByte() -> {
332356
return (readValue(buffer) as? List<Any?>)?.let {
333-
InboxStyle.fromList(it)
357+
PendingIntent.fromList(it)
334358
}
335359
}
336360
132.toByte() -> {
337361
return (readValue(buffer) as? List<Any?>)?.let {
338-
Person.fromList(it)
362+
InboxStyle.fromList(it)
339363
}
340364
}
341365
133.toByte() -> {
342366
return (readValue(buffer) as? List<Any?>)?.let {
343-
MessagingStyleMessage.fromList(it)
367+
Person.fromList(it)
344368
}
345369
}
346370
134.toByte() -> {
347371
return (readValue(buffer) as? List<Any?>)?.let {
348-
MessagingStyle.fromList(it)
372+
MessagingStyleMessage.fromList(it)
349373
}
350374
}
351375
135.toByte() -> {
352376
return (readValue(buffer) as? List<Any?>)?.let {
353-
Notification.fromList(it)
377+
MessagingStyle.fromList(it)
354378
}
355379
}
356380
136.toByte() -> {
381+
return (readValue(buffer) as? List<Any?>)?.let {
382+
Notification.fromList(it)
383+
}
384+
}
385+
137.toByte() -> {
357386
return (readValue(buffer) as? List<Any?>)?.let {
358387
StatusBarNotification.fromList(it)
359388
}
@@ -367,34 +396,38 @@ private object NotificationsPigeonCodec : StandardMessageCodec() {
367396
stream.write(129)
368397
writeValue(stream, value.toList())
369398
}
370-
is PendingIntent -> {
399+
is AndroidIntent -> {
371400
stream.write(130)
372401
writeValue(stream, value.toList())
373402
}
374-
is InboxStyle -> {
403+
is PendingIntent -> {
375404
stream.write(131)
376405
writeValue(stream, value.toList())
377406
}
378-
is Person -> {
407+
is InboxStyle -> {
379408
stream.write(132)
380409
writeValue(stream, value.toList())
381410
}
382-
is MessagingStyleMessage -> {
411+
is Person -> {
383412
stream.write(133)
384413
writeValue(stream, value.toList())
385414
}
386-
is MessagingStyle -> {
415+
is MessagingStyleMessage -> {
387416
stream.write(134)
388417
writeValue(stream, value.toList())
389418
}
390-
is Notification -> {
419+
is MessagingStyle -> {
391420
stream.write(135)
392421
writeValue(stream, value.toList())
393422
}
394-
is StatusBarNotification -> {
423+
is Notification -> {
395424
stream.write(136)
396425
writeValue(stream, value.toList())
397426
}
427+
is StatusBarNotification -> {
428+
stream.write(137)
429+
writeValue(stream, value.toList())
430+
}
398431
else -> super.writeValue(stream, value)
399432
}
400433
}

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,10 @@ private class AndroidNotificationHost(val context: Context)
9797
contentIntent?.let { setContentIntent(
9898
android.app.PendingIntent.getActivity(context,
9999
it.requestCode.toInt(),
100-
Intent(context, MainActivity::class.java).apply {
101-
// This action name and extra name are special to
102-
// FlutterLocalNotificationsPlugin, which handles receiving the Intent.
103-
// TODO take care of receiving the notification-opened Intent ourselves
104-
action = "SELECT_NOTIFICATION"
105-
putExtra("payload", it.intentPayload)
106-
},
100+
it.intent.let { intent -> Intent(context, MainActivity::class.java).apply {
101+
action = intent.action
102+
intent.extras.forEach { (k, v) -> putExtra(k!!, v!!) }
103+
} },
107104
it.flags.toInt())
108105
) }
109106
contentText?.let { setContentText(it) }

lib/host/android_notifications.g.dart

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

66+
/// Corresponds to `android.content.Intent`
67+
///
68+
/// See: https://developer.android.com/reference/android/content/Intent
69+
class AndroidIntent {
70+
AndroidIntent({
71+
required this.action,
72+
required this.extras,
73+
});
74+
75+
String action;
76+
77+
Map<String?, String?> extras;
78+
79+
Object encode() {
80+
return <Object?>[
81+
action,
82+
extras,
83+
];
84+
}
85+
86+
static AndroidIntent decode(Object result) {
87+
result as List<Object?>;
88+
return AndroidIntent(
89+
action: result[0]! as String,
90+
extras: (result[1] as Map<Object?, Object?>?)!.cast<String?, String?>(),
91+
);
92+
}
93+
}
94+
6695
/// Corresponds to `android.app.PendingIntent`.
6796
///
6897
/// See: https://developer.android.com/reference/android/app/PendingIntent
6998
class PendingIntent {
7099
PendingIntent({
71100
required this.requestCode,
72-
required this.intentPayload,
101+
required this.intent,
73102
required this.flags,
74103
});
75104

76105
int requestCode;
77106

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

82109
/// A combination of flags from [PendingIntent.flags], and others associated
83110
/// with `Intent`; see Android docs for `PendingIntent.getActivity`.
@@ -86,7 +113,7 @@ class PendingIntent {
86113
Object encode() {
87114
return <Object?>[
88115
requestCode,
89-
intentPayload,
116+
intent,
90117
flags,
91118
];
92119
}
@@ -95,7 +122,7 @@ class PendingIntent {
95122
result as List<Object?>;
96123
return PendingIntent(
97124
requestCode: result[0]! as int,
98-
intentPayload: result[1]! as String,
125+
intent: result[1]! as AndroidIntent,
99126
flags: result[2]! as int,
100127
);
101128
}
@@ -311,27 +338,30 @@ class _PigeonCodec extends StandardMessageCodec {
311338
if (value is NotificationChannel) {
312339
buffer.putUint8(129);
313340
writeValue(buffer, value.encode());
314-
} else if (value is PendingIntent) {
341+
} else if (value is AndroidIntent) {
315342
buffer.putUint8(130);
316343
writeValue(buffer, value.encode());
317-
} else if (value is InboxStyle) {
344+
} else if (value is PendingIntent) {
318345
buffer.putUint8(131);
319346
writeValue(buffer, value.encode());
320-
} else if (value is Person) {
347+
} else if (value is InboxStyle) {
321348
buffer.putUint8(132);
322349
writeValue(buffer, value.encode());
323-
} else if (value is MessagingStyleMessage) {
350+
} else if (value is Person) {
324351
buffer.putUint8(133);
325352
writeValue(buffer, value.encode());
326-
} else if (value is MessagingStyle) {
353+
} else if (value is MessagingStyleMessage) {
327354
buffer.putUint8(134);
328355
writeValue(buffer, value.encode());
329-
} else if (value is Notification) {
356+
} else if (value is MessagingStyle) {
330357
buffer.putUint8(135);
331358
writeValue(buffer, value.encode());
332-
} else if (value is StatusBarNotification) {
359+
} else if (value is Notification) {
333360
buffer.putUint8(136);
334361
writeValue(buffer, value.encode());
362+
} else if (value is StatusBarNotification) {
363+
buffer.putUint8(137);
364+
writeValue(buffer, value.encode());
335365
} else {
336366
super.writeValue(buffer, value);
337367
}
@@ -343,18 +373,20 @@ class _PigeonCodec extends StandardMessageCodec {
343373
case 129:
344374
return NotificationChannel.decode(readValue(buffer)!);
345375
case 130:
346-
return PendingIntent.decode(readValue(buffer)!);
376+
return AndroidIntent.decode(readValue(buffer)!);
347377
case 131:
348-
return InboxStyle.decode(readValue(buffer)!);
378+
return PendingIntent.decode(readValue(buffer)!);
349379
case 132:
350-
return Person.decode(readValue(buffer)!);
380+
return InboxStyle.decode(readValue(buffer)!);
351381
case 133:
352-
return MessagingStyleMessage.decode(readValue(buffer)!);
382+
return Person.decode(readValue(buffer)!);
353383
case 134:
354-
return MessagingStyle.decode(readValue(buffer)!);
384+
return MessagingStyleMessage.decode(readValue(buffer)!);
355385
case 135:
356-
return Notification.decode(readValue(buffer)!);
386+
return MessagingStyle.decode(readValue(buffer)!);
357387
case 136:
388+
return Notification.decode(readValue(buffer)!);
389+
case 137:
358390
return StatusBarNotification.decode(readValue(buffer)!);
359391
default:
360392
return super.readValueOfType(type, buffer);

lib/notifications/display.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,14 @@ class NotificationDisplayManager {
204204
// TODO is setting PendingIntentFlag.updateCurrent OK?
205205
// (That's a legacy of `flutter_local_notifications`.)
206206
flags: PendingIntentFlag.immutable | PendingIntentFlag.updateCurrent,
207-
intentPayload: jsonEncode(dataJson),
207+
intent: AndroidIntent(
208+
// This action name and extra name are special to
209+
// FlutterLocalNotificationsPlugin, which handles receiving the Intent.
210+
// TODO take care of receiving the notification-opened Intent ourselves
211+
action: 'SELECT_NOTIFICATION',
212+
extras: <String, String>{
213+
'payload': jsonEncode(dataJson),
214+
}),
208215
// TODO this doesn't set the Intent flags we set in zulip-mobile; is that OK?
209216
// (This is a legacy of `flutter_local_notifications`.)
210217
),

pigeon/notifications.dart

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

39+
/// Corresponds to `android.content.Intent`
40+
///
41+
/// See: https://developer.android.com/reference/android/content/Intent
42+
class AndroidIntent {
43+
AndroidIntent({required this.action, required this.extras});
44+
45+
final String action;
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+
PendingIntent({required this.requestCode, required this.intent, required this.flags});
4555

4656
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;
57+
final AndroidIntent intent;
5258

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

test/notifications/display_test.dart

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,11 @@ void main() {
272272
..contentIntent.which((it) => it.isNotNull()
273273
..requestCode.equals(expectedId)
274274
..flags.equals(expectedIntentFlags)
275-
..intentPayload.equals(jsonEncode(data.toJson()))),
275+
..intent.which((it) => it
276+
..action.equals('SELECT_NOTIFICATION')
277+
..extras.deepEquals({
278+
'payload': jsonEncode(data.toJson()),
279+
}))),
276280
(it) => it.isA<AndroidNotificationHostApiNotifyCall>()
277281
..id.equals(NotificationDisplayManager.notificationIdAsHashOf(expectedGroupKey))
278282
..tag.equals(expectedGroupKey)
@@ -979,10 +983,15 @@ extension on Subject<AndroidNotificationHostApiNotifyCall> {
979983

980984
extension on Subject<PendingIntent> {
981985
Subject<int> get requestCode => has((x) => x.requestCode, 'requestCode');
982-
Subject<String> get intentPayload => has((x) => x.intentPayload, 'intentPayload');
986+
Subject<AndroidIntent> get intent => has((x) => x.intent, 'intent');
983987
Subject<int> get flags => has((x) => x.flags, 'flags');
984988
}
985989

990+
extension on Subject<AndroidIntent> {
991+
Subject<String> get action => has((x) => x.action, 'action');
992+
Subject<Map<String?, String?>> get extras => has((x) => x.extras, 'extras');
993+
}
994+
986995
extension on Subject<InboxStyle> {
987996
Subject<String> get summaryText => has((x) => x.summaryText, 'summaryText');
988997
}

0 commit comments

Comments
 (0)