Skip to content

Commit 9f64d61

Browse files
author
Dan Brown
committed
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.
1 parent 46cde8f commit 9f64d61

File tree

3 files changed

+66
-79
lines changed

3 files changed

+66
-79
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ dependencies {
9191
package="com.reactnativeproject">
9292

9393
<uses-permission android:name="android.permission.INTERNET" />
94-
<uses-permission android:name="android.permission.GET_TASKS" /> <!-- <- Add this line -->
9594
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <!-- <- Add this line -->
9695
<uses-permission android:name="android.permission.VIBRATE"/> <!-- <- Add this line -->
9796

Lines changed: 36 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
package io.neson.react.notification;
22

3-
import android.content.ComponentName;
4-
import android.os.Build;
53
import android.os.Bundle;
6-
import android.os.SystemClock;
7-
import android.app.ActivityManager;
8-
import android.app.ActivityManager.RunningAppProcessInfo;
4+
import android.app.Activity;
95
import android.content.Intent;
106
import android.content.Context;
117
import android.content.BroadcastReceiver;
128

13-
import java.util.List;
14-
159
import android.util.Log;
1610

1711
/**
@@ -20,68 +14,56 @@
2014
* Sends broadcast to the application, launches the app if needed.
2115
*/
2216
public class NotificationEventReceiver extends BroadcastReceiver {
17+
final static String INTENT_ID = "io.neson.react.notification.NotificationEvent";
2318
final static String NOTIFICATION_ID = "id";
2419
final static String ACTION = "action";
2520
final static String PAYLOAD = "payload";
2621

2722
@Override
2823
public void onReceive(Context context, Intent intent) {
2924
Bundle extras = intent.getExtras();
25+
Log.i("ReactSystemNotification",
26+
"NotificationEventReceiver: Received: " + extras.getString(ACTION) +
27+
", Notification ID: " + extras.getInt(NOTIFICATION_ID) +
28+
", payload: " + extras.getString(PAYLOAD));
29+
sendBroadcast(context, extras);
30+
}
3031

31-
Log.i("ReactSystemNotification", "NotificationEventReceiver: Recived: " + extras.getString(ACTION) + ", Notification ID: " + extras.getInt(NOTIFICATION_ID) + ", payload: " + extras.getString(PAYLOAD));
32-
33-
// If the application is not running or is not in foreground, start it with the notification
34-
// passed in
35-
if (!applicationIsRunning(context)) {
36-
String packageName = context.getApplicationContext().getPackageName();
37-
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(packageName);
38-
39-
launchIntent.putExtra("initialSysNotificationId", extras.getInt(NOTIFICATION_ID));
40-
launchIntent.putExtra("initialSysNotificationAction", extras.getString(ACTION));
41-
launchIntent.putExtra("initialSysNotificationPayload", extras.getString(PAYLOAD));
42-
launchIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
32+
private void sendBroadcast(final Context context, final Bundle extras) {
33+
Intent intent = new Intent(INTENT_ID);
4334

44-
context.startActivity(launchIntent);
45-
Log.i("ReactSystemNotification", "NotificationEventReceiver: Launching: " + packageName);
46-
} else {
47-
sendBroadcast(context, extras); // If the application is already running in foreground, send a brodcast too
48-
}
49-
}
35+
intent.putExtra("id", extras.getInt(NOTIFICATION_ID));
36+
intent.putExtra("action", extras.getString(ACTION));
37+
intent.putExtra("payload", extras.getString(PAYLOAD));
5038

51-
private void sendBroadcast(Context context, Bundle extras) {
52-
Intent intent = new Intent("NotificationEvent");
39+
context.sendOrderedBroadcast(intent, null, new BroadcastReceiver() {
40+
@Override
41+
public void onReceive(Context context, Intent intent) {
42+
int result = getResultCode();
5343

54-
intent.putExtra("id", extras.getInt(NOTIFICATION_ID));
55-
intent.putExtra("action", extras.getString(ACTION));
56-
intent.putExtra("payload", extras.getString(PAYLOAD));
44+
if (result != Activity.RESULT_OK) {
45+
launchApplication(context, extras);
46+
}
47+
}
48+
}, null, Activity.RESULT_CANCELED, null, null);
5749

58-
context.sendBroadcast(intent);
59-
Log.v("ReactSystemNotification", "NotificationEventReceiver: Broadcast Sent: NotificationEvent: " + extras.getString(ACTION) + ", Notification ID: " + extras.getInt(NOTIFICATION_ID) + ", payload: " + extras.getString(PAYLOAD));
50+
Log.v("ReactSystemNotification",
51+
"NotificationEventReceiver: Broadcast Sent: NotificationEvent: " +
52+
extras.getString(ACTION) +
53+
", Notification ID: " + extras.getInt(NOTIFICATION_ID) +
54+
", payload: " + extras.getString(PAYLOAD));
6055
}
6156

62-
private boolean applicationIsRunning(Context context) {
63-
ActivityManager activityManager = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
57+
private void launchApplication(Context context, Bundle extras) {
58+
String packageName = context.getApplicationContext().getPackageName();
59+
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(packageName);
6460

65-
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
66-
List<RunningAppProcessInfo> processInfos = activityManager.getRunningAppProcesses();
67-
for (ActivityManager.RunningAppProcessInfo processInfo : processInfos) {
68-
if (processInfo.processName.equals(context.getApplicationContext().getPackageName())) {
69-
if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
70-
for (String d: processInfo.pkgList) {
71-
Log.v("ReactSystemNotification", "NotificationEventReceiver: ok: " + d);
72-
return true;
73-
}
74-
}
75-
}
76-
}
77-
} else {
78-
List<ActivityManager.RunningTaskInfo> taskInfo = activityManager.getRunningTasks(1);
79-
ComponentName componentInfo = taskInfo.get(0).topActivity;
80-
if (componentInfo.getPackageName().equals(context.getPackageName())) {
81-
return true;
82-
}
83-
}
61+
launchIntent.putExtra("initialSysNotificationId", extras.getInt(NOTIFICATION_ID));
62+
launchIntent.putExtra("initialSysNotificationAction", extras.getString(ACTION));
63+
launchIntent.putExtra("initialSysNotificationPayload", extras.getString(PAYLOAD));
64+
launchIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
8465

85-
return false;
66+
context.startActivity(launchIntent);
67+
Log.i("ReactSystemNotification", "NotificationEventReceiver: Launching: " + packageName);
8668
}
8769
}

android/src/main/java/io/neson/react/notification/NotificationModule.java

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
import android.content.BroadcastReceiver;
88
import android.app.Activity;
99

10+
import com.facebook.react.bridge.LifecycleEventListener;
1011
import com.facebook.react.modules.core.DeviceEventManagerModule;
1112
import com.facebook.react.bridge.ReactApplicationContext;
12-
import com.facebook.react.bridge.ReactContext;
1313
import com.facebook.react.bridge.ReactContextBaseJavaModule;
1414
import com.facebook.react.bridge.ReactMethod;
1515
import com.facebook.react.bridge.Arguments;
@@ -20,15 +20,7 @@
2020
import com.facebook.react.bridge.WritableArray;
2121
import com.facebook.react.bridge.WritableNativeArray;
2222

23-
import io.neson.react.notification.NotificationManager;
24-
import io.neson.react.notification.Notification;
25-
import io.neson.react.notification.NotificationAttributes;
26-
import io.neson.react.notification.NotificationEventReceiver;
27-
2823
import java.util.ArrayList;
29-
import java.util.Set;
30-
import java.util.HashMap;
31-
import java.util.Map;
3224

3325
import android.util.Log;
3426

@@ -37,7 +29,7 @@
3729
*
3830
* Provides JS accessible API, bridge Java and JavaScript.
3931
*/
40-
public class NotificationModule extends ReactContextBaseJavaModule {
32+
public class NotificationModule extends ReactContextBaseJavaModule implements LifecycleEventListener {
4133
final static String PREFERENCES_KEY = "ReactNativeSystemNotification";
4234
public Context mContext = null;
4335
public NotificationManager mNotificationManager = null;
@@ -56,7 +48,7 @@ public NotificationModule(ReactApplicationContext reactContext) {
5648
this.mContext = reactContext;
5749
this.mNotificationManager = (NotificationManager) new NotificationManager(reactContext);
5850

59-
listenNotificationEvent();
51+
reactContext.addLifecycleEventListener(this);
6052
}
6153

6254
/**
@@ -281,22 +273,36 @@ private NotificationAttributes getNotificationAttributesFromReadableMap(
281273
return notificationAttributes;
282274
}
283275

284-
private void listenNotificationEvent() {
285-
IntentFilter intentFilter = new IntentFilter("NotificationEvent");
276+
private IntentFilter intentFilter = new IntentFilter(NotificationEventReceiver.INTENT_ID);
277+
private BroadcastReceiver intentReceiver = new BroadcastReceiver() {
278+
@Override
279+
public void onReceive(Context context, Intent intent) {
286280

287-
getReactApplicationContext().registerReceiver(new BroadcastReceiver() {
288-
@Override
289-
public void onReceive(Context context, Intent intent) {
281+
Bundle extras = intent.getExtras();
290282

291-
Bundle extras = intent.getExtras();
283+
WritableMap params = Arguments.createMap();
284+
params.putInt("notificationID", extras.getInt(NotificationEventReceiver.NOTIFICATION_ID));
285+
params.putString("action", extras.getString(NotificationEventReceiver.ACTION));
286+
params.putString("payload", extras.getString(NotificationEventReceiver.PAYLOAD));
292287

293-
WritableMap params = Arguments.createMap();
294-
params.putInt("notificationID", extras.getInt(NotificationEventReceiver.NOTIFICATION_ID));
295-
params.putString("action", extras.getString(NotificationEventReceiver.ACTION));
296-
params.putString("payload", extras.getString(NotificationEventReceiver.PAYLOAD));
288+
sendEvent("sysModuleNotificationClick", params);
297289

298-
sendEvent("sysModuleNotificationClick", params);
299-
}
300-
}, intentFilter);
290+
this.setResultCode(Activity.RESULT_OK);
291+
}
292+
};
293+
294+
// Activity lifecycle
295+
296+
public void onHostResume() {
297+
mContext.registerReceiver(intentReceiver, intentFilter);
298+
}
299+
300+
public void onHostPause() {
301+
mContext.unregisterReceiver(intentReceiver);
302+
}
303+
304+
public void onHostDestroy() {
305+
// Nothing to do; intentReceiver will already have been unregistered by this point
306+
// in the lifecycle.
301307
}
302308
}

0 commit comments

Comments
 (0)