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,49 @@ +package app.comm.android.notifications; + +import android.app.NotificationManager; +import android.content.Context; +import android.os.Bundle; +import android.service.notification.StatusBarNotification; +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) { + if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.M) { + // Method used below appeared in API level 23. Details: + // https://developer.android.com/reference/android/app/NotificationManager#getActiveNotifications() + // https://developer.android.com/reference/android/os/Build.VERSION_CODES#M + return; + } + for (StatusBarNotification notification : + notificationManager.getActiveNotifications()) { + Bundle data = notification.getNotification().extras; + if (data == null) { + continue; + } + String notificationThreadID = data.getString("threadID"); + if (notificationThreadID != null && + notificationThreadID.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; + } +}