Skip to content

Commit

Permalink
fix(push): Deserialization from platform maps (#3557)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dnys1 authored Aug 16, 2023
1 parent 702ccf7 commit f09132c
Show file tree
Hide file tree
Showing 13 changed files with 120 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ class PushNotificationMessage
this.data = const {},
});

factory PushNotificationMessage.fromJson(Map<Object?, Object?> json) {
final data = (json['data'] as Map<String, Object?>?) ?? const {};
factory PushNotificationMessage.fromJson(Map<String, Object?> json) {
final data =
(json['data'] as Map<Object?, Object?>?)?.cast<String, Object?>() ??
const {};
switch (json) {
// `aps` dictionary references:
// - https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/generating_a_remote_notification
// - https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html
case {'aps': final Map<String, Object?> aps}:
case {'aps': final Map<Object?, Object?> aps}:
final title = switch (aps) {
{'alert': final String alert} => alert,
{'alert': {'title': final String title}} => title,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ include: package:amplify_lints/library.yaml
analyzer:
exclude:
- "**/*.g.dart"
- "**/*.mocks.dart"
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// Autogenerated from Pigeon (v10.1.2), do not edit directly.
// Autogenerated from Pigeon (v10.1.6), do not edit directly.
// See also: https://pub.dev/packages/pigeon

package com.amazonaws.amplify.amplify_push_notifications;
Expand Down Expand Up @@ -277,15 +277,15 @@ public interface Reply<T> {
public void onNotificationReceivedInBackground(@NonNull Map<Object, Object> withPayloadArg, @NonNull Reply<Void> callback) {
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(
binaryMessenger, "dev.flutter.pigeon.PushNotificationsFlutterApi.onNotificationReceivedInBackground", getCodec());
binaryMessenger, "dev.flutter.pigeon.amplify_push_notifications.PushNotificationsFlutterApi.onNotificationReceivedInBackground", getCodec());
channel.send(
new ArrayList<Object>(Collections.singletonList(withPayloadArg)),
channelReply -> callback.reply(null));
}
public void nullifyLaunchNotification(@NonNull Reply<Void> callback) {
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(
binaryMessenger, "dev.flutter.pigeon.PushNotificationsFlutterApi.nullifyLaunchNotification", getCodec());
binaryMessenger, "dev.flutter.pigeon.amplify_push_notifications.PushNotificationsFlutterApi.nullifyLaunchNotification", getCodec());
channel.send(
null,
channelReply -> callback.reply(null));
Expand Down Expand Up @@ -351,7 +351,7 @@ static void setup(@NonNull BinaryMessenger binaryMessenger, @Nullable PushNotifi
{
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(
binaryMessenger, "dev.flutter.pigeon.PushNotificationsHostApi.requestInitialToken", getCodec());
binaryMessenger, "dev.flutter.pigeon.amplify_push_notifications.PushNotificationsHostApi.requestInitialToken", getCodec());
if (api != null) {
channel.setMessageHandler(
(message, reply) -> {
Expand All @@ -373,7 +373,7 @@ static void setup(@NonNull BinaryMessenger binaryMessenger, @Nullable PushNotifi
{
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(
binaryMessenger, "dev.flutter.pigeon.PushNotificationsHostApi.getPermissionStatus", getCodec());
binaryMessenger, "dev.flutter.pigeon.amplify_push_notifications.PushNotificationsHostApi.getPermissionStatus", getCodec());
if (api != null) {
channel.setMessageHandler(
(message, reply) -> {
Expand All @@ -400,7 +400,7 @@ public void error(Throwable error) {
{
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(
binaryMessenger, "dev.flutter.pigeon.PushNotificationsHostApi.requestPermissions", getCodec());
binaryMessenger, "dev.flutter.pigeon.amplify_push_notifications.PushNotificationsHostApi.requestPermissions", getCodec());
if (api != null) {
channel.setMessageHandler(
(message, reply) -> {
Expand Down Expand Up @@ -429,7 +429,7 @@ public void error(Throwable error) {
{
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(
binaryMessenger, "dev.flutter.pigeon.PushNotificationsHostApi.getLaunchNotification", getCodec());
binaryMessenger, "dev.flutter.pigeon.amplify_push_notifications.PushNotificationsHostApi.getLaunchNotification", getCodec());
if (api != null) {
channel.setMessageHandler(
(message, reply) -> {
Expand All @@ -451,7 +451,7 @@ public void error(Throwable error) {
{
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(
binaryMessenger, "dev.flutter.pigeon.PushNotificationsHostApi.getBadgeCount", getCodec());
binaryMessenger, "dev.flutter.pigeon.amplify_push_notifications.PushNotificationsHostApi.getBadgeCount", getCodec());
if (api != null) {
channel.setMessageHandler(
(message, reply) -> {
Expand All @@ -473,7 +473,7 @@ public void error(Throwable error) {
{
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(
binaryMessenger, "dev.flutter.pigeon.PushNotificationsHostApi.setBadgeCount", getCodec());
binaryMessenger, "dev.flutter.pigeon.amplify_push_notifications.PushNotificationsHostApi.setBadgeCount", getCodec());
if (api != null) {
channel.setMessageHandler(
(message, reply) -> {
Expand All @@ -497,7 +497,7 @@ public void error(Throwable error) {
{
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(
binaryMessenger, "dev.flutter.pigeon.PushNotificationsHostApi.registerCallbackFunction", getCodec());
binaryMessenger, "dev.flutter.pigeon.amplify_push_notifications.PushNotificationsHostApi.registerCallbackFunction", getCodec());
if (api != null) {
channel.setMessageHandler(
(message, reply) -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// Autogenerated from Pigeon (v10.1.2), do not edit directly.
// Autogenerated from Pigeon (v10.1.6), do not edit directly.
// See also: https://pub.dev/packages/pigeon

#import <Foundation/Foundation.h>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// Autogenerated from Pigeon (v10.1.2), do not edit directly.
// Autogenerated from Pigeon (v10.1.6), do not edit directly.
// See also: https://pub.dev/packages/pigeon

#import "PushNotificationsNativePlugin.h"
Expand Down Expand Up @@ -162,7 +162,7 @@ - (instancetype)initWithBinaryMessenger:(NSObject<FlutterBinaryMessenger> *)bina
- (void)onNotificationReceivedInBackgroundWithPayload:(NSDictionary<id, id> *)arg_withPayload completion:(void (^)(FlutterError *_Nullable))completion {
FlutterBasicMessageChannel *channel =
[FlutterBasicMessageChannel
messageChannelWithName:@"dev.flutter.pigeon.PushNotificationsFlutterApi.onNotificationReceivedInBackground"
messageChannelWithName:@"dev.flutter.pigeon.amplify_push_notifications.PushNotificationsFlutterApi.onNotificationReceivedInBackground"
binaryMessenger:self.binaryMessenger
codec:PushNotificationsFlutterApiGetCodec()];
[channel sendMessage:@[arg_withPayload ?: [NSNull null]] reply:^(id reply) {
Expand All @@ -172,7 +172,7 @@ - (void)onNotificationReceivedInBackgroundWithPayload:(NSDictionary<id, id> *)ar
- (void)nullifyLaunchNotificationWithCompletion:(void (^)(FlutterError *_Nullable))completion {
FlutterBasicMessageChannel *channel =
[FlutterBasicMessageChannel
messageChannelWithName:@"dev.flutter.pigeon.PushNotificationsFlutterApi.nullifyLaunchNotification"
messageChannelWithName:@"dev.flutter.pigeon.amplify_push_notifications.PushNotificationsFlutterApi.nullifyLaunchNotification"
binaryMessenger:self.binaryMessenger
codec:PushNotificationsFlutterApiGetCodec()];
[channel sendMessage:nil reply:^(id reply) {
Expand Down Expand Up @@ -237,7 +237,7 @@ void PushNotificationsHostApiSetup(id<FlutterBinaryMessenger> binaryMessenger, N
{
FlutterBasicMessageChannel *channel =
[[FlutterBasicMessageChannel alloc]
initWithName:@"dev.flutter.pigeon.PushNotificationsHostApi.requestInitialToken"
initWithName:@"dev.flutter.pigeon.amplify_push_notifications.PushNotificationsHostApi.requestInitialToken"
binaryMessenger:binaryMessenger
codec:PushNotificationsHostApiGetCodec()];
if (api) {
Expand All @@ -254,7 +254,7 @@ void PushNotificationsHostApiSetup(id<FlutterBinaryMessenger> binaryMessenger, N
{
FlutterBasicMessageChannel *channel =
[[FlutterBasicMessageChannel alloc]
initWithName:@"dev.flutter.pigeon.PushNotificationsHostApi.getPermissionStatus"
initWithName:@"dev.flutter.pigeon.amplify_push_notifications.PushNotificationsHostApi.getPermissionStatus"
binaryMessenger:binaryMessenger
codec:PushNotificationsHostApiGetCodec()];
if (api) {
Expand All @@ -271,7 +271,7 @@ void PushNotificationsHostApiSetup(id<FlutterBinaryMessenger> binaryMessenger, N
{
FlutterBasicMessageChannel *channel =
[[FlutterBasicMessageChannel alloc]
initWithName:@"dev.flutter.pigeon.PushNotificationsHostApi.requestPermissions"
initWithName:@"dev.flutter.pigeon.amplify_push_notifications.PushNotificationsHostApi.requestPermissions"
binaryMessenger:binaryMessenger
codec:PushNotificationsHostApiGetCodec()];
if (api) {
Expand All @@ -290,7 +290,7 @@ void PushNotificationsHostApiSetup(id<FlutterBinaryMessenger> binaryMessenger, N
{
FlutterBasicMessageChannel *channel =
[[FlutterBasicMessageChannel alloc]
initWithName:@"dev.flutter.pigeon.PushNotificationsHostApi.getLaunchNotification"
initWithName:@"dev.flutter.pigeon.amplify_push_notifications.PushNotificationsHostApi.getLaunchNotification"
binaryMessenger:binaryMessenger
codec:PushNotificationsHostApiGetCodec()];
if (api) {
Expand All @@ -307,7 +307,7 @@ void PushNotificationsHostApiSetup(id<FlutterBinaryMessenger> binaryMessenger, N
{
FlutterBasicMessageChannel *channel =
[[FlutterBasicMessageChannel alloc]
initWithName:@"dev.flutter.pigeon.PushNotificationsHostApi.getBadgeCount"
initWithName:@"dev.flutter.pigeon.amplify_push_notifications.PushNotificationsHostApi.getBadgeCount"
binaryMessenger:binaryMessenger
codec:PushNotificationsHostApiGetCodec()];
if (api) {
Expand All @@ -324,7 +324,7 @@ void PushNotificationsHostApiSetup(id<FlutterBinaryMessenger> binaryMessenger, N
{
FlutterBasicMessageChannel *channel =
[[FlutterBasicMessageChannel alloc]
initWithName:@"dev.flutter.pigeon.PushNotificationsHostApi.setBadgeCount"
initWithName:@"dev.flutter.pigeon.amplify_push_notifications.PushNotificationsHostApi.setBadgeCount"
binaryMessenger:binaryMessenger
codec:PushNotificationsHostApiGetCodec()];
if (api) {
Expand All @@ -343,7 +343,7 @@ void PushNotificationsHostApiSetup(id<FlutterBinaryMessenger> binaryMessenger, N
{
FlutterBasicMessageChannel *channel =
[[FlutterBasicMessageChannel alloc]
initWithName:@"dev.flutter.pigeon.PushNotificationsHostApi.registerCallbackFunction"
initWithName:@"dev.flutter.pigeon.amplify_push_notifications.PushNotificationsHostApi.registerCallbackFunction"
binaryMessenger:binaryMessenger
codec:PushNotificationsHostApiGetCodec()];
if (api) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,13 @@ abstract class AmplifyPushNotifications
_onForegroundNotificationReceived = foregroundNotificationEventChannel
.receiveBroadcastStream()
.cast<Map<Object?, Object?>>()
.map((map) => map.cast<String, Object?>())
.map(PushNotificationMessage.fromJson);

_onNotificationOpened = notificationOpenedEventChannel
.receiveBroadcastStream()
.cast<Map<Object?, Object?>>()
.map((map) => map.cast<String, Object?>())
.map(PushNotificationMessage.fromJson);
}

Expand Down Expand Up @@ -243,7 +245,7 @@ abstract class AmplifyPushNotifications
final rawLaunchNotification = await _hostApi.getLaunchNotification();
if (rawLaunchNotification != null) {
final launchNotification =
PushNotificationMessage.fromJson(rawLaunchNotification);
PushNotificationMessage.fromJson(rawLaunchNotification.cast());
_launchNotification = launchNotification;
_flutterApi.onNullifyLaunchNotificationCallback = () {
_launchNotification = null;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class AmplifyPushNotificationsFlutterApi
Future<void> onNotificationReceivedInBackground(
Map<Object?, Object?> payload,
) async {
final notification = PushNotificationMessage.fromJson(payload);
final notification = PushNotificationMessage.fromJson(payload.cast());

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

0 comments on commit f09132c

Please sign in to comment.