Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3489138
D6271.id21591.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
3 KB
Referenced Files
None
Subscribers
None
D6271.id21591.diff
View Options
diff --git a/native/android/app/src/main/java/app/comm/android/notifications/CommAndroidNotificationsEventEmitter.java b/native/android/app/src/main/java/app/comm/android/notifications/CommAndroidNotificationsEventEmitter.java
new file mode 100644
--- /dev/null
+++ b/native/android/app/src/main/java/app/comm/android/notifications/CommAndroidNotificationsEventEmitter.java
@@ -0,0 +1,62 @@
+package app.comm.android.notifications;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import androidx.localbroadcastmanager.content.LocalBroadcastManager;
+import com.facebook.react.bridge.NativeModule;
+import com.facebook.react.bridge.ReactApplicationContext;
+import com.facebook.react.bridge.ReactContext;
+import com.facebook.react.bridge.ReactContextBaseJavaModule;
+import com.facebook.react.bridge.ReactMethod;
+import com.facebook.react.modules.core.DeviceEventManagerModule;
+import java.util.Map;
+
+public class CommAndroidNotificationsEventEmitter
+ extends ReactContextBaseJavaModule {
+ private static final String TAG = "CommAndroidNotifications";
+ private volatile int listenersCount = 0;
+
+ CommAndroidNotificationsEventEmitter(ReactApplicationContext reactContext) {
+ super(reactContext);
+ LocalBroadcastManager localBroadcastManager =
+ LocalBroadcastManager.getInstance(reactContext);
+ localBroadcastManager.registerReceiver(
+ new CommAndroidNotificationsTokenReceiver(),
+ new IntentFilter(CommNotificationsHandler.TOKEN_EVENT));
+ }
+
+ @Override
+ public String getName() {
+ return "CommAndroidNotificationsEventEmitter";
+ }
+
+ @ReactMethod
+ public void addListener(String eventName) {
+ this.listenersCount += 1;
+ }
+
+ @ReactMethod
+ public void removeListeners(Integer count) {
+ this.listenersCount -= count;
+ }
+
+ private void sendEventToJS(String eventName, Object body) {
+ if (this.listenersCount == 0) {
+ return;
+ }
+ getReactApplicationContext()
+ .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
+ .emit(eventName, body);
+ }
+
+ private class CommAndroidNotificationsTokenReceiver
+ extends BroadcastReceiver {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ String token = intent.getStringExtra("token");
+ sendEventToJS("commAndroidNotificationsToken", token);
+ }
+ }
+}
diff --git a/native/android/app/src/main/java/app/comm/android/notifications/CommAndroidNotificationsPackage.java b/native/android/app/src/main/java/app/comm/android/notifications/CommAndroidNotificationsPackage.java
--- a/native/android/app/src/main/java/app/comm/android/notifications/CommAndroidNotificationsPackage.java
+++ b/native/android/app/src/main/java/app/comm/android/notifications/CommAndroidNotificationsPackage.java
@@ -22,6 +22,7 @@
List<NativeModule> modules = new ArrayList<>();
modules.add(new CommAndroidNotifications(reactContext));
+ modules.add(new CommAndroidNotificationsEventEmitter(reactContext));
return modules;
}
diff --git a/native/android/app/src/main/java/app/comm/android/notifications/CommNotificationsHandler.java b/native/android/app/src/main/java/app/comm/android/notifications/CommNotificationsHandler.java
--- a/native/android/app/src/main/java/app/comm/android/notifications/CommNotificationsHandler.java
+++ b/native/android/app/src/main/java/app/comm/android/notifications/CommNotificationsHandler.java
@@ -67,6 +67,8 @@
private Bitmap displayableNotificationLargeIcon;
private NotificationManager notificationManager;
+ public static final String TOKEN_EVENT = "TOKEN_EVENT";
+
@Override
public void onCreate() {
super.onCreate();
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Thu, Dec 19, 11:42 AM (3 h, 9 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2677195
Default Alt Text
D6271.id21591.diff (3 KB)
Attached To
Mode
D6271: Introduce CommAndroidNotificationsEventEmitter native module, capable of sending device token from Java to JavaScript in response to broadcast event
Attached
Detach File
Event Timeline
Log In to Comment