1
1
package com.urbanairship.reactnative
2
2
3
+ import android.annotation.SuppressLint
4
+ import android.os.Build
3
5
import com.facebook.react.bridge.*
4
6
import com.facebook.react.modules.core.RCTNativeAppEventEmitter
5
7
import com.urbanairship.PendingResult
@@ -51,6 +53,7 @@ class AirshipModule internal constructor(val context: ReactApplicationContext) :
51
53
}
52
54
}
53
55
56
+ @SuppressLint(" RestrictedApi" )
54
57
override fun initialize () {
55
58
super .initialize()
56
59
@@ -119,6 +122,7 @@ class AirshipModule internal constructor(val context: ReactApplicationContext) :
119
122
}
120
123
}
121
124
125
+ @SuppressLint(" RestrictedApi" )
122
126
@ReactMethod
123
127
override fun takePendingEvents (eventName : String? , isHeadlessJS : Boolean , promise : Promise ) {
124
128
promise.resolveResult {
@@ -235,14 +239,14 @@ class AirshipModule internal constructor(val context: ReactApplicationContext) :
235
239
236
240
@ReactMethod
237
241
override fun pushEnableUserNotifications (promise : Promise ) {
238
- promise.resolvePending {
242
+ promise.resolveSuspending(scope) {
239
243
proxy.push.enableUserPushNotifications()
240
244
}
241
245
}
242
246
243
247
@ReactMethod
244
248
override fun pushGetNotificationStatus (promise : Promise ) {
245
- promise.resolveResult {
249
+ promise.resolveSuspending(scope) {
246
250
proxy.push.getNotificationStatus()
247
251
}
248
252
}
@@ -340,8 +344,12 @@ class AirshipModule internal constructor(val context: ReactApplicationContext) :
340
344
341
345
@ReactMethod
342
346
override fun pushAndroidIsNotificationChannelEnabled (channel : String? , promise : Promise ) {
343
- promise.resolveResult {
344
- proxy.push.isNotificationChannelEnabled(requireNotNull(channel))
347
+ promise.resolveSuspending(scope) {
348
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
349
+ proxy.push.isNotificationChannelEnabled(requireNotNull(channel))
350
+ } else {
351
+ true
352
+ }
345
353
}
346
354
}
347
355
@@ -671,16 +679,9 @@ class AirshipModule internal constructor(val context: ReactApplicationContext) :
671
679
672
680
@ReactMethod
673
681
override fun featureFlagManagerFlag (flagName : String? , promise : Promise ) {
674
- promise.resolveDeferred { callback ->
675
- scope.launch {
676
- try {
677
- requireNotNull(flagName)
678
- val flag = proxy.featureFlagManager.flag(flagName)
679
- callback(flag, null )
680
- } catch (e: Exception ) {
681
- callback(null , e)
682
- }
683
- }
682
+ promise.resolveSuspending(scope) {
683
+ requireNotNull(flagName)
684
+ proxy.featureFlagManager.flag(flagName)
684
685
}
685
686
}
686
687
@@ -692,6 +693,30 @@ class AirshipModule internal constructor(val context: ReactApplicationContext) :
692
693
}
693
694
}
694
695
696
+ override fun liveActivityList (request : ReadableMap ? , promise : Promise ) {
697
+ promise.resolveResult {
698
+ throw IllegalStateException (" Not supported on Android" )
699
+ }
700
+ }
701
+
702
+ override fun liveActivityCreate (request : ReadableMap ? , promise : Promise ) {
703
+ promise.resolveResult {
704
+ throw IllegalStateException (" Not supported on Android" )
705
+ }
706
+ }
707
+
708
+ override fun liveActivityUpdate (request : ReadableMap ? , promise : Promise ) {
709
+ promise.resolveResult {
710
+ throw IllegalStateException (" Not supported on Android" )
711
+ }
712
+ }
713
+
714
+ override fun liveActivityEnd (request : ReadableMap ? , promise : Promise ) {
715
+ promise.resolveResult {
716
+ throw IllegalStateException (" Not supported on Android" )
717
+ }
718
+ }
719
+
695
720
private fun notifyPending () {
696
721
if (context.hasActiveReactInstance()) {
697
722
val appEventEmitter = context.getJSModule(RCTNativeAppEventEmitter ::class .java)
@@ -720,6 +745,30 @@ internal fun Promise.resolveResult(function: () -> Any?) {
720
745
resolveDeferred<Any > { callback -> callback(function(), null ) }
721
746
}
722
747
748
+ internal fun Promise.resolveSuspending (scope : CoroutineScope , function : suspend () -> Any? ) {
749
+ scope.launch {
750
+ try {
751
+ when (val result = function()) {
752
+ is Unit -> {
753
+ this @resolveSuspending.resolve(null )
754
+ }
755
+ is JsonSerializable -> {
756
+ this @resolveSuspending.resolve(result.toReactType())
757
+ }
758
+ is Number -> {
759
+ this @resolveSuspending.resolve(result.toDouble())
760
+ }
761
+ else -> {
762
+ this @resolveSuspending.resolve(result)
763
+ }
764
+ }
765
+ } catch (e: Exception ) {
766
+ this @resolveSuspending.reject(" AIRSHIP_ERROR" , e)
767
+ }
768
+ }
769
+
770
+ }
771
+
723
772
internal fun <T > Promise.resolveDeferred (function : ((T ? , Exception ? ) -> Unit ) -> Unit ) {
724
773
try {
725
774
function { result, error ->
0 commit comments