Skip to content

Commit 65b2a9f

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 d72fb73 commit 65b2a9f

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;
@@ -21,15 +21,7 @@
2121
import com.facebook.react.bridge.WritableNativeArray;
2222
import com.facebook.react.bridge.LifecycleEventListener;
2323

24-
import io.neson.react.notification.NotificationManager;
25-
import io.neson.react.notification.Notification;
26-
import io.neson.react.notification.NotificationAttributes;
27-
import io.neson.react.notification.NotificationEventReceiver;
28-
2924
import java.util.ArrayList;
30-
import java.util.Set;
31-
import java.util.HashMap;
32-
import java.util.Map;
3325

3426
import android.util.Log;
3527

@@ -38,7 +30,7 @@
3830
*
3931
* Provides JS accessible API, bridge Java and JavaScript.
4032
*/
41-
public class NotificationModule extends ReactContextBaseJavaModule {
33+
public class NotificationModule extends ReactContextBaseJavaModule implements LifecycleEventListener {
4234
final static String PREFERENCES_KEY = "ReactNativeSystemNotification";
4335
public Context mContext = null;
4436
public NotificationManager mNotificationManager = null;
@@ -57,7 +49,7 @@ public NotificationModule(ReactApplicationContext reactContext) {
5749
this.mContext = reactContext;
5850
this.mNotificationManager = (NotificationManager) new NotificationManager(reactContext);
5951

60-
listenNotificationEvent();
52+
reactContext.addLifecycleEventListener(this);
6153
}
6254

6355
/**
@@ -282,23 +274,37 @@ private NotificationAttributes getNotificationAttributesFromReadableMap(
282274
return notificationAttributes;
283275
}
284276

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

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

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

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

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

304310
}

0 commit comments

Comments
 (0)