Skip to content

Commit

Permalink
Live Activities (#595)
Browse files Browse the repository at this point in the history
* Add LA support

* Fixes

* Use attributesType to match iOS APNS api

* Update docs

* Update latest proxy

* Fix android

* Use actual pod
  • Loading branch information
rlepinski authored Sep 30, 2024
1 parent aefacc5 commit 3a0ca80
Show file tree
Hide file tree
Showing 32 changed files with 934 additions and 147 deletions.
2 changes: 1 addition & 1 deletion android/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ Airship_minSdkVersion=21
Airship_targetSdkVersion=34
Airship_compileSdkVersion=34
Airship_ndkversion=26.1.10909125
Airship_airshipProxyVersion=7.3.0
Airship_airshipProxyVersion=8.3.0
77 changes: 63 additions & 14 deletions android/src/main/java/com/urbanairship/reactnative/AirshipModule.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.urbanairship.reactnative

import android.annotation.SuppressLint
import android.os.Build
import com.facebook.react.bridge.*
import com.facebook.react.modules.core.RCTNativeAppEventEmitter
import com.urbanairship.PendingResult
Expand Down Expand Up @@ -51,6 +53,7 @@ class AirshipModule internal constructor(val context: ReactApplicationContext) :
}
}

@SuppressLint("RestrictedApi")
override fun initialize() {
super.initialize()

Expand Down Expand Up @@ -119,6 +122,7 @@ class AirshipModule internal constructor(val context: ReactApplicationContext) :
}
}

@SuppressLint("RestrictedApi")
@ReactMethod
override fun takePendingEvents(eventName: String?, isHeadlessJS: Boolean, promise: Promise) {
promise.resolveResult {
Expand Down Expand Up @@ -235,14 +239,14 @@ class AirshipModule internal constructor(val context: ReactApplicationContext) :

@ReactMethod
override fun pushEnableUserNotifications(promise: Promise) {
promise.resolvePending {
promise.resolveSuspending(scope) {
proxy.push.enableUserPushNotifications()
}
}

@ReactMethod
override fun pushGetNotificationStatus(promise: Promise) {
promise.resolveResult {
promise.resolveSuspending(scope) {
proxy.push.getNotificationStatus()
}
}
Expand Down Expand Up @@ -340,8 +344,12 @@ class AirshipModule internal constructor(val context: ReactApplicationContext) :

@ReactMethod
override fun pushAndroidIsNotificationChannelEnabled(channel: String?, promise: Promise) {
promise.resolveResult {
proxy.push.isNotificationChannelEnabled(requireNotNull(channel))
promise.resolveSuspending(scope) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
proxy.push.isNotificationChannelEnabled(requireNotNull(channel))
} else {
true
}
}
}

Expand Down Expand Up @@ -671,16 +679,9 @@ class AirshipModule internal constructor(val context: ReactApplicationContext) :

@ReactMethod
override fun featureFlagManagerFlag(flagName: String?, promise: Promise) {
promise.resolveDeferred { callback ->
scope.launch {
try {
requireNotNull(flagName)
val flag = proxy.featureFlagManager.flag(flagName)
callback(flag, null)
} catch (e: Exception) {
callback(null, e)
}
}
promise.resolveSuspending(scope) {
requireNotNull(flagName)
proxy.featureFlagManager.flag(flagName)
}
}

Expand All @@ -692,6 +693,30 @@ class AirshipModule internal constructor(val context: ReactApplicationContext) :
}
}

override fun liveActivityList(request: ReadableMap?, promise: Promise) {
promise.resolveResult {
throw IllegalStateException("Not supported on Android")
}
}

override fun liveActivityCreate(request: ReadableMap?, promise: Promise) {
promise.resolveResult {
throw IllegalStateException("Not supported on Android")
}
}

override fun liveActivityUpdate(request: ReadableMap?, promise: Promise) {
promise.resolveResult {
throw IllegalStateException("Not supported on Android")
}
}

override fun liveActivityEnd(request: ReadableMap?, promise: Promise) {
promise.resolveResult {
throw IllegalStateException("Not supported on Android")
}
}

private fun notifyPending() {
if (context.hasActiveReactInstance()) {
val appEventEmitter = context.getJSModule(RCTNativeAppEventEmitter::class.java)
Expand Down Expand Up @@ -720,6 +745,30 @@ internal fun Promise.resolveResult(function: () -> Any?) {
resolveDeferred<Any> { callback -> callback(function(), null) }
}

internal fun Promise.resolveSuspending(scope: CoroutineScope, function: suspend () -> Any?) {
scope.launch {
try {
when (val result = function()) {
is Unit -> {
this@resolveSuspending.resolve(null)
}
is JsonSerializable -> {
this@resolveSuspending.resolve(result.toReactType())
}
is Number -> {
this@resolveSuspending.resolve(result.toDouble())
}
else -> {
this@resolveSuspending.resolve(result)
}
}
} catch (e: Exception) {
this@resolveSuspending.reject("AIRSHIP_ERROR", e)
}
}

}

internal fun <T> Promise.resolveDeferred(function: ((T?, Exception?) -> Unit) -> Unit) {
try {
function { result, error ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,22 @@ abstract class AirshipSpec internal constructor(context: ReactApplicationContext
@ReactMethod
@com.facebook.proguard.annotations.DoNotStrip
abstract fun featureFlagManagerTrackInteraction(flag: ReadableMap?, promise: Promise)

@ReactMethod
@com.facebook.proguard.annotations.DoNotStrip
abstract fun liveActivityList(request: ReadableMap?, promise: Promise)

@ReactMethod
@com.facebook.proguard.annotations.DoNotStrip
abstract fun liveActivityCreate(request: ReadableMap?, promise: Promise)

@ReactMethod
@com.facebook.proguard.annotations.DoNotStrip
abstract fun liveActivityUpdate(request: ReadableMap?, promise: Promise)

@ReactMethod
@com.facebook.proguard.annotations.DoNotStrip
abstract fun liveActivityEnd(request: ReadableMap?, promise: Promise)
}


Loading

0 comments on commit 3a0ca80

Please sign in to comment.