Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
38 changes: 33 additions & 5 deletions android/src/main/java/com/oney/gcm/GcmModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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);

Expand All @@ -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
Expand Down
21 changes: 20 additions & 1 deletion index.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

Expand Down Expand Up @@ -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;
}
Expand Down