diff --git a/README.md b/README.md
index 46f0b34..0eb8d64 100644
--- a/README.md
+++ b/README.md
@@ -91,7 +91,6 @@ dependencies {
package="com.reactnativeproject">
-
diff --git a/android/src/main/java/io/neson/react/notification/Notification.java b/android/src/main/java/io/neson/react/notification/Notification.java
index 66a2f38..2d5366d 100644
--- a/android/src/main/java/io/neson/react/notification/Notification.java
+++ b/android/src/main/java/io/neson/react/notification/Notification.java
@@ -15,16 +15,9 @@
import java.lang.System;
import java.net.URL;
-import java.util.HashMap;
-import java.util.Map;
import com.google.gson.Gson;
-import io.neson.react.notification.NotificationAttributes;
-import io.neson.react.notification.NotificationEventReceiver;
-import io.neson.react.notification.NotificationPublisher;
-
-import android.util.Base64;
import android.support.v7.app.NotificationCompat;
import android.text.Html;
import android.util.Base64;
diff --git a/android/src/main/java/io/neson/react/notification/NotificationEventReceiver.java b/android/src/main/java/io/neson/react/notification/NotificationEventReceiver.java
index 5fbdef3..7f51996 100644
--- a/android/src/main/java/io/neson/react/notification/NotificationEventReceiver.java
+++ b/android/src/main/java/io/neson/react/notification/NotificationEventReceiver.java
@@ -1,17 +1,11 @@
package io.neson.react.notification;
-import android.content.ComponentName;
-import android.os.Build;
import android.os.Bundle;
-import android.os.SystemClock;
-import android.app.ActivityManager;
-import android.app.ActivityManager.RunningAppProcessInfo;
+import android.app.Activity;
import android.content.Intent;
import android.content.Context;
import android.content.BroadcastReceiver;
-import java.util.List;
-
import android.util.Log;
/**
@@ -20,6 +14,7 @@
* Sends broadcast to the application, launches the app if needed.
*/
public class NotificationEventReceiver extends BroadcastReceiver {
+ final static String INTENT_ID = "io.neson.react.notification.NotificationEvent";
final static String NOTIFICATION_ID = "id";
final static String ACTION = "action";
final static String PAYLOAD = "payload";
@@ -27,61 +22,48 @@ public class NotificationEventReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
+ Log.i("ReactSystemNotification",
+ "NotificationEventReceiver: Received: " + extras.getString(ACTION) +
+ ", Notification ID: " + extras.getInt(NOTIFICATION_ID) +
+ ", payload: " + extras.getString(PAYLOAD));
+ sendBroadcast(context, extras);
+ }
- Log.i("ReactSystemNotification", "NotificationEventReceiver: Recived: " + extras.getString(ACTION) + ", Notification ID: " + extras.getInt(NOTIFICATION_ID) + ", payload: " + extras.getString(PAYLOAD));
-
- // If the application is not running or is not in foreground, start it with the notification
- // passed in
- if (!applicationIsRunning(context)) {
- String packageName = context.getApplicationContext().getPackageName();
- Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(packageName);
-
- launchIntent.putExtra("initialSysNotificationId", extras.getInt(NOTIFICATION_ID));
- launchIntent.putExtra("initialSysNotificationAction", extras.getString(ACTION));
- launchIntent.putExtra("initialSysNotificationPayload", extras.getString(PAYLOAD));
- launchIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
+ private void sendBroadcast(final Context context, final Bundle extras) {
+ Intent intent = new Intent(INTENT_ID);
- context.startActivity(launchIntent);
- Log.i("ReactSystemNotification", "NotificationEventReceiver: Launching: " + packageName);
- } else {
- sendBroadcast(context, extras); // If the application is already running in foreground, send a brodcast too
- }
- }
+ intent.putExtra("id", extras.getInt(NOTIFICATION_ID));
+ intent.putExtra("action", extras.getString(ACTION));
+ intent.putExtra("payload", extras.getString(PAYLOAD));
- private void sendBroadcast(Context context, Bundle extras) {
- Intent brodcastIntent = new Intent("NotificationEvent");
+ context.sendOrderedBroadcast(intent, null, new BroadcastReceiver() {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ int result = getResultCode();
- brodcastIntent.putExtra("id", extras.getInt(NOTIFICATION_ID));
- brodcastIntent.putExtra("action", extras.getString(ACTION));
- brodcastIntent.putExtra("payload", extras.getString(PAYLOAD));
+ if (result != Activity.RESULT_OK) {
+ launchApplication(context, extras);
+ }
+ }
+ }, null, Activity.RESULT_CANCELED, null, null);
- context.sendBroadcast(brodcastIntent);
- Log.v("ReactSystemNotification", "NotificationEventReceiver: Broadcast Sent: NotificationEvent: " + extras.getString(ACTION) + ", Notification ID: " + extras.getInt(NOTIFICATION_ID) + ", payload: " + extras.getString(PAYLOAD));
+ Log.v("ReactSystemNotification",
+ "NotificationEventReceiver: Broadcast Sent: NotificationEvent: " +
+ extras.getString(ACTION) +
+ ", Notification ID: " + extras.getInt(NOTIFICATION_ID) +
+ ", payload: " + extras.getString(PAYLOAD));
}
- private boolean applicationIsRunning(Context context) {
- ActivityManager activityManager = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
+ private void launchApplication(Context context, Bundle extras) {
+ String packageName = context.getApplicationContext().getPackageName();
+ Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(packageName);
- if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
- List processInfos = activityManager.getRunningAppProcesses();
- for (ActivityManager.RunningAppProcessInfo processInfo : processInfos) {
- if (processInfo.processName.equals(context.getApplicationContext().getPackageName())) {
- if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
- for (String d: processInfo.pkgList) {
- Log.v("ReactSystemNotification", "NotificationEventReceiver: ok: " + d);
- return true;
- }
- }
- }
- }
- } else {
- List taskInfo = activityManager.getRunningTasks(1);
- ComponentName componentInfo = taskInfo.get(0).topActivity;
- if (componentInfo.getPackageName().equals(context.getPackageName())) {
- return true;
- }
- }
+ launchIntent.putExtra("initialSysNotificationId", extras.getInt(NOTIFICATION_ID));
+ launchIntent.putExtra("initialSysNotificationAction", extras.getString(ACTION));
+ launchIntent.putExtra("initialSysNotificationPayload", extras.getString(PAYLOAD));
+ launchIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- return false;
+ context.startActivity(launchIntent);
+ Log.i("ReactSystemNotification", "NotificationEventReceiver: Launching: " + packageName);
}
}
diff --git a/android/src/main/java/io/neson/react/notification/NotificationManager.java b/android/src/main/java/io/neson/react/notification/NotificationManager.java
index 0c0f29a..fa1ab98 100644
--- a/android/src/main/java/io/neson/react/notification/NotificationManager.java
+++ b/android/src/main/java/io/neson/react/notification/NotificationManager.java
@@ -3,9 +3,6 @@
import android.content.Context;
import android.content.SharedPreferences;
-import io.neson.react.notification.Notification;
-import io.neson.react.notification.NotificationAttributes;
-
import java.util.ArrayList;
import java.util.Set;
diff --git a/android/src/main/java/io/neson/react/notification/NotificationModule.java b/android/src/main/java/io/neson/react/notification/NotificationModule.java
index bccde4c..7a0048d 100644
--- a/android/src/main/java/io/neson/react/notification/NotificationModule.java
+++ b/android/src/main/java/io/neson/react/notification/NotificationModule.java
@@ -7,9 +7,9 @@
import android.content.BroadcastReceiver;
import android.app.Activity;
+import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.bridge.ReactApplicationContext;
-import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Arguments;
@@ -21,15 +21,7 @@
import com.facebook.react.bridge.WritableNativeArray;
import com.facebook.react.bridge.LifecycleEventListener;
-import io.neson.react.notification.NotificationManager;
-import io.neson.react.notification.Notification;
-import io.neson.react.notification.NotificationAttributes;
-import io.neson.react.notification.NotificationEventReceiver;
-
import java.util.ArrayList;
-import java.util.Set;
-import java.util.HashMap;
-import java.util.Map;
import android.util.Log;
@@ -38,7 +30,7 @@
*
* Provides JS accessible API, bridge Java and JavaScript.
*/
-public class NotificationModule extends ReactContextBaseJavaModule {
+public class NotificationModule extends ReactContextBaseJavaModule implements LifecycleEventListener {
final static String PREFERENCES_KEY = "ReactNativeSystemNotification";
public Context mContext = null;
public NotificationManager mNotificationManager = null;
@@ -57,7 +49,7 @@ public NotificationModule(ReactApplicationContext reactContext) {
this.mContext = reactContext;
this.mNotificationManager = (NotificationManager) new NotificationManager(reactContext);
- listenNotificationEvent();
+ reactContext.addLifecycleEventListener(this);
}
/**
@@ -282,23 +274,37 @@ private NotificationAttributes getNotificationAttributesFromReadableMap(
return notificationAttributes;
}
- private void listenNotificationEvent() {
- IntentFilter intentFilter = new IntentFilter("NotificationEvent");
+ private IntentFilter intentFilter = new IntentFilter(NotificationEventReceiver.INTENT_ID);
+ private BroadcastReceiver intentReceiver = new BroadcastReceiver() {
+ @Override
+ public void onReceive(Context context, Intent intent) {
- getReactApplicationContext().registerReceiver(new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
+ Bundle extras = intent.getExtras();
- Bundle extras = intent.getExtras();
+ WritableMap params = Arguments.createMap();
+ params.putInt("notificationID", extras.getInt(NotificationEventReceiver.NOTIFICATION_ID));
+ params.putString("action", extras.getString(NotificationEventReceiver.ACTION));
+ params.putString("payload", extras.getString(NotificationEventReceiver.PAYLOAD));
- WritableMap params = Arguments.createMap();
- params.putInt("notificationID", extras.getInt(NotificationEventReceiver.NOTIFICATION_ID));
- params.putString("action", extras.getString(NotificationEventReceiver.ACTION));
- params.putString("payload", extras.getString(NotificationEventReceiver.PAYLOAD));
+ sendEvent("sysModuleNotificationClick", params);
- sendEvent("sysModuleNotificationClick", params);
- }
- }, intentFilter);
+ this.setResultCode(Activity.RESULT_OK);
+ }
+ };
+
+ // Activity lifecycle
+
+ public void onHostResume() {
+ mContext.registerReceiver(intentReceiver, intentFilter);
+ }
+
+ public void onHostPause() {
+ mContext.unregisterReceiver(intentReceiver);
+ }
+
+ public void onHostDestroy() {
+ // Nothing to do; intentReceiver will already have been unregistered by this point
+ // in the lifecycle.
}
}
diff --git a/android/src/main/java/io/neson/react/notification/NotificationPackage.java b/android/src/main/java/io/neson/react/notification/NotificationPackage.java
index 34d94b7..9e97343 100644
--- a/android/src/main/java/io/neson/react/notification/NotificationPackage.java
+++ b/android/src/main/java/io/neson/react/notification/NotificationPackage.java
@@ -1,12 +1,9 @@
package io.neson.react.notification;
-import android.app.Activity;
-
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.JavaScriptModule;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
-import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.uimanager.ViewManager;
import java.util.ArrayList;
import java.util.Arrays;
diff --git a/android/src/main/java/io/neson/react/notification/NotificationPublisher.java b/android/src/main/java/io/neson/react/notification/NotificationPublisher.java
index b0dfde6..1905d51 100644
--- a/android/src/main/java/io/neson/react/notification/NotificationPublisher.java
+++ b/android/src/main/java/io/neson/react/notification/NotificationPublisher.java
@@ -7,9 +7,6 @@
import java.lang.System;
import java.util.Calendar;
-import io.neson.react.notification.Notification;
-import io.neson.react.notification.NotificationManager;
-
import android.util.Log;
/**
diff --git a/android/src/main/java/io/neson/react/notification/SystemBootEventReceiver.java b/android/src/main/java/io/neson/react/notification/SystemBootEventReceiver.java
index b163102..c2177e7 100644
--- a/android/src/main/java/io/neson/react/notification/SystemBootEventReceiver.java
+++ b/android/src/main/java/io/neson/react/notification/SystemBootEventReceiver.java
@@ -3,13 +3,9 @@
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
-import android.content.SharedPreferences;
import java.util.ArrayList;
-import io.neson.react.notification.NotificationManager;
-import io.neson.react.notification.Notification;
-
import android.util.Log;
/**
@@ -23,7 +19,6 @@ public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
NotificationManager notificationManager = new NotificationManager(context);
- SharedPreferences sharedPreferences = context.getSharedPreferences(NotificationManager.PREFERENCES_KEY, Context.MODE_PRIVATE);
ArrayList ids = notificationManager.getIDs();