1 // Copyright 2015-present 650 Industries. All rights reserved. 2 package host.exp.exponent.notifications 3 4 import host.exp.exponent.analytics.EXL.e 5 import host.exp.exponent.RNObject 6 import org.json.JSONException 7 import org.json.JSONObject 8 9 open class ExponentNotification( 10 val experienceScopeKey: String, 11 val body: String?, 12 val notificationId: Int, 13 val isMultiple: Boolean, 14 val isRemote: Boolean 15 ) { 16 var actionType: String? = null 17 var inputText: String? = null 18 19 fun toJSONObject(origin: String?): JSONObject { 20 return JSONObject().apply { 21 try { 22 put(NotificationConstants.NOTIFICATION_EXPERIENCE_ID_KEY, experienceScopeKey) 23 put( 24 NotificationConstants.NOTIFICATION_EXPERIENCE_SCOPE_KEY_KEY, 25 experienceScopeKey 26 ) 27 if (origin != null) { 28 put(NotificationConstants.NOTIFICATION_ORIGIN_KEY, origin) 29 } 30 put(NotificationConstants.NOTIFICATION_MESSAGE_KEY, body) // deprecated 31 put(NotificationConstants.NOTIFICATION_DATA_KEY, body) 32 put(NotificationConstants.NOTIFICATION_ID_KEY, notificationId) 33 put(NotificationConstants.NOTIFICATION_IS_MULTIPLE_KEY, isMultiple) 34 put(NotificationConstants.NOTIFICATION_REMOTE_KEY, isRemote) 35 } catch (e: JSONException) { 36 e(TAG, e.toString()) 37 } 38 } 39 } 40 41 fun toWriteableMap(sdkVersion: String?, origin: String?): Any { 42 return RNObject("com.facebook.react.bridge.Arguments").loadVersion(sdkVersion!!) 43 .callStaticRecursive("createMap")!!.apply { 44 if (origin != null) { 45 call("putString", NotificationConstants.NOTIFICATION_ORIGIN_KEY, origin) 46 } 47 call("putString", NotificationConstants.NOTIFICATION_DATA_KEY, body) 48 call("putInt", NotificationConstants.NOTIFICATION_ID_KEY, notificationId) 49 call("putBoolean", NotificationConstants.NOTIFICATION_IS_MULTIPLE_KEY, isMultiple) 50 call("putBoolean", NotificationConstants.NOTIFICATION_REMOTE_KEY, isRemote) 51 call("putString", NotificationConstants.NOTIFICATION_ACTION_TYPE, actionType) 52 call("putString", NotificationConstants.NOTIFICATION_INPUT_TEXT, inputText) 53 } 54 .get()!! 55 } 56 57 companion object { 58 private val TAG = ExponentNotification::class.java.simpleName 59 60 fun fromJSONObjectString(json: String?): ExponentNotification? { 61 return if (json == null) { 62 null 63 } else try { 64 val jsonObject = JSONObject(json) 65 val body = if (jsonObject.has(NotificationConstants.NOTIFICATION_DATA_KEY)) { 66 jsonObject.getString(NotificationConstants.NOTIFICATION_DATA_KEY) 67 } else { 68 if (jsonObject.has(NotificationConstants.NOTIFICATION_MESSAGE_KEY)) { 69 jsonObject.getString(NotificationConstants.NOTIFICATION_MESSAGE_KEY) 70 } else { 71 null 72 } 73 } 74 ExponentNotification( 75 if (jsonObject.has(NotificationConstants.NOTIFICATION_EXPERIENCE_SCOPE_KEY_KEY)) { 76 jsonObject.optString( 77 NotificationConstants.NOTIFICATION_EXPERIENCE_SCOPE_KEY_KEY 78 ) 79 } else { 80 jsonObject.getString(NotificationConstants.NOTIFICATION_EXPERIENCE_ID_KEY) 81 }, 82 body, 83 jsonObject.getInt(NotificationConstants.NOTIFICATION_ID_KEY), 84 jsonObject.getBoolean(NotificationConstants.NOTIFICATION_IS_MULTIPLE_KEY), 85 jsonObject.getBoolean(NotificationConstants.NOTIFICATION_REMOTE_KEY) 86 ) 87 } catch (e: JSONException) { 88 e(TAG, e.toString()) 89 null 90 } 91 } 92 } 93 } 94