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