From f75c2f4cbb0b61d393a7206980a9c3d78c9ef06c Mon Sep 17 00:00:00 2001 From: Jawad Rehman Date: Fri, 25 Dec 2015 19:12:09 +0000 Subject: [PATCH] added popInitialNotification function --- README.md | 14 +++++++ .../src/main/java/com/oney/gcm/GcmModule.java | 38 ++++++++++++++++--- index.android.js | 21 +++++++++- 3 files changed, 67 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 8356d3c..a97601a 100644 --- a/README.md +++ b/README.md @@ -179,6 +179,20 @@ There are two situations. ##### The app is killed/closed `GcmAndroid.launchNotification` is your GCM data. You can create notification with resolving the data by using [react-native-system-notification module](https://github.com/Neson/react-native-system-notification). + +### popInitialNotification Usage + +GcmAndroid.addEventListener('popInitialNotification', function(data){ + if(data.popInitialNotification != null){ + //Do things! + } + + }); ## Troubleshoot - Do not add `multiDexEnabled true` in `android/app/build.gradle` even encounter `com.android.dex.DexException: Multiple dex files...` failure. +- If you encounter com.android.dex.DexException: Multiple dex files.. Do the following +cd android +./gradlew clean +cd ../ +react-native-start diff --git a/android/src/main/java/com/oney/gcm/GcmModule.java b/android/src/main/java/com/oney/gcm/GcmModule.java index 0f9e5b9..3d205cb 100644 --- a/android/src/main/java/com/oney/gcm/GcmModule.java +++ b/android/src/main/java/com/oney/gcm/GcmModule.java @@ -41,6 +41,10 @@ import android.media.RingtoneManager; import android.net.Uri; +//BEGIN VOX EDIT// +import java.util.Random; +//END VOX EDIT// + public class GcmModule extends ReactContextBaseJavaModule implements LifecycleEventListener { private final static String TAG = GcmModule.class.getCanonicalName(); private ReactContext mReactContext; @@ -177,6 +181,20 @@ private Class getMainActivityClass() { } } + @ReactMethod + /** + Returns the notification tapped when the app has been opened via a notification + + --BEGIN VOXMARKETS EDIT + */ + public void popInitialNotification() { + Intent intent =mActivity.getIntent(); + WritableMap params = Arguments.createMap(); + params.putString("popInitialNotification", intent.getStringExtra("payload")); + sendEvent("popInitialNotification", params); + } + //-- END VOX MARKETS EDIT// + @ReactMethod public void createNotification(ReadableMap infos) { Resources resources = mReactContext.getResources(); @@ -195,9 +213,17 @@ public void createNotification(ReadableMap infos) { int resourceId = resources.getIdentifier(infos.getString("largeIcon"), "mipmap", packageName); Intent intent = new Intent(mReactContext, intentClass); - intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); - PendingIntent pendingIntent = PendingIntent.getActivity(mReactContext, 0, intent, - PendingIntent.FLAG_ONE_SHOT); + //BEGIN VOX EDIT// + //add payload to intent. + if(infos.hasKey("payload")){ + intent.putExtra("payload", infos.getString("payload")); + } + //Add random interger to pending intent to allow multiple notifications being added when the app is closed. + Random rand = new Random(); + int randomInteger = rand.nextInt(10); + + PendingIntent pendingIntent = PendingIntent.getActivity(mReactContext, randomInteger, intent, PendingIntent.FLAG_ONE_SHOT); + //END VOX EDIT// Bitmap largeIcon = BitmapFactory.decodeResource(resources, resourceId); @@ -223,8 +249,10 @@ public void createNotification(ReadableMap infos) { notif.defaults |= Notification.DEFAULT_VIBRATE; notif.defaults |= Notification.DEFAULT_SOUND; notif.defaults |= Notification.DEFAULT_LIGHTS; - - notificationManager.notify(0, notif); + //BEGIN VOX EDIT// + //Add random interger to notificationmanager to allow multiple notifications being added when the app is closed. + notificationManager.notify(randomInteger, notif); + //END VOX EDIT// } @Override diff --git a/index.android.js b/index.android.js index 9a2aa01..fc1fc53 100644 --- a/index.android.js +++ b/index.android.js @@ -10,7 +10,10 @@ var _notifHandlers = new Map(); var DEVICE_NOTIF_EVENT = 'remoteNotificationReceived'; var NOTIF_REGISTER_EVENT = 'remoteNotificationsRegistered'; - +//BEGIN VOX MARKETS EDIT/ +//EVENT FOR POPINITIALEVENT +var POP_INITIAL_EVENT = 'popInitialNotification'; +//END VOX MARKETS EDIT/ class GcmAndroid { static addEventListener(type: string, handler: Function) { var listener; @@ -31,6 +34,14 @@ class GcmAndroid { } ); } + //BEGIN VOX MARKETS EDIT// + //ADD LISTENER TO RETURN POPINITIALEVENT + else if(type == POP_INITIAL_EVENT){ + listener = DeviceEventEmitter.addListener(POP_INITIAL_EVENT, (popInitialNotification)=>{ + handler(popInitialNotification); + }); + } + //END VOX MARKETS EDIT// _notifHandlers.set(handler, listener); } @@ -59,6 +70,14 @@ class GcmAndroid { _notifHandlers.delete(handler); } + //BEGIN VOX MARKETS EDIT// + //CALL JAVA FUNCTION DEFINED TO GET INITIAL NOTIFICATION + static popInitialNotification(){ + GcmModule.popInitialNotification(); + } + //END VOX MARKETS EDIT// + + constructor(data) { this.data = data; }