@trycourier/courier-react-native 2.0.0-beta5 → 2.0.0-beta7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. package/android/build.gradle +2 -1
  2. package/android/src/main/java/com/courierreactnative/CourierEvents.kt +28 -0
  3. package/android/src/main/java/com/courierreactnative/CourierReactNativeActivity.kt +17 -19
  4. package/android/src/main/java/com/courierreactnative/CourierReactNativeModule.kt +229 -158
  5. package/android/src/main/java/com/courierreactnative/CourierReactNativeViewManager.kt +135 -17
  6. package/android/src/main/java/com/courierreactnative/Extensions.kt +95 -0
  7. package/ios/CourierReactNative.xcodeproj/project.xcworkspace/xcuserdata/mike.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
  8. package/ios/CourierReactNativeModule.m +5 -8
  9. package/ios/CourierReactNativeModule.swift +16 -31
  10. package/lib/commonjs/hooks/CourierProvider.js +2 -2
  11. package/lib/commonjs/hooks/CourierProvider.js.map +1 -1
  12. package/lib/commonjs/index.js +2 -0
  13. package/lib/commonjs/index.js.map +1 -1
  14. package/lib/commonjs/views/CourierInboxView.js +44 -11
  15. package/lib/commonjs/views/CourierInboxView.js.map +1 -1
  16. package/lib/module/hooks/CourierProvider.js +2 -2
  17. package/lib/module/hooks/CourierProvider.js.map +1 -1
  18. package/lib/module/index.js +2 -0
  19. package/lib/module/index.js.map +1 -1
  20. package/lib/module/views/CourierInboxView.js +43 -11
  21. package/lib/module/views/CourierInboxView.js.map +1 -1
  22. package/lib/typescript/hooks/CourierProvider.d.ts +2 -2
  23. package/lib/typescript/hooks/CourierProvider.d.ts.map +1 -1
  24. package/lib/typescript/index.d.ts +5 -3
  25. package/lib/typescript/index.d.ts.map +1 -1
  26. package/lib/typescript/models/CourierInboxTheme.d.ts +3 -0
  27. package/lib/typescript/models/CourierInboxTheme.d.ts.map +1 -1
  28. package/lib/typescript/views/CourierInboxView.d.ts.map +1 -1
  29. package/package.json +1 -1
  30. package/src/hooks/CourierProvider.tsx +4 -4
  31. package/src/index.tsx +14 -6
  32. package/src/models/CourierInboxTheme.tsx +4 -1
  33. package/src/views/CourierInboxView.tsx +55 -11
@@ -101,7 +101,8 @@ dependencies {
101
101
  implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
102
102
 
103
103
  // Courier Core SDK
104
- api 'com.github.trycourier:courier-android:2.0.2'
104
+ api 'com.github.trycourier:courier-android:2.0.32'
105
+ api 'androidx.recyclerview:recyclerview:1.3.1'
105
106
 
106
107
  }
107
108
 
@@ -0,0 +1,28 @@
1
+ package com.courierreactnative
2
+
3
+ internal class CourierEvents {
4
+
5
+ companion object {
6
+ const val COURIER_ERROR_TAG = "Courier Android SDK Error"
7
+ }
8
+
9
+ object Log {
10
+ const val DEBUG_LOG = "courierDebugEvent"
11
+ }
12
+
13
+ object Auth {
14
+ const val USER_CHANGED = "courierAuthUserChanged"
15
+ }
16
+
17
+ object Push {
18
+ const val CLICKED_EVENT = "pushNotificationClicked"
19
+ const val DELIVERED_EVENT = "pushNotificationDelivered"
20
+ }
21
+
22
+ object Inbox {
23
+ const val INITIAL_LOADING = "inboxInitialLoad"
24
+ const val ERROR = "inboxError"
25
+ const val MESSAGES_CHANGED = "inboxMessagesChanged"
26
+ }
27
+
28
+ }
@@ -3,6 +3,9 @@ package com.courierreactnative
3
3
  import android.content.Intent
4
4
  import android.os.Bundle
5
5
  import com.courier.android.Courier
6
+ import com.courier.android.utils.getLastDeliveredMessage
7
+ import com.courier.android.utils.pushNotification
8
+ import com.courier.android.utils.trackPushNotificationClick
6
9
  //import com.courier.android.pushNotification
7
10
  //import com.courier.android.trackPushNotificationClick
8
11
  import com.facebook.react.ReactActivity
@@ -12,11 +15,6 @@ import org.json.JSONObject
12
15
 
13
16
  open class CourierReactNativeActivity : ReactActivity() {
14
17
 
15
- companion object {
16
- private const val PUSH_CLICKED_EVENT = "pushNotificationClicked"
17
- private const val PUSH_DELIVERED_EVENT = "pushNotificationDelivered"
18
- }
19
-
20
18
  override fun onCreate(savedInstanceState: Bundle?) {
21
19
  super.onCreate(savedInstanceState)
22
20
 
@@ -28,16 +26,10 @@ open class CourierReactNativeActivity : ReactActivity() {
28
26
  checkIntentForPushNotificationClick(intent)
29
27
 
30
28
  // Handle delivered messages on the main thread
31
- // Courier.getLastDeliveredMessage { message ->
32
- // postPushNotificationDelivered(message)
33
- // }
34
-
35
- }
36
-
37
- private fun sendEvent(eventName: String, params: String) {
38
- reactInstanceManager.currentReactContext?.let { reactContext ->
39
- reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)?.emit(eventName, params)
29
+ Courier.shared.getLastDeliveredMessage { message ->
30
+ postPushNotificationDelivered(message)
40
31
  }
32
+
41
33
  }
42
34
 
43
35
  override fun onNewIntent(intent: Intent?) {
@@ -46,17 +38,23 @@ open class CourierReactNativeActivity : ReactActivity() {
46
38
  }
47
39
 
48
40
  private fun checkIntentForPushNotificationClick(intent: Intent?) {
49
- // intent?.trackPushNotificationClick { message ->
50
- // postPushNotificationClicked(message)
51
- // }
41
+ intent?.trackPushNotificationClick { message ->
42
+ postPushNotificationClicked(message)
43
+ }
52
44
  }
53
45
 
54
46
  private fun postPushNotificationDelivered(message: RemoteMessage) {
55
- // sendEvent(PUSH_DELIVERED_EVENT, JSONObject(message.pushNotification).toString())
47
+ reactInstanceManager.currentReactContext?.sendEvent(
48
+ eventName = CourierEvents.Push.DELIVERED_EVENT,
49
+ value = JSONObject(message.pushNotification).toString()
50
+ )
56
51
  }
57
52
 
58
53
  private fun postPushNotificationClicked(message: RemoteMessage) {
59
- // sendEvent(PUSH_CLICKED_EVENT, JSONObject(message.pushNotification).toString())
54
+ reactInstanceManager.currentReactContext?.sendEvent(
55
+ eventName = CourierEvents.Push.CLICKED_EVENT,
56
+ value = JSONObject(message.pushNotification).toString()
57
+ )
60
58
  }
61
59
 
62
60
  }
@@ -1,52 +1,99 @@
1
1
  package com.courierreactnative
2
2
 
3
3
  import android.content.Intent
4
- import com.courier.android.*
5
- import com.courier.android.models.CourierAgent
6
- import com.courier.android.models.CourierProvider
4
+ import com.courier.android.Courier
5
+ import com.courier.android.models.*
7
6
  import com.courier.android.modules.*
8
7
  import com.courier.android.utils.pushNotification
9
8
  import com.courier.android.utils.trackPushNotificationClick
10
9
  import com.facebook.react.ReactActivity
11
10
  import com.facebook.react.bridge.*
12
- import com.facebook.react.modules.core.DeviceEventManagerModule
13
11
  import com.google.firebase.messaging.RemoteMessage
14
12
  import org.json.JSONObject
13
+ import java.util.*
15
14
 
16
15
 
17
16
  class CourierReactNativeModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
18
17
 
19
18
  override fun getName() = "CourierReactNativeModule"
19
+ private val reactActivity: ReactActivity? get() = currentActivity as? ReactActivity
20
20
 
21
- companion object {
22
- private const val COURIER_ERROR_TAG = "Courier Android SDK Error"
23
- private const val COURIER_PUSH_NOTIFICATION_DEBUG_LOG_EVENT = "courierDebugEvent"
24
- private const val COURIER_PUSH_NOTIFICATION_CLICKED_EVENT = "pushNotificationClicked"
25
- }
21
+ private val authListeners = mutableMapOf<String, CourierAuthenticationListener>()
22
+ private val inboxListeners = mutableMapOf<String, CourierInboxListener>()
26
23
 
27
24
  init {
25
+
28
26
  // User Agent is used to ensure we know the SDK
29
27
  // the requests come from
30
28
  Courier.USER_AGENT = CourierAgent.REACT_NATIVE_ANDROID
31
29
 
32
- // Courier.shared.logListener = { data ->
33
- // sendEvent(reactContext, COURIER_PUSH_NOTIFICATION_DEBUG_LOG_EVENT, data)
34
- // }
30
+ // Attach the log listener
31
+ Courier.shared.logListener = { data ->
32
+ reactContext.sendEvent(CourierEvents.Log.DEBUG_LOG, data)
33
+ }
35
34
 
36
35
  }
37
36
 
38
- private val reactActivity: ReactActivity? get() = currentActivity as? ReactActivity
37
+ @ReactMethod
38
+ fun addListener(type: String?) {
39
+ // Keep: Required for RN built in Event Emitter Calls.
40
+ }
39
41
 
40
42
  @ReactMethod
41
- fun setDebugMode(isDebugging: Boolean, promise: Promise) {
42
- try {
43
- Courier.shared.isDebugging = isDebugging
44
- promise.resolve(Courier.shared.isDebugging)
45
- } catch (e: Exception) {
46
- promise.reject(COURIER_ERROR_TAG, e)
43
+ fun removeListeners(type: Int?) {
44
+ // Keep: Required for RN built in Event Emitter Calls.
45
+ }
46
+
47
+ @ReactMethod(isBlockingSynchronousMethod = true)
48
+ fun setDebugMode(isDebugging: Boolean): Boolean {
49
+ Courier.shared.isDebugging = isDebugging
50
+ return Courier.shared.isDebugging
51
+ }
52
+
53
+ @ReactMethod
54
+ fun registerPushNotificationClickedOnKilledState() {
55
+ reactActivity?.let { activity ->
56
+ checkIntentForPushNotificationClick(activity.intent)
57
+ }
58
+ }
59
+
60
+ private fun checkIntentForPushNotificationClick(intent: Intent?) {
61
+ intent?.trackPushNotificationClick { message ->
62
+ postPushNotificationClicked(message)
47
63
  }
48
64
  }
49
65
 
66
+ private fun postPushNotificationClicked(message: RemoteMessage) {
67
+ reactApplicationContext.sendEvent(
68
+ eventName = CourierEvents.Push.CLICKED_EVENT,
69
+ value = JSONObject(message.pushNotification).toString()
70
+ )
71
+ }
72
+
73
+ @ReactMethod
74
+ fun requestNotificationPermission(promise: Promise) {
75
+ // try {
76
+ // reactActivity?.requestNotificationPermission { isGranted ->
77
+ //// val status = if (isGranted) NotificationPermissionStatus.AUTHORIZED else NotificationPermissionStatus.DENIED
78
+ //// promise.resolve(status.value)
79
+ // }
80
+ // } catch (e: Exception) {
81
+ // promise.reject(CourierEvents.COURIER_ERROR_TAG, e)
82
+ // }
83
+ }
84
+
85
+ @ReactMethod
86
+ fun getNotificationPermissionStatus(promise: Promise) {
87
+ // try {
88
+ // reactActivity?.getNotificationPermissionStatus { isGranted ->
89
+ // val status = if (isGranted) NotificationPermissionStatus.AUTHORIZED else NotificationPermissionStatus.DENIED
90
+ // promise.resolve(status.value)
91
+ // }
92
+ // } catch (e: Exception) {
93
+ // promise.reject(COURIER_ERROR_TAG, e)
94
+ // }
95
+ }
96
+
50
97
  @ReactMethod
51
98
  fun signIn(accessToken: String, clientKey: String?, userId: String, promise: Promise) {
52
99
  Courier.shared.signIn(
@@ -57,160 +104,184 @@ class CourierReactNativeModule(reactContext: ReactApplicationContext) : ReactCon
57
104
  promise.resolve(null)
58
105
  },
59
106
  onFailure = { e ->
60
- promise.reject(COURIER_ERROR_TAG, e)
107
+ promise.reject(CourierEvents.COURIER_ERROR_TAG, e)
61
108
  }
62
109
  )
63
110
  }
64
111
 
65
- // @ReactMethod
66
- // fun getFcmToken(promise: Promise) {
67
- // Courier.shared.getFCMToken(
68
- // onSuccess = { token ->
69
- // promise.resolve(token)
70
- // },
71
- // onFailure = { e ->
72
- // promise.reject(COURIER_ERROR_TAG, e)
73
- // }
74
- // )
75
- // }
76
- //
77
- // @ReactMethod
78
- // fun setFcmToken(token: String, promise: Promise) {
79
- //
80
- // token.let { fcmToken ->
81
- //
82
- // Courier.shared.setFCMToken(
83
- // fcmToken,
84
- // onSuccess = {
85
- // promise.resolve(null)
86
- // },
87
- // onFailure = { e ->
88
- // promise.reject(COURIER_ERROR_TAG, e)
89
- // }
90
- // )
91
- //
92
- // }
93
- //
94
- // }
95
- //
96
112
  @ReactMethod
97
- fun getUserId(promise: Promise) {
98
- try {
99
- val userId = Courier.shared.userId
100
- promise.resolve(userId)
101
- } catch (e: Exception) {
102
- promise.reject(COURIER_ERROR_TAG, e)
113
+ fun signOut(promise: Promise) {
114
+ Courier.shared.signOut(
115
+ onSuccess = {
116
+ promise.resolve(null)
117
+ },
118
+ onFailure = { e ->
119
+ promise.reject(CourierEvents.COURIER_ERROR_TAG, e)
120
+ }
121
+ )
122
+ }
123
+
124
+ @ReactMethod(isBlockingSynchronousMethod = true)
125
+ fun getUserId(): String? {
126
+ return Courier.shared.userId
127
+ }
128
+
129
+ @ReactMethod(isBlockingSynchronousMethod = true)
130
+ fun addAuthenticationListener(): String {
131
+
132
+ val listener = Courier.shared.addAuthenticationListener { userId ->
133
+ reactApplicationContext.sendEvent(
134
+ eventName = CourierEvents.Auth.USER_CHANGED,
135
+ value = userId
136
+ )
103
137
  }
138
+
139
+ val id = UUID.randomUUID().toString()
140
+ authListeners[id] = listener
141
+
142
+ return id
143
+
144
+ }
145
+
146
+ @ReactMethod(isBlockingSynchronousMethod = true)
147
+ fun removeAuthenticationListener(listenerId: String): String {
148
+
149
+ // Remove the listener
150
+ val listener = authListeners[listenerId]
151
+ listener?.remove()
152
+
153
+ // Remove from map
154
+ authListeners.remove(listenerId)
155
+
156
+ return listenerId
157
+
104
158
  }
105
159
 
106
160
  @ReactMethod
107
- fun signOut(promise: Promise) {
108
- Courier.shared.signOut(
161
+ fun setFcmToken(token: String, promise: Promise) {
162
+ Courier.shared.setFCMToken(
163
+ token = token,
109
164
  onSuccess = {
110
165
  promise.resolve(null)
111
166
  },
112
167
  onFailure = { e ->
113
- promise.reject(COURIER_ERROR_TAG, e)
168
+ promise.reject(CourierEvents.COURIER_ERROR_TAG, e)
114
169
  }
115
170
  )
116
171
  }
117
- //
118
- // @ReactMethod
119
- // fun sendPush(
120
- // authKey: String,
121
- // userId: String,
122
- // title: String,
123
- // body: String,
124
- // providers: ReadableArray,
125
- // promise: Promise
126
- // ) {
127
- //// Courier.shared.sendPush(
128
- //// authKey = authKey,
129
- //// userId = userId,
130
- //// title = title,
131
- //// body = body,
132
- //// providers = providers.toCourierProviders(),
133
- //// onSuccess = { messageId ->
134
- //// promise.resolve(messageId)
135
- //// },
136
- //// onFailure = { e ->
137
- //// promise.reject(COURIER_ERROR_TAG, e)
138
- //// }
139
- //// )
140
- // }
141
- //
142
- // @ReactMethod
143
- // fun requestNotificationPermission(promise: Promise) {
144
- //// try {
145
- //// reactActivity?.requestNotificationPermission { isGranted ->
146
- //// val status = if (isGranted) NotificationPermissionStatus.AUTHORIZED else NotificationPermissionStatus.DENIED
147
- //// promise.resolve(status.value)
148
- //// }
149
- //// } catch (e: Exception) {
150
- //// promise.reject(COURIER_ERROR_TAG, e)
151
- //// }
152
- // }
153
- //
154
- // @ReactMethod
155
- // fun getNotificationPermissionStatus(promise: Promise) {
156
- //// try {
157
- //// reactActivity?.getNotificationPermissionStatus { isGranted ->
158
- //// val status = if (isGranted) NotificationPermissionStatus.AUTHORIZED else NotificationPermissionStatus.DENIED
159
- //// promise.resolve(status.value)
160
- //// }
161
- //// } catch (e: Exception) {
162
- //// promise.reject(COURIER_ERROR_TAG, e)
163
- //// }
164
- // }
165
- //
166
- // @ReactMethod
167
- // fun registerPushNotificationClickedOnKilledState() {
168
- // reactActivity?.let { activity ->
169
- // checkIntentForPushNotificationClick(activity.intent)
170
- // }
171
- // }
172
- //
173
- // private fun checkIntentForPushNotificationClick(intent: Intent?) {
174
- // intent?.trackPushNotificationClick { message ->
175
- // postPushNotificationClicked(message)
176
- // }
177
- // }
178
- //
179
- // private fun postPushNotificationClicked(message: RemoteMessage) {
180
- // sendEvent(
181
- // reactApplicationContext,
182
- // COURIER_PUSH_NOTIFICATION_CLICKED_EVENT,
183
- // JSONObject(message.pushNotification).toString()
184
- // )
185
- // }
186
- //
187
- //
188
- // private fun sendEvent(reactContext: ReactContext, eventName: String, params: String) {
189
- // reactContext
190
- // .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
191
- // .emit(eventName, params)
192
- // }
193
- //
194
- // @ReactMethod
195
- // fun addListener(eventName: String?) {
196
- // // Empty
197
- // }
198
- //
199
- // @ReactMethod
200
- // fun removeListeners(count: Int?) {
201
- // // Empty
202
- // }
203
- //
204
- // private fun ReadableArray.toCourierProviders(): List<CourierProvider> {
205
- // val providers: MutableList<CourierProvider> = mutableListOf()
206
- // for (provider in toArrayList()) {
207
- // CourierProvider.values().forEach {
208
- // if (it.value == provider) {
209
- // providers.add(it)
210
- // }
211
- // }
212
- // }
213
- // return providers
214
- // }
172
+
173
+ @ReactMethod
174
+ fun getFcmToken(promise: Promise) {
175
+ Courier.shared.getFCMToken(
176
+ onSuccess = { token ->
177
+ promise.resolve(token)
178
+ },
179
+ onFailure = { e ->
180
+ promise.reject(CourierEvents.COURIER_ERROR_TAG, e)
181
+ }
182
+ )
183
+ }
184
+
185
+ @ReactMethod(isBlockingSynchronousMethod = true)
186
+ fun readMessage(messageId: String): String {
187
+ Courier.shared.readMessage(messageId)
188
+ return messageId
189
+ }
190
+
191
+ @ReactMethod(isBlockingSynchronousMethod = true)
192
+ fun unreadMessage(messageId: String): String {
193
+ Courier.shared.unreadMessage(messageId)
194
+ return messageId
195
+ }
196
+
197
+ @ReactMethod
198
+ fun readAllInboxMessages(promise: Promise) {
199
+ Courier.shared.readAllInboxMessages(
200
+ onSuccess = {
201
+ promise.resolve(null)
202
+ },
203
+ onFailure = { e ->
204
+ promise.reject(CourierEvents.COURIER_ERROR_TAG, e)
205
+ }
206
+ )
207
+ }
208
+
209
+ @ReactMethod(isBlockingSynchronousMethod = true)
210
+ fun addInboxListener(): String {
211
+
212
+ val listener = Courier.shared.addInboxListener(
213
+ onInitialLoad = {
214
+ reactApplicationContext.sendEvent(
215
+ eventName = CourierEvents.Inbox.INITIAL_LOADING,
216
+ value = null
217
+ )
218
+ },
219
+ onError = { e ->
220
+ reactApplicationContext.sendEvent(
221
+ eventName = CourierEvents.Inbox.ERROR,
222
+ value = e.message ?: "Courier Inbox Error"
223
+ )
224
+ },
225
+ onMessagesChanged = { messages: List<InboxMessage>, unreadMessageCount: Int, totalMessageCount: Int, canPaginate: Boolean ->
226
+
227
+ val json = Arguments.createMap()
228
+ json.putArray("messages", messages.toWritableArray())
229
+ json.putInt("unreadMessageCount", unreadMessageCount)
230
+ json.putInt("totalMessageCount", totalMessageCount)
231
+ json.putBoolean("canPaginate", canPaginate)
232
+
233
+ reactApplicationContext.sendEvent(
234
+ eventName = CourierEvents.Inbox.MESSAGES_CHANGED,
235
+ value = json
236
+ )
237
+
238
+ }
239
+ )
240
+
241
+ val id = UUID.randomUUID().toString()
242
+ inboxListeners[id] = listener
243
+
244
+ return id
245
+
246
+ }
247
+
248
+ @ReactMethod(isBlockingSynchronousMethod = true)
249
+ fun removeInboxListener(listenerId: String): String {
250
+
251
+ // Remove the listener
252
+ val listener = inboxListeners[listenerId]
253
+ listener?.remove()
254
+
255
+ // Remove from map
256
+ inboxListeners.remove(listenerId)
257
+
258
+ return listenerId
259
+
260
+ }
261
+
262
+ @ReactMethod
263
+ fun refreshInbox(promise: Promise) {
264
+ Courier.shared.refreshInbox {
265
+ promise.resolve(null)
266
+ }
267
+ }
268
+
269
+ @ReactMethod
270
+ fun fetchNextPageOfMessages(promise: Promise) {
271
+ Courier.shared.fetchNextPageOfMessages(
272
+ onSuccess = { messages ->
273
+ promise.resolve(messages.toWritableArray())
274
+ },
275
+ onFailure = { e ->
276
+ promise.reject(CourierEvents.COURIER_ERROR_TAG, e)
277
+ }
278
+ )
279
+ }
280
+
281
+ @ReactMethod(isBlockingSynchronousMethod = true)
282
+ fun setInboxPaginationLimit(limit: Double): String {
283
+ Courier.shared.inboxPaginationLimit = limit.toInt()
284
+ return Courier.shared.inboxPaginationLimit.toString()
285
+ }
215
286
 
216
287
  }