Skip to content

Commit 7d8a8df

Browse files
committed
whitelist performance update
- better null whitelist handling - some debug logs - updated readme
1 parent e7a478f commit 7d8a8df

File tree

5 files changed

+49
-31
lines changed

5 files changed

+49
-31
lines changed

README.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ It is a replacement of [https://github.com/Alone2/capacitor-notificationlistener
77
This plugin works on Android 14 and adds few additional features like persistent notifications caching.
88
Tested on Capacitor v6.
99

10+
**Note: Plugin is in active development, bugs are to be expected, especially in background processing service.**
11+
12+
Background service autostart is TODO, right now it only works on reboot if you start your app manually. Caching works when main app is killed, but for now there is no prevention from android killing the service. Not happened to me during testing though.
13+
1014
## Install
1115

1216
```bash
@@ -224,14 +228,15 @@ removeAllListeners() => Promise<void>
224228
### replacePackagesWhitelist(...)
225229

226230
```typescript
227-
replacePackagesWhitelist(options: { packagesWhitelist: string[]; }) => Promise<void>
231+
replacePackagesWhitelist(options: { packagesWhitelist: string[] | null; }) => Promise<void>
228232
```
229233

230234
Replace the current white list of packages with new one.
235+
send null to disable whitelist.
231236

232-
| Param | Type |
233-
| ------------- | --------------------------------------------- |
234-
| **`options`** | <code>{ packagesWhitelist: string[]; }</code> |
237+
| Param | Type |
238+
| ------------- | ----------------------------------------------------- |
239+
| **`options`** | <code>{ packagesWhitelist: string[] \| null; }</code> |
235240

236241
--------------------
237242

@@ -260,9 +265,9 @@ Replace the current white list of packages with new one.
260265

261266
#### ListenerOptions
262267

263-
| Prop | Type |
264-
| ------------------------ | --------------------- |
265-
| **`cacheNotifications`** | <code>boolean</code> |
266-
| **`packagesWhitelist`** | <code>string[]</code> |
268+
| Prop | Type |
269+
| ------------------------ | ----------------------------- |
270+
| **`cacheNotifications`** | <code>boolean</code> |
271+
| **`packagesWhitelist`** | <code>string[] \| null</code> |
267272

268273
</docgen-api>

android/src/main/java/com/capacitor/notifications/listener/NotificationService.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import android.service.notification.StatusBarNotification;
77
import android.util.Log;
88

9+
import java.util.ArrayList;
10+
import java.util.Objects;
11+
912
public class NotificationService extends NotificationListenerService {
1013
public static final String ACTION_RECEIVE = "com.capacitor.notifications.listener.NOTIFICATION_RECEIVE_EVENT";
1114
public static final String ACTION_REMOVE = "com.capacitor.notifications.listener.NOTIFICATION_REMOVE_EVENT";
@@ -21,6 +24,8 @@ public class NotificationService extends NotificationListenerService {
2124
// TODO hold the 'old' app context?
2225
public static NotificationsListenerPlugin.NotificationReceiver notificationReceiver;
2326
public static boolean isConnected = false;
27+
// TODO: persist whitelist for autostart
28+
public static ArrayList<String> packagesWhitelist = null;
2429

2530
private static final String TAG = NotificationService.class.getSimpleName();
2631

@@ -33,14 +38,18 @@ public void onDestroy() {
3338

3439
@Override
3540
public void onNotificationPosted(StatusBarNotification sbn) {
36-
Intent i = notificationToIntent(sbn, ACTION_RECEIVE);
37-
sendBroadcast(i);
41+
if (packagesWhitelist == null || existsInWhitelist(sbn)) {
42+
Intent i = notificationToIntent(sbn, ACTION_RECEIVE);
43+
sendBroadcast(i);
44+
}
3845
}
3946

4047
@Override
4148
public void onNotificationRemoved(StatusBarNotification sbn) {
42-
Intent i = notificationToIntent(sbn, ACTION_REMOVE);
43-
sendBroadcast(i);
49+
if (packagesWhitelist == null || existsInWhitelist(sbn)) {
50+
Intent i = notificationToIntent(sbn, ACTION_REMOVE);
51+
sendBroadcast(i);
52+
}
4453
}
4554

4655
@Override
@@ -74,6 +83,9 @@ private Intent notificationToIntent(StatusBarNotification sbn, String action) {
7483

7584
i.putExtra(ARG_TIME, n.when);
7685

86+
if (Objects.equals(action, ACTION_RECEIVE)) {
87+
Log.d(TAG, "Received notification: " + text);
88+
}
7789
return i;
7890
}
7991

@@ -85,11 +97,19 @@ private String[] charSequenceArrayToStringArray(CharSequence[] c) {
8597
if (c == null) return new String[0];
8698
String[] out = new String[c.length];
8799
for (int i = 0; i < c.length; i++) {
88-
Log.d(TAG, String.valueOf(c[i]));
89100
out[i] = charSequenceToString(c[i]);
90101
}
91102
return out;
92103
}
104+
105+
private boolean existsInWhitelist(StatusBarNotification notification) {
106+
String packageName = notification.getPackageName();
107+
boolean exists = packagesWhitelist.contains(packageName);
108+
if (!exists) {
109+
Log.d(TAG, "Package not in whitelist: " + packageName);
110+
}
111+
return exists;
112+
}
93113
}
94114

95115

android/src/main/java/com/capacitor/notifications/listener/NotificationsListenerPlugin.java

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,19 @@ protected void handleOnDestroy() {
4949
Log.d(TAG, "Destroyed");
5050
}
5151

52+
// TODO: test if reinstalling the app will properly register new listener
5253
@SuppressLint("UnspecifiedRegisterReceiverFlag")
5354
@PluginMethod
5455
public void startListening(PluginCall call) {
5556
Boolean cacheEnabledValue = call.getBoolean("cacheNotifications");
56-
// TODO: persist whitelist for autostart
5757
ArrayList<String> packagesWhitelist = arrayFromPluginCall(call);
5858
if (packagesWhitelist != null) {
5959
Log.d(TAG, "Listening to packages: " + packagesWhitelist.toString());
6060
}
6161

6262
cacheEnabled = (cacheEnabledValue != null) ? cacheEnabledValue : false;
6363
if (NotificationService.notificationReceiver != null) {
64-
Log.d(TAG, "NotificationReceiver already exists");
64+
Log.d(TAG, "NotificationReceiver already exists, unregistering");
6565
NotificationService.notificationReceiver.unregister(getContext());
6666
}
6767
NotificationService.notificationReceiver = new NotificationReceiver(cacheEnabled, webViewActive, packagesWhitelist);
@@ -174,14 +174,13 @@ public class NotificationReceiver extends BroadcastReceiver {
174174
public boolean isRegistered;
175175
private boolean cacheEnabled;
176176
private boolean webViewActive;
177-
private ArrayList<String> packagesWhitelist;
178177

179178
private NotificationReceiver(boolean cacheEnabled, boolean webViewActive, ArrayList<String> packagesWhitelist) {
180179
Log.d(TAG, "NotificationReceiver created");
181180
this.isRegistered = false;
182181
this.cacheEnabled = cacheEnabled;
183182
this.webViewActive = webViewActive;
184-
this.packagesWhitelist = packagesWhitelist;
183+
NotificationService.packagesWhitelist = packagesWhitelist;
185184
}
186185

187186
/**
@@ -236,7 +235,7 @@ public void setWebViewActive(boolean webViewActive) {
236235
}
237236

238237
public void setPackagesWhitelist(ArrayList<String> packagesWhitelist) {
239-
this.packagesWhitelist = packagesWhitelist;
238+
NotificationService.packagesWhitelist = packagesWhitelist;
240239
}
241240

242241
@Override
@@ -245,19 +244,17 @@ public void onReceive(Context context, Intent intent) {
245244
Log.w(TAG, "Unregistered receiver is still registered in Android. Hope it kills it");
246245
return;
247246
}
248-
if (packagesWhitelist != null && !existsInWhitelist(intent)) {
249-
return;
250-
}
251247
JSObject jo = parseNotification(intent);
252248
switch (Objects.requireNonNull(intent.getAction())) {
253249
case NotificationService.ACTION_RECEIVE:
254250
// log original intent
255-
Log.d(TAG, "Received notification: " + jo.toString());
256251
if (cacheEnabled && !webViewActive) {
257252
Log.d(TAG, "Caching notification");
258253
persistentStorage.append(STORAGE_KEY, jo);
259254
Log.d(TAG, "New cache size: " + persistentStorage.size(STORAGE_KEY));
260255
return;
256+
} else if (!cacheEnabled && !webViewActive) {
257+
Log.d(TAG, "Cache disabled, not caching notification in bg");
261258
}
262259
notifyListeners(EVENT_NOTIFICATION_RECEIVED, jo);
263260
break;
@@ -285,10 +282,5 @@ private JSObject parseNotification(Intent intent) {
285282
}
286283
return jo;
287284
}
288-
289-
private boolean existsInWhitelist(Intent intent) {
290-
String packageName = intent.getStringExtra(NotificationService.ARG_PACKAGE);
291-
return packagesWhitelist.contains(packageName);
292-
}
293285
}
294286
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "capacitor-notifications-listener",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"description": "Plugin for reading all android notifications in capacitor.",
55
"main": "dist/plugin.cjs.js",
66
"module": "dist/esm/index.js",

src/definitions.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,17 @@ export interface NotificationsListenerPlugin {
2727
removeAllListeners(): Promise<void>;
2828
/**
2929
* Replace the current white list of packages with new one.
30+
* send null to disable whitelist.
3031
*/
3132
replacePackagesWhitelist(options: {
32-
packagesWhitelist: string[];
33+
packagesWhitelist: string[] | null;
3334
}): Promise<void>;
3435
}
3536

3637
export interface ListenerOptions {
3738
cacheNotifications?: boolean;
38-
// listen to notifications from specific packages. Improves performance.
39-
packagesWhitelist?: string[];
39+
// listen to notifications from specific packages. Improves performance
40+
packagesWhitelist?: string[] | null;
4041
}
4142

4243
export interface AndroidNotification {

0 commit comments

Comments
 (0)