Skip to content

Instantly share code, notes, and snippets.

@cristiangu
Created January 10, 2025 19:09
Show Gist options
  • Save cristiangu/2c77d185de680f4e499b39288921637b to your computer and use it in GitHub Desktop.
Save cristiangu/2c77d185de680f4e499b39288921637b to your computer and use it in GitHub Desktop.
import android.os.Bundle
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.WritableMap
object ReactNativeFirebaseMessagingSerializer {
fun klaviyoNotificationExtrasToWritableMap(
extras: Bundle?
): WritableMap? {
val notificationMap = Arguments.createMap()
val androidNotificationMap = Arguments.createMap()
if (extras?.getString("com.klaviyo.title") != null) {
notificationMap.putString("title", extras.getString("com.klaviyo.title"))
}
if (extras?.getString("com.klaviyo.body") != null) {
notificationMap.putString("body", extras.getString("com.klaviyo.body"))
}
if (extras?.getInt("com.klaviyo.notification_count") != null) {
androidNotificationMap.putInt("count", extras.getInt("com.klaviyo.notification_count"))
}
if (extras?.getString("com.klaviyo.sound") != null) {
androidNotificationMap.putString("sound", extras.getString("com.klaviyo.sound"))
}
notificationMap.putMap("android", androidNotificationMap)
return notificationMap
}
fun klaviyoExtrasToWritableMap(extras: Bundle?): WritableMap? {
val messageMap = Arguments.createMap()
val dataMap = Arguments.createMap()
// TODO: Copy the `data` payload from that notification if exists
// if (remoteMessage.getData().size > 0) {
// val entries: Set<Map.Entry<String?, String?>> = remoteMessage.getData().entries
// for ((key, value) in entries) {
// dataMap.putString(key!!, value)
// }
// }
messageMap.putMap("data", dataMap)
messageMap.putMap(
"notification",
klaviyoNotificationExtrasToWritableMap(extras)
)
return messageMap
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment