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 @@ -5,12 +5,15 @@ import android.content.Intent; import android.content.IntentFilter; import androidx.localbroadcastmanager.content.LocalBroadcastManager; +import com.facebook.react.bridge.Arguments; 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.bridge.WritableMap; import com.facebook.react.modules.core.DeviceEventManagerModule; +import com.google.firebase.messaging.RemoteMessage; import java.util.Map; public class CommAndroidNotificationsEventEmitter @@ -25,6 +28,9 @@ localBroadcastManager.registerReceiver( new CommAndroidNotificationsTokenReceiver(), new IntentFilter(CommNotificationsHandler.TOKEN_EVENT)); + localBroadcastManager.registerReceiver( + new CommAndroidNotificationsForegroundMessageReceiver(), + new IntentFilter(CommNotificationsHandler.FOREGROUND_MESSAGE_EVENT)); } @Override @@ -51,6 +57,48 @@ .emit(eventName, body); } + private WritableMap + parseRemoteMessageToJSForegroundMessage(RemoteMessage message) { + if (message.getData() == null) { + return null; + } + WritableMap jsForegroundMessage = Arguments.createMap(); + boolean dataComplete = false; + + // Logic below matches JS types we define in native/push/android.js + String[] obligatoryKeys = { + CommNotificationsHandler.TITLE_KEY, + CommNotificationsHandler.BODY_KEY, + CommNotificationsHandler.THREAD_ID_KEY, + }; + + String[] optionalKeys = { + CommNotificationsHandler.MESSAGE_INFOS_KEY, + CommNotificationsHandler.PREFIX_KEY, + }; + + for (String key : obligatoryKeys) { + if (message.getData().get(key) == null) { + dataComplete = false; + break; + } + dataComplete = true; + jsForegroundMessage.putString(key, message.getData().get(key)); + } + + if (!dataComplete) { + return null; + } + + for (String key : optionalKeys) { + if (message.getData().get(key) == null) { + continue; + } + jsForegroundMessage.putString(key, message.getData().get(key)); + } + return jsForegroundMessage; + } + private class CommAndroidNotificationsTokenReceiver extends BroadcastReceiver { @Override @@ -59,4 +107,18 @@ sendEventToJS("commAndroidNotificationsToken", token); } } -} + + private class CommAndroidNotificationsForegroundMessageReceiver + extends BroadcastReceiver { + @Override + public void onReceive(Context context, Intent intent) { + RemoteMessage message = intent.getParcelableExtra("message"); + WritableMap jsForegroundMessage = + parseRemoteMessageToJSForegroundMessage(message); + if (jsForegroundMessage != null) { + sendEventToJS( + "commAndroidNotificationsForegroundMessage", jsForegroundMessage); + } + } + } +} \ No newline at end of file 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 @@ -49,24 +49,27 @@ * 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}; private NotificationManager notificationManager; + 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() {