1 package host.exp.exponent.fcm 2 3 import android.content.Context 4 import com.google.firebase.messaging.RemoteMessage 5 import expo.modules.notifications.notifications.model.NotificationContent 6 import expo.modules.notifications.notifications.model.NotificationRequest 7 import expo.modules.notifications.notifications.model.triggers.FirebaseNotificationTrigger 8 import expo.modules.notifications.service.delegates.FirebaseMessagingDelegate 9 import host.exp.exponent.Constants 10 import host.exp.exponent.analytics.EXL 11 import host.exp.exponent.notifications.NotificationConstants 12 import host.exp.exponent.notifications.model.ScopedNotificationRequest 13 import host.exp.exponent.storage.ExponentDB 14 import org.json.JSONException 15 16 class ExpoFirebaseMessagingDelegate(context: Context) : FirebaseMessagingDelegate(context) { onNewTokennull17 override fun onNewToken(token: String) { 18 if (!Constants.FCM_ENABLED) { 19 return 20 } 21 super.onNewToken(token) 22 FcmRegistrationIntentService.registerForeground(context.applicationContext, token) 23 } 24 onMessageReceivednull25 override fun onMessageReceived(remoteMessage: RemoteMessage) { 26 if (!Constants.FCM_ENABLED) { 27 return 28 } 29 30 val scopeKey = remoteMessage.data[NotificationConstants.NOTIFICATION_EXPERIENCE_SCOPE_KEY_KEY] 31 if (scopeKey == null) { 32 EXL.e("expo-notifications", "No scope key found in notification") 33 return 34 } 35 36 val exponentDBObject = try { 37 ExponentDB.experienceScopeKeyToExperienceSync(scopeKey) 38 } catch (e: JSONException) { 39 e.printStackTrace() 40 EXL.e("expo-notifications", "Error getting experience for scope key $scopeKey") 41 return 42 } 43 44 if (exponentDBObject == null) { 45 EXL.e("expo-notifications", "No experience found for scope key $scopeKey") 46 return 47 } 48 49 dispatchToNextNotificationModule(remoteMessage) 50 } 51 dispatchToNextNotificationModulenull52 private fun dispatchToNextNotificationModule(remoteMessage: RemoteMessage) { 53 super.onMessageReceived(remoteMessage) 54 } 55 createNotificationRequestnull56 override fun createNotificationRequest( 57 identifier: String, 58 content: NotificationContent, 59 notificationTrigger: FirebaseNotificationTrigger 60 ): NotificationRequest { 61 if (Constants.isStandaloneApp()) { 62 return super.createNotificationRequest(identifier, content, notificationTrigger) 63 } 64 65 val data = notificationTrigger.remoteMessage.data 66 return ScopedNotificationRequest( 67 identifier, 68 content, 69 notificationTrigger, 70 data[NotificationConstants.NOTIFICATION_EXPERIENCE_SCOPE_KEY_KEY] 71 ) 72 } 73 } 74