diff --git a/native/android/app/src/main/java/app/comm/android/MainApplication.java b/native/android/app/src/main/java/app/comm/android/MainApplication.java --- a/native/android/app/src/main/java/app/comm/android/MainApplication.java +++ b/native/android/app/src/main/java/app/comm/android/MainApplication.java @@ -6,6 +6,7 @@ import androidx.annotation.NonNull; import androidx.multidex.MultiDexApplication; import app.comm.android.newarchitecture.MainApplicationReactNativeHost; +import app.comm.android.notifications.CommAndroidNotificationsPackage; import com.facebook.react.PackageList; import com.facebook.react.ReactApplication; import com.facebook.react.ReactInstanceManager; @@ -44,6 +45,7 @@ packages.add(new RNFirebaseMessagingPackage()); packages.add(new RNFirebaseNotificationsPackage()); packages.add(new KeyboardInputPackage(this.getApplication())); + packages.add(new CommAndroidNotificationsPackage()); return packages; } diff --git a/native/android/app/src/main/java/app/comm/android/notifications/CommAndroidNotifications.java b/native/android/app/src/main/java/app/comm/android/notifications/CommAndroidNotifications.java new file mode 100644 --- /dev/null +++ b/native/android/app/src/main/java/app/comm/android/notifications/CommAndroidNotifications.java @@ -0,0 +1,40 @@ +package app.comm.android.notifications; + +import android.app.NotificationManager; +import android.content.Context; +import android.service.notification.StatusBarNotification; +import android.util.Log; +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; + +public class CommAndroidNotifications extends ReactContextBaseJavaModule { + private NotificationManager notificationManager; + + CommAndroidNotifications(ReactApplicationContext reactContext) { + super(reactContext); + Context context = reactContext.getApplicationContext(); + this.notificationManager = (NotificationManager)context.getSystemService( + Context.NOTIFICATION_SERVICE); + } + + @Override + public String getName() { + return "CommAndroidNotifications"; + } + + @ReactMethod + public void removeAllActiveNotificationsForThread(String threadID) { + for (StatusBarNotification notification : + notificationManager.getActiveNotifications()) { + char[] threadIDCharArray = + notification.getNotification().extras.getCharArray("threadID"); + if (threadIDCharArray != null && + new String(threadIDCharArray).equals(threadID)) { + notificationManager.cancel(notification.getTag(), notification.getId()); + } + } + } +} 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 new file mode 100644 --- /dev/null +++ b/native/android/app/src/main/java/app/comm/android/notifications/CommAndroidNotificationsPackage.java @@ -0,0 +1,28 @@ +package app.comm.android.notifications; + +import com.facebook.react.ReactPackage; +import com.facebook.react.bridge.NativeModule; +import com.facebook.react.bridge.ReactApplicationContext; +import com.facebook.react.uimanager.ViewManager; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class CommAndroidNotificationsPackage implements ReactPackage { + + @Override + public List + createViewManagers(ReactApplicationContext reactContext) { + return Collections.emptyList(); + } + + @Override + public List + createNativeModules(ReactApplicationContext reactContext) { + List modules = new ArrayList<>(); + + modules.add(new CommAndroidNotifications(reactContext)); + + return modules; + } +}