|
1 | 1 | package io.neson.react.notification; |
2 | 2 |
|
3 | | -import android.content.ComponentName; |
4 | | -import android.os.Build; |
5 | 3 | 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; |
9 | 5 | import android.content.Intent; |
10 | 6 | import android.content.Context; |
11 | 7 | import android.content.BroadcastReceiver; |
12 | 8 |
|
13 | | -import java.util.List; |
14 | | - |
15 | 9 | import android.util.Log; |
16 | 10 |
|
17 | 11 | /** |
|
20 | 14 | * Sends broadcast to the application, launches the app if needed. |
21 | 15 | */ |
22 | 16 | public class NotificationEventReceiver extends BroadcastReceiver { |
| 17 | + final static String INTENT_ID = "io.neson.react.notification.NotificationEvent"; |
23 | 18 | final static String NOTIFICATION_ID = "id"; |
24 | 19 | final static String ACTION = "action"; |
25 | 20 | final static String PAYLOAD = "payload"; |
26 | 21 |
|
27 | 22 | @Override |
28 | 23 | public void onReceive(Context context, Intent intent) { |
29 | 24 | 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 | + } |
30 | 31 |
|
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); |
43 | 34 |
|
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)); |
50 | 38 |
|
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(); |
53 | 43 |
|
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); |
57 | 49 |
|
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)); |
60 | 55 | } |
61 | 56 |
|
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); |
64 | 60 |
|
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); |
84 | 65 |
|
85 | | - return false; |
| 66 | + context.startActivity(launchIntent); |
| 67 | + Log.i("ReactSystemNotification", "NotificationEventReceiver: Launching: " + packageName); |
86 | 68 | } |
87 | 69 | } |
0 commit comments