Repository navigation
Expand file tree
/
Copy pathNotificationListenerModule.java
More file actions
160 lines (140 loc) · 6.03 KB
/
Copy pathNotificationListenerModule.java
File metadata and controls
160 lines (140 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package com.flashnotify.notificationlistener;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
public class NotificationListenerModule extends ReactContextBaseJavaModule {
private static final String TAG = "NotificationListenerModule";
private static final String MODULE_NAME = "NotificationListenerModule";
private static final String EVENT_NOTIFICATION = "onNotificationReceived";
private static final String EVENT_NOTIFICATION_REMOVED = "onNotificationRemoved";
private final ReactApplicationContext reactContext;
private BroadcastReceiver notificationReceiver;
private int listenerCount = 0;
public NotificationListenerModule(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;
}
@Override
public String getName() {
return MODULE_NAME;
}
@ReactMethod
public void addListener(String eventName) {
if (listenerCount == 0) {
startListening();
}
listenerCount++;
}
@ReactMethod
public void removeListeners(int count) {
listenerCount -= count;
if (listenerCount <= 0) {
listenerCount = 0;
stopListening();
}
}
@ReactMethod
public void isNotificationListenerEnabled(com.facebook.react.bridge.Promise promise) {
try {
promise.resolve(isNotificationServiceEnabled());
} catch (Exception e) {
promise.reject("ERROR", e.getMessage());
}
}
@ReactMethod
public void openNotificationListenerSettings(com.facebook.react.bridge.Promise promise) {
try {
Intent intent = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
reactContext.startActivity(intent);
promise.resolve(null);
} catch (Exception e) {
promise.reject("ERROR", e.getMessage());
}
}
private boolean isNotificationServiceEnabled() {
String pkgName = reactContext.getPackageName();
String flat = Settings.Secure.getString(
reactContext.getContentResolver(),
"enabled_notification_listeners"
);
if (!TextUtils.isEmpty(flat)) {
String[] names = flat.split(":");
for (String name : names) {
ComponentName cn = ComponentName.unflattenFromString(name);
if (cn != null && pkgName.equals(cn.getPackageName())) {
return true;
}
}
}
return false;
}
private void startListening() {
if (notificationReceiver != null) return;
notificationReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (FlashNotifyListenerService.ACTION_NOTIFICATION_RECEIVED.equals(action)) {
WritableMap params = Arguments.createMap();
params.putString("packageName", intent.getStringExtra(FlashNotifyListenerService.EXTRA_PACKAGE_NAME));
params.putString("appName", intent.getStringExtra(FlashNotifyListenerService.EXTRA_APP_NAME));
params.putString("title", intent.getStringExtra(FlashNotifyListenerService.EXTRA_TITLE));
params.putString("text", intent.getStringExtra(FlashNotifyListenerService.EXTRA_TEXT));
params.putString("key", intent.getStringExtra(FlashNotifyListenerService.EXTRA_KEY));
sendEvent(EVENT_NOTIFICATION, params);
} else if (FlashNotifyListenerService.ACTION_NOTIFICATION_REMOVED.equals(action)) {
WritableMap params = Arguments.createMap();
params.putString("packageName", intent.getStringExtra(FlashNotifyListenerService.EXTRA_PACKAGE_NAME));
params.putString("key", intent.getStringExtra(FlashNotifyListenerService.EXTRA_KEY));
sendEvent(EVENT_NOTIFICATION_REMOVED, params);
}
}
};
// Register for both posted and removed broadcasts
IntentFilter filter = new IntentFilter();
filter.addAction(FlashNotifyListenerService.ACTION_NOTIFICATION_RECEIVED);
filter.addAction(FlashNotifyListenerService.ACTION_NOTIFICATION_REMOVED);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
reactContext.registerReceiver(notificationReceiver, filter, Context.RECEIVER_NOT_EXPORTED);
} else {
reactContext.registerReceiver(notificationReceiver, filter);
}
Log.d(TAG, "Started listening for notifications");
}
private void stopListening() {
if (notificationReceiver != null) {
try {
reactContext.unregisterReceiver(notificationReceiver);
} catch (Exception e) {
Log.w(TAG, "Error unregistering receiver", e);
}
notificationReceiver = null;
}
}
private void sendEvent(String eventName, WritableMap params) {
try {
reactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(eventName, params);
} catch (Exception e) {
Log.e(TAG, "Error sending event", e);
}
}
@Override
public void onCatalystInstanceDestroy() {
stopListening();
}
}