From 4591392ba1db61f50204abc539b22b7fabc0587d Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Mon, 1 Aug 2016 15:55:49 +0100 Subject: [PATCH 1/4] Corrected typo --- .../react/notification/NotificationEventReceiver.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) 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..5f4ef32 100644 --- a/android/src/main/java/io/neson/react/notification/NotificationEventReceiver.java +++ b/android/src/main/java/io/neson/react/notification/NotificationEventReceiver.java @@ -49,13 +49,13 @@ public void onReceive(Context context, Intent intent) { } private void sendBroadcast(Context context, Bundle extras) { - Intent brodcastIntent = new Intent("NotificationEvent"); + Intent intent = new Intent("NotificationEvent"); - brodcastIntent.putExtra("id", extras.getInt(NOTIFICATION_ID)); - brodcastIntent.putExtra("action", extras.getString(ACTION)); - brodcastIntent.putExtra("payload", extras.getString(PAYLOAD)); + intent.putExtra("id", extras.getInt(NOTIFICATION_ID)); + intent.putExtra("action", extras.getString(ACTION)); + intent.putExtra("payload", extras.getString(PAYLOAD)); - context.sendBroadcast(brodcastIntent); + context.sendBroadcast(intent); Log.v("ReactSystemNotification", "NotificationEventReceiver: Broadcast Sent: NotificationEvent: " + extras.getString(ACTION) + ", Notification ID: " + extras.getInt(NOTIFICATION_ID) + ", payload: " + extras.getString(PAYLOAD)); } From 9b682fb8106e7236d9d4f9e9b7705bc23f0b29e7 Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Mon, 1 Aug 2016 15:56:32 +0100 Subject: [PATCH 2/4] Removed unnecessary code --- .../io/neson/react/notification/SystemBootEventReceiver.java | 5 ----- 1 file changed, 5 deletions(-) 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(); From d72fb73922e1bd3c022a0d1e86499b01ffaa0e0b Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Mon, 1 Aug 2016 17:07:05 +0100 Subject: [PATCH 3/4] Removed some unused imports --- .../java/io/neson/react/notification/Notification.java | 7 ------- .../io/neson/react/notification/NotificationManager.java | 3 --- .../io/neson/react/notification/NotificationPackage.java | 3 --- .../io/neson/react/notification/NotificationPublisher.java | 3 --- 4 files changed, 16 deletions(-) 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/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/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; /** From 65b2a9fbe08173685b8b168de3ef4999db7f5890 Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Tue, 2 Aug 2016 12:15:29 +0100 Subject: [PATCH 4/4] Removed need for deprecated GET_TASKS permission Rewritten notification-click intent handling so that we don't need to check whether app is running or not. Instead, it sends the intent such that it can get an 'OK' response back if the app is running; if not, it'll start the app. --- README.md | 1 - .../NotificationEventReceiver.java | 90 ++++++++----------- .../notification/NotificationModule.java | 54 ++++++----- 3 files changed, 66 insertions(+), 79 deletions(-) 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/NotificationEventReceiver.java b/android/src/main/java/io/neson/react/notification/NotificationEventReceiver.java index 5f4ef32..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 intent = new Intent("NotificationEvent"); + context.sendOrderedBroadcast(intent, null, new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + int result = getResultCode(); - intent.putExtra("id", extras.getInt(NOTIFICATION_ID)); - intent.putExtra("action", extras.getString(ACTION)); - intent.putExtra("payload", extras.getString(PAYLOAD)); + if (result != Activity.RESULT_OK) { + launchApplication(context, extras); + } + } + }, null, Activity.RESULT_CANCELED, null, null); - context.sendBroadcast(intent); - 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/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. } }