Skip to content

Commit 9e1f53a

Browse files
committed
fix(push): Deserialization from platform maps (#3557)
Platform maps are type erased maps `Map<Object?, Object?>` which will fail the casts performed in `PushNotificationMessage.fromJson`. This was not caught before because the test data used constant Maps which were correctly typed. This removes all assumptions of Map types and updates tests to correctly handle this distinction.
1 parent 060eb06 commit 9e1f53a

13 files changed

+120
-87
lines changed

packages/amplify_core/lib/src/types/notifications/push/push_notification_message.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ class PushNotificationMessage
2020
this.data = const {},
2121
});
2222

23-
factory PushNotificationMessage.fromJson(Map<Object?, Object?> json) {
24-
final data = (json['data'] as Map<String, Object?>?) ?? const {};
23+
factory PushNotificationMessage.fromJson(Map<String, Object?> json) {
24+
final data =
25+
(json['data'] as Map<Object?, Object?>?)?.cast<String, Object?>() ??
26+
const {};
2527
switch (json) {
2628
// `aps` dictionary references:
2729
// - https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/generating_a_remote_notification
2830
// - https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html
29-
case {'aps': final Map<String, Object?> aps}:
31+
case {'aps': final Map<Object?, Object?> aps}:
3032
final title = switch (aps) {
3133
{'alert': final String alert} => alert,
3234
{'alert': {'title': final String title}} => title,

packages/notifications/push/amplify_push_notifications/analysis_options.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ include: package:amplify_lints/library.yaml
33
analyzer:
44
exclude:
55
- "**/*.g.dart"
6+
- "**/*.mocks.dart"

packages/notifications/push/amplify_push_notifications/android/src/main/kotlin/com/amazonaws/amplify/amplify_push_notifications/PushNotificationsHostApiBindings.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
// SPDX-License-Identifier: Apache-2.0
4-
// Autogenerated from Pigeon (v10.1.2), do not edit directly.
4+
// Autogenerated from Pigeon (v10.1.6), do not edit directly.
55
// See also: https://pub.dev/packages/pigeon
66

77
package com.amazonaws.amplify.amplify_push_notifications;
@@ -277,15 +277,15 @@ public interface Reply<T> {
277277
public void onNotificationReceivedInBackground(@NonNull Map<Object, Object> withPayloadArg, @NonNull Reply<Void> callback) {
278278
BasicMessageChannel<Object> channel =
279279
new BasicMessageChannel<>(
280-
binaryMessenger, "dev.flutter.pigeon.PushNotificationsFlutterApi.onNotificationReceivedInBackground", getCodec());
280+
binaryMessenger, "dev.flutter.pigeon.amplify_push_notifications.PushNotificationsFlutterApi.onNotificationReceivedInBackground", getCodec());
281281
channel.send(
282282
new ArrayList<Object>(Collections.singletonList(withPayloadArg)),
283283
channelReply -> callback.reply(null));
284284
}
285285
public void nullifyLaunchNotification(@NonNull Reply<Void> callback) {
286286
BasicMessageChannel<Object> channel =
287287
new BasicMessageChannel<>(
288-
binaryMessenger, "dev.flutter.pigeon.PushNotificationsFlutterApi.nullifyLaunchNotification", getCodec());
288+
binaryMessenger, "dev.flutter.pigeon.amplify_push_notifications.PushNotificationsFlutterApi.nullifyLaunchNotification", getCodec());
289289
channel.send(
290290
null,
291291
channelReply -> callback.reply(null));
@@ -351,7 +351,7 @@ static void setup(@NonNull BinaryMessenger binaryMessenger, @Nullable PushNotifi
351351
{
352352
BasicMessageChannel<Object> channel =
353353
new BasicMessageChannel<>(
354-
binaryMessenger, "dev.flutter.pigeon.PushNotificationsHostApi.requestInitialToken", getCodec());
354+
binaryMessenger, "dev.flutter.pigeon.amplify_push_notifications.PushNotificationsHostApi.requestInitialToken", getCodec());
355355
if (api != null) {
356356
channel.setMessageHandler(
357357
(message, reply) -> {
@@ -373,7 +373,7 @@ static void setup(@NonNull BinaryMessenger binaryMessenger, @Nullable PushNotifi
373373
{
374374
BasicMessageChannel<Object> channel =
375375
new BasicMessageChannel<>(
376-
binaryMessenger, "dev.flutter.pigeon.PushNotificationsHostApi.getPermissionStatus", getCodec());
376+
binaryMessenger, "dev.flutter.pigeon.amplify_push_notifications.PushNotificationsHostApi.getPermissionStatus", getCodec());
377377
if (api != null) {
378378
channel.setMessageHandler(
379379
(message, reply) -> {
@@ -400,7 +400,7 @@ public void error(Throwable error) {
400400
{
401401
BasicMessageChannel<Object> channel =
402402
new BasicMessageChannel<>(
403-
binaryMessenger, "dev.flutter.pigeon.PushNotificationsHostApi.requestPermissions", getCodec());
403+
binaryMessenger, "dev.flutter.pigeon.amplify_push_notifications.PushNotificationsHostApi.requestPermissions", getCodec());
404404
if (api != null) {
405405
channel.setMessageHandler(
406406
(message, reply) -> {
@@ -429,7 +429,7 @@ public void error(Throwable error) {
429429
{
430430
BasicMessageChannel<Object> channel =
431431
new BasicMessageChannel<>(
432-
binaryMessenger, "dev.flutter.pigeon.PushNotificationsHostApi.getLaunchNotification", getCodec());
432+
binaryMessenger, "dev.flutter.pigeon.amplify_push_notifications.PushNotificationsHostApi.getLaunchNotification", getCodec());
433433
if (api != null) {
434434
channel.setMessageHandler(
435435
(message, reply) -> {
@@ -451,7 +451,7 @@ public void error(Throwable error) {
451451
{
452452
BasicMessageChannel<Object> channel =
453453
new BasicMessageChannel<>(
454-
binaryMessenger, "dev.flutter.pigeon.PushNotificationsHostApi.getBadgeCount", getCodec());
454+
binaryMessenger, "dev.flutter.pigeon.amplify_push_notifications.PushNotificationsHostApi.getBadgeCount", getCodec());
455455
if (api != null) {
456456
channel.setMessageHandler(
457457
(message, reply) -> {
@@ -473,7 +473,7 @@ public void error(Throwable error) {
473473
{
474474
BasicMessageChannel<Object> channel =
475475
new BasicMessageChannel<>(
476-
binaryMessenger, "dev.flutter.pigeon.PushNotificationsHostApi.setBadgeCount", getCodec());
476+
binaryMessenger, "dev.flutter.pigeon.amplify_push_notifications.PushNotificationsHostApi.setBadgeCount", getCodec());
477477
if (api != null) {
478478
channel.setMessageHandler(
479479
(message, reply) -> {
@@ -497,7 +497,7 @@ public void error(Throwable error) {
497497
{
498498
BasicMessageChannel<Object> channel =
499499
new BasicMessageChannel<>(
500-
binaryMessenger, "dev.flutter.pigeon.PushNotificationsHostApi.registerCallbackFunction", getCodec());
500+
binaryMessenger, "dev.flutter.pigeon.amplify_push_notifications.PushNotificationsHostApi.registerCallbackFunction", getCodec());
501501
if (api != null) {
502502
channel.setMessageHandler(
503503
(message, reply) -> {

packages/notifications/push/amplify_push_notifications/ios/Classes/PushNotificationsNativePlugin.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
// SPDX-License-Identifier: Apache-2.0
4-
// Autogenerated from Pigeon (v10.1.2), do not edit directly.
4+
// Autogenerated from Pigeon (v10.1.6), do not edit directly.
55
// See also: https://pub.dev/packages/pigeon
66

77
#import <Foundation/Foundation.h>

packages/notifications/push/amplify_push_notifications/ios/Classes/PushNotificationsNativePlugin.m

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
// SPDX-License-Identifier: Apache-2.0
4-
// Autogenerated from Pigeon (v10.1.2), do not edit directly.
4+
// Autogenerated from Pigeon (v10.1.6), do not edit directly.
55
// See also: https://pub.dev/packages/pigeon
66

77
#import "PushNotificationsNativePlugin.h"
@@ -162,7 +162,7 @@ - (instancetype)initWithBinaryMessenger:(NSObject<FlutterBinaryMessenger> *)bina
162162
- (void)onNotificationReceivedInBackgroundWithPayload:(NSDictionary<id, id> *)arg_withPayload completion:(void (^)(FlutterError *_Nullable))completion {
163163
FlutterBasicMessageChannel *channel =
164164
[FlutterBasicMessageChannel
165-
messageChannelWithName:@"dev.flutter.pigeon.PushNotificationsFlutterApi.onNotificationReceivedInBackground"
165+
messageChannelWithName:@"dev.flutter.pigeon.amplify_push_notifications.PushNotificationsFlutterApi.onNotificationReceivedInBackground"
166166
binaryMessenger:self.binaryMessenger
167167
codec:PushNotificationsFlutterApiGetCodec()];
168168
[channel sendMessage:@[arg_withPayload ?: [NSNull null]] reply:^(id reply) {
@@ -172,7 +172,7 @@ - (void)onNotificationReceivedInBackgroundWithPayload:(NSDictionary<id, id> *)ar
172172
- (void)nullifyLaunchNotificationWithCompletion:(void (^)(FlutterError *_Nullable))completion {
173173
FlutterBasicMessageChannel *channel =
174174
[FlutterBasicMessageChannel
175-
messageChannelWithName:@"dev.flutter.pigeon.PushNotificationsFlutterApi.nullifyLaunchNotification"
175+
messageChannelWithName:@"dev.flutter.pigeon.amplify_push_notifications.PushNotificationsFlutterApi.nullifyLaunchNotification"
176176
binaryMessenger:self.binaryMessenger
177177
codec:PushNotificationsFlutterApiGetCodec()];
178178
[channel sendMessage:nil reply:^(id reply) {
@@ -237,7 +237,7 @@ void PushNotificationsHostApiSetup(id<FlutterBinaryMessenger> binaryMessenger, N
237237
{
238238
FlutterBasicMessageChannel *channel =
239239
[[FlutterBasicMessageChannel alloc]
240-
initWithName:@"dev.flutter.pigeon.PushNotificationsHostApi.requestInitialToken"
240+
initWithName:@"dev.flutter.pigeon.amplify_push_notifications.PushNotificationsHostApi.requestInitialToken"
241241
binaryMessenger:binaryMessenger
242242
codec:PushNotificationsHostApiGetCodec()];
243243
if (api) {
@@ -254,7 +254,7 @@ void PushNotificationsHostApiSetup(id<FlutterBinaryMessenger> binaryMessenger, N
254254
{
255255
FlutterBasicMessageChannel *channel =
256256
[[FlutterBasicMessageChannel alloc]
257-
initWithName:@"dev.flutter.pigeon.PushNotificationsHostApi.getPermissionStatus"
257+
initWithName:@"dev.flutter.pigeon.amplify_push_notifications.PushNotificationsHostApi.getPermissionStatus"
258258
binaryMessenger:binaryMessenger
259259
codec:PushNotificationsHostApiGetCodec()];
260260
if (api) {
@@ -271,7 +271,7 @@ void PushNotificationsHostApiSetup(id<FlutterBinaryMessenger> binaryMessenger, N
271271
{
272272
FlutterBasicMessageChannel *channel =
273273
[[FlutterBasicMessageChannel alloc]
274-
initWithName:@"dev.flutter.pigeon.PushNotificationsHostApi.requestPermissions"
274+
initWithName:@"dev.flutter.pigeon.amplify_push_notifications.PushNotificationsHostApi.requestPermissions"
275275
binaryMessenger:binaryMessenger
276276
codec:PushNotificationsHostApiGetCodec()];
277277
if (api) {
@@ -290,7 +290,7 @@ void PushNotificationsHostApiSetup(id<FlutterBinaryMessenger> binaryMessenger, N
290290
{
291291
FlutterBasicMessageChannel *channel =
292292
[[FlutterBasicMessageChannel alloc]
293-
initWithName:@"dev.flutter.pigeon.PushNotificationsHostApi.getLaunchNotification"
293+
initWithName:@"dev.flutter.pigeon.amplify_push_notifications.PushNotificationsHostApi.getLaunchNotification"
294294
binaryMessenger:binaryMessenger
295295
codec:PushNotificationsHostApiGetCodec()];
296296
if (api) {
@@ -307,7 +307,7 @@ void PushNotificationsHostApiSetup(id<FlutterBinaryMessenger> binaryMessenger, N
307307
{
308308
FlutterBasicMessageChannel *channel =
309309
[[FlutterBasicMessageChannel alloc]
310-
initWithName:@"dev.flutter.pigeon.PushNotificationsHostApi.getBadgeCount"
310+
initWithName:@"dev.flutter.pigeon.amplify_push_notifications.PushNotificationsHostApi.getBadgeCount"
311311
binaryMessenger:binaryMessenger
312312
codec:PushNotificationsHostApiGetCodec()];
313313
if (api) {
@@ -324,7 +324,7 @@ void PushNotificationsHostApiSetup(id<FlutterBinaryMessenger> binaryMessenger, N
324324
{
325325
FlutterBasicMessageChannel *channel =
326326
[[FlutterBasicMessageChannel alloc]
327-
initWithName:@"dev.flutter.pigeon.PushNotificationsHostApi.setBadgeCount"
327+
initWithName:@"dev.flutter.pigeon.amplify_push_notifications.PushNotificationsHostApi.setBadgeCount"
328328
binaryMessenger:binaryMessenger
329329
codec:PushNotificationsHostApiGetCodec()];
330330
if (api) {
@@ -343,7 +343,7 @@ void PushNotificationsHostApiSetup(id<FlutterBinaryMessenger> binaryMessenger, N
343343
{
344344
FlutterBasicMessageChannel *channel =
345345
[[FlutterBasicMessageChannel alloc]
346-
initWithName:@"dev.flutter.pigeon.PushNotificationsHostApi.registerCallbackFunction"
346+
initWithName:@"dev.flutter.pigeon.amplify_push_notifications.PushNotificationsHostApi.registerCallbackFunction"
347347
binaryMessenger:binaryMessenger
348348
codec:PushNotificationsHostApiGetCodec()];
349349
if (api) {

packages/notifications/push/amplify_push_notifications/lib/src/amplify_push_notifications_impl.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,13 @@ abstract class AmplifyPushNotifications
104104
_onForegroundNotificationReceived = foregroundNotificationEventChannel
105105
.receiveBroadcastStream()
106106
.cast<Map<Object?, Object?>>()
107+
.map((map) => map.cast<String, Object?>())
107108
.map(PushNotificationMessage.fromJson);
108109

109110
_onNotificationOpened = notificationOpenedEventChannel
110111
.receiveBroadcastStream()
111112
.cast<Map<Object?, Object?>>()
113+
.map((map) => map.cast<String, Object?>())
112114
.map(PushNotificationMessage.fromJson);
113115
}
114116

@@ -243,7 +245,7 @@ abstract class AmplifyPushNotifications
243245
final rawLaunchNotification = await _hostApi.getLaunchNotification();
244246
if (rawLaunchNotification != null) {
245247
final launchNotification =
246-
PushNotificationMessage.fromJson(rawLaunchNotification);
248+
PushNotificationMessage.fromJson(rawLaunchNotification.cast());
247249
_launchNotification = launchNotification;
248250
_flutterApi.onNullifyLaunchNotificationCallback = () {
249251
_launchNotification = null;

packages/notifications/push/amplify_push_notifications/lib/src/native_push_notifications_plugin.g.dart

Lines changed: 15 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/notifications/push/amplify_push_notifications/lib/src/push_notifications_flutter_api.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class AmplifyPushNotificationsFlutterApi
114114
Future<void> onNotificationReceivedInBackground(
115115
Map<Object?, Object?> payload,
116116
) async {
117-
final notification = PushNotificationMessage.fromJson(payload);
117+
final notification = PushNotificationMessage.fromJson(payload.cast());
118118

119119
// Queue when service client is not available without blocking invocation of external callback
120120
if (_serviceProviderClient != null) {

0 commit comments

Comments
 (0)