diff --git a/native/android/app/src/main/java/app/comm/android/notifications/CommAndroidNotificationParser.java b/native/android/app/src/main/java/app/comm/android/notifications/CommAndroidNotificationParser.java new file mode 100644 --- /dev/null +++ b/native/android/app/src/main/java/app/comm/android/notifications/CommAndroidNotificationParser.java @@ -0,0 +1,44 @@ +package app.comm.android.notifications; + +import com.facebook.react.bridge.Arguments; +import com.facebook.react.bridge.WritableMap; +import com.google.firebase.messaging.RemoteMessage; +import java.util.Set; + +public class CommAndroidNotificationParser { + + private static final Set obligatoryKeys = Set.of( + CommNotificationsHandler.TITLE_KEY, + CommNotificationsHandler.BODY_KEY, + CommNotificationsHandler.THREAD_ID_KEY); + + private static final Set optionalKeys = Set.of( + CommNotificationsHandler.MESSAGE_INFOS_KEY, + CommNotificationsHandler.PREFIX_KEY); + + public static WritableMap + parseRemoteMessageToJSForegroundMessage(RemoteMessage message) { + if (message.getData() == null) { + return null; + } + WritableMap jsForegroundMessage = Arguments.createMap(); + + for (String key : obligatoryKeys) { + String value = message.getData().get(key); + if (value == null) { + return null; + } + jsForegroundMessage.putString(key, value); + } + + for (String key : optionalKeys) { + String value = message.getData().get(key); + if (value == null) { + continue; + } + jsForegroundMessage.putString(key, value); + } + + return jsForegroundMessage; + } +} \ No newline at end of file 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 --- 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 @@ -10,8 +10,9 @@ import com.facebook.react.bridge.ReactContext; 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; -import java.util.Map; +import com.google.firebase.messaging.RemoteMessage; public class CommAndroidNotificationsEventEmitter extends ReactContextBaseJavaModule { @@ -25,6 +26,9 @@ localBroadcastManager.registerReceiver( new CommAndroidNotificationsTokenReceiver(), new IntentFilter(CommNotificationsHandler.TOKEN_EVENT)); + localBroadcastManager.registerReceiver( + new CommAndroidNotificationsForegroundMessageReceiver(), + new IntentFilter(CommNotificationsHandler.FOREGROUND_MESSAGE_EVENT)); } @Override @@ -59,4 +63,19 @@ sendEventToJS("commAndroidNotificationsToken", token); } } + + private class CommAndroidNotificationsForegroundMessageReceiver + extends BroadcastReceiver { + @Override + public void onReceive(Context context, Intent intent) { + RemoteMessage message = intent.getParcelableExtra("message"); + WritableMap jsForegroundMessage = + CommAndroidNotificationParser.parseRemoteMessageToJSForegroundMessage( + message); + if (jsForegroundMessage != null) { + sendEventToJS( + "commAndroidNotificationsForegroundMessage", jsForegroundMessage); + } + } + } } 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 @@ -52,18 +52,11 @@ * the easiest in terms of making it safe. */ public class CommNotificationsHandler extends RNFirebaseMessagingService { - private static final String RESCIND_KEY = "rescind"; - private static final String RESCIND_ID_KEY = "rescindID"; private static final String BADGE_KEY = "badge"; private static final String BADGE_ONLY_KEY = "badgeOnly"; private static final String BACKGROUND_NOTIF_TYPE_KEY = "backgroundNotifType"; - private static final String THREAD_ID_KEY = "threadID"; private static final String SET_UNREAD_STATUS_KEY = "setUnreadStatus"; - private static final String MESSAGE_INFOS_KEY = "messageInfos"; private static final String NOTIF_ID_KEY = "id"; - private static final String TITLE_KEY = "title"; - private static final String PREFIX_KEY = "prefix"; - private static final String BODY_KEY = "body"; private static final String CHANNEL_ID = "default"; private static final long[] VIBRATION_SPEC = {500, 500}; @@ -71,7 +64,17 @@ private NotificationManager notificationManager; private boolean isAppInForeground = false; + public static final String RESCIND_KEY = "rescind"; + public static final String RESCIND_ID_KEY = "rescindID"; + public static final String TITLE_KEY = "title"; + public static final String PREFIX_KEY = "prefix"; + public static final String BODY_KEY = "body"; + public static final String MESSAGE_INFOS_KEY = "messageInfos"; + public static final String THREAD_ID_KEY = "threadID"; + public static final String TOKEN_EVENT = "TOKEN_EVENT"; + public static final String FOREGROUND_MESSAGE_EVENT = + "FOREGROUND_MESSAGE_EVENT"; @Override public void onCreate() {