From 731cc93bd44d0d8e385491a59f6c2ff4ad81f74d Mon Sep 17 00:00:00 2001 From: yogevbd Date: Mon, 4 Feb 2019 17:28:46 +0200 Subject: [PATCH] Verify FCM notification intent --- .../RNNotificationsModule.java | 4 +++- .../RNNotificationsPackage.java | 13 ++++++++----- .../core/notification/PushNotification.java | 12 ++++++++++++ .../gcm/FcmInstanceIdListenerService.java | 13 +------------ package.json | 2 +- 5 files changed, 25 insertions(+), 19 deletions(-) diff --git a/android/src/main/java/com/wix/reactnativenotifications/RNNotificationsModule.java b/android/src/main/java/com/wix/reactnativenotifications/RNNotificationsModule.java index dd38c7d..6588ce0 100644 --- a/android/src/main/java/com/wix/reactnativenotifications/RNNotificationsModule.java +++ b/android/src/main/java/com/wix/reactnativenotifications/RNNotificationsModule.java @@ -65,7 +65,9 @@ public class RNNotificationsModule extends ReactContextBaseJavaModule implements Bundle notificationData = intent.getExtras(); if (notificationData != null) { final IPushNotification notification = PushNotification.get(getReactApplicationContext().getApplicationContext(), notificationData); - notification.onOpened(); + if (notification != null) { + notification.onOpened(); + } } } diff --git a/android/src/main/java/com/wix/reactnativenotifications/RNNotificationsPackage.java b/android/src/main/java/com/wix/reactnativenotifications/RNNotificationsPackage.java index 26748a1..d280fe7 100644 --- a/android/src/main/java/com/wix/reactnativenotifications/RNNotificationsPackage.java +++ b/android/src/main/java/com/wix/reactnativenotifications/RNNotificationsPackage.java @@ -63,11 +63,14 @@ public class RNNotificationsPackage implements ReactPackage, AppLifecycleFacade. final IPushNotificationsDrawer notificationsDrawer = PushNotificationsDrawer.get(mApplication.getApplicationContext()); notificationsDrawer.onNewActivity(activity); - Bundle notificationData = activity.getIntent().getExtras(); - if (notificationData != null) { - final IPushNotification pushNotification = PushNotification.get(mApplication.getApplicationContext(), notificationData); - if (pushNotification != null) { - pushNotification.onOpened(); + Intent intent = activity.getIntent(); + if (intent != null) { + Bundle notificationData = intent.getExtras(); + if (notificationData != null) { + final IPushNotification pushNotification = PushNotification.get(mApplication.getApplicationContext(), notificationData); + if (pushNotification != null) { + pushNotification.onOpened(); + } } } } diff --git a/android/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotification.java b/android/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotification.java index 356faed..ba5e1c3 100644 --- a/android/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotification.java +++ b/android/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotification.java @@ -43,6 +43,10 @@ public class PushNotification implements IPushNotification { }; public static IPushNotification get(Context context, Bundle bundle) { + if (verifyNotificationBundle(bundle) == false) { + return null; + } + Context appContext = context.getApplicationContext(); if (appContext instanceof INotificationsApplication) { return ((INotificationsApplication) appContext).getPushNotification(context, bundle, AppLifecycleFacadeHolder.get(), new AppLaunchHelper()); @@ -58,6 +62,14 @@ public class PushNotification implements IPushNotification { mNotificationProps = createProps(bundle); } + private static boolean verifyNotificationBundle(Bundle bundle) { + if (bundle.getString("google.message_id") != null) { + return true; + } + + return false; + } + @Override public void onReceived() throws InvalidNotificationException { postNotification(null); diff --git a/android/src/main/java/com/wix/reactnativenotifications/gcm/FcmInstanceIdListenerService.java b/android/src/main/java/com/wix/reactnativenotifications/gcm/FcmInstanceIdListenerService.java index cb7763c..2456913 100644 --- a/android/src/main/java/com/wix/reactnativenotifications/gcm/FcmInstanceIdListenerService.java +++ b/android/src/main/java/com/wix/reactnativenotifications/gcm/FcmInstanceIdListenerService.java @@ -21,8 +21,7 @@ public class FcmInstanceIdListenerService extends FirebaseMessagingService { @Override public void onMessageReceived(RemoteMessage message){ - Map data = message.getData(); - Bundle bundle = convertMapToBundle(data); + Bundle bundle = message.toIntent().getExtras(); Log.d(LOGTAG, "New message from GCM: " + bundle); try { @@ -33,14 +32,4 @@ public class FcmInstanceIdListenerService extends FirebaseMessagingService { Log.v(LOGTAG, "GCM message handling aborted", e); } } - - private Bundle convertMapToBundle(Map map) { - Bundle bundle = new Bundle(); - for (Map.Entry entry : map.entrySet()) { - bundle.putString(entry.getKey(), entry.getValue()); - } - - return bundle; - } - } diff --git a/package.json b/package.json index 6fb9d90..26976c3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-notifications", - "version": "1.2.5", + "version": "1.2.51", "description": "Advanced Push Notifications (Silent, interactive notifications) for iOS & Android", "author": "Lidan Hifi ", "license": "MIT", -- 2.26.2