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 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();