Skip to content

Commit 531a295

Browse files
authored
Fix BG push when isHeadlessJSTaskEnabledOnStart is disabled (#668)
1 parent 9bbd295 commit 531a295

File tree

3 files changed

+48
-43
lines changed

3 files changed

+48
-43
lines changed

android/src/main/java/com/urbanairship/reactnative/AirshipModule.kt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@ import android.annotation.SuppressLint
44
import android.os.Build
55
import com.facebook.react.bridge.*
66
import com.facebook.react.modules.core.RCTNativeAppEventEmitter
7-
import com.urbanairship.PendingResult
87
import com.urbanairship.actions.ActionResult
9-
import com.urbanairship.actions.ActionValue
108
import com.urbanairship.android.framework.proxy.EventType
11-
import com.urbanairship.android.framework.proxy.NotificationConfig
129
import com.urbanairship.android.framework.proxy.ProxyLogger
1310
import com.urbanairship.android.framework.proxy.events.EventEmitter
1411
import com.urbanairship.android.framework.proxy.proxies.AirshipProxy
@@ -19,6 +16,7 @@ import com.urbanairship.android.framework.proxy.proxies.SuspendingPredicate
1916
import com.urbanairship.json.JsonMap
2017
import com.urbanairship.json.JsonSerializable
2118
import com.urbanairship.json.JsonValue
19+
import com.urbanairship.reactnative.ManifestUtils.isHeadlessJSTaskEnabledOnStart
2220
import kotlin.time.Duration.Companion.milliseconds
2321
import kotlinx.coroutines.*
2422
import kotlinx.coroutines.flow.filter
@@ -61,6 +59,16 @@ class AirshipModule internal constructor(val context: ReactApplicationContext) :
6159
override fun initialize() {
6260
super.initialize()
6361

62+
if (!context.isHeadlessJSTaskEnabledOnStart()) {
63+
scope.launch {
64+
EventEmitter.shared().pendingEventListener
65+
.filter { !it.type.isForeground() }
66+
.collect {
67+
AirshipHeadlessEventService.startService(context)
68+
}
69+
}
70+
}
71+
6472
scope.launch {
6573
// Background events will create a headless JS task in ReactAutopilot since
6674
// initialized wont be called until we have a JS task.
@@ -196,7 +204,7 @@ class AirshipModule internal constructor(val context: ReactApplicationContext) :
196204
proxy.channel.getChannelId()
197205
}
198206
}
199-
207+
200208
@ReactMethod
201209
override fun channelWaitForChannelId(promise: Promise) {
202210
promise.resolve(scope) {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.urbanairship.reactnative
2+
3+
import android.content.Context
4+
import android.content.pm.PackageManager
5+
6+
object ManifestUtils {
7+
8+
private const val HEADLESS_JS_TASK_ON_START_MANIFEST_KEY = "com.urbanairship.reactnative.ALLOW_HEADLESS_JS_TASK_BEFORE_MODULE"
9+
private const val EXTENDER_MANIFEST_KEY = "com.urbanairship.reactnative.AIRSHIP_EXTENDER"
10+
11+
fun Context.extenderClassName(): String? {
12+
return try {
13+
this.packageManager.getApplicationInfo(this.packageName, PackageManager.GET_META_DATA)
14+
.metaData.getString(EXTENDER_MANIFEST_KEY)
15+
} catch (e: PackageManager.NameNotFoundException) {
16+
null
17+
}
18+
}
19+
20+
fun Context.isHeadlessJSTaskEnabledOnStart(): Boolean {
21+
return try {
22+
this.packageManager.getApplicationInfo(this.packageName, PackageManager.GET_META_DATA)
23+
.metaData.getBoolean(HEADLESS_JS_TASK_ON_START_MANIFEST_KEY, true)
24+
} catch (e: PackageManager.NameNotFoundException) {
25+
true
26+
}
27+
}
28+
}
29+
30+

android/src/main/java/com/urbanairship/reactnative/ReactAutopilot.kt

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
package com.urbanairship.reactnative
44

55
import android.content.Context
6-
import android.content.pm.ApplicationInfo
7-
import android.content.pm.PackageManager
86
import com.urbanairship.UAirship
97
import com.urbanairship.analytics.Extension
108
import com.urbanairship.android.framework.proxy.BaseAutopilot
@@ -17,6 +15,8 @@ import com.urbanairship.embedded.AirshipEmbeddedInfo
1715
import com.urbanairship.embedded.AirshipEmbeddedObserver
1816
import com.urbanairship.json.JsonMap
1917
import com.urbanairship.json.jsonMapOf
18+
import com.urbanairship.reactnative.ManifestUtils.extenderClassName
19+
import com.urbanairship.reactnative.ManifestUtils.isHeadlessJSTaskEnabledOnStart
2020
import kotlinx.coroutines.CoroutineScope
2121
import kotlinx.coroutines.Dispatchers
2222
import kotlinx.coroutines.SupervisorJob
@@ -33,7 +33,7 @@ class ReactAutopilot : BaseAutopilot() {
3333
override fun onReady(context: Context, airship: UAirship) {
3434
ProxyLogger.info("Airship React Native version: %s, SDK version: %s", BuildConfig.AIRSHIP_MODULE_VERSION, UAirship.getVersion())
3535

36-
val allowHeadlessJsTaskBeforeModule = isHeadlessJSTaskEnabledOnStart(context)
36+
val allowHeadlessJsTaskBeforeModule = context.isHeadlessJSTaskEnabledOnStart()
3737
ProxyLogger.debug("ALLOW_HEADLESS_JS_TASK_BEFORE_MODULE: $allowHeadlessJsTaskBeforeModule")
3838

3939
if (allowHeadlessJsTaskBeforeModule) {
@@ -69,48 +69,15 @@ class ReactAutopilot : BaseAutopilot() {
6969

7070
@Suppress("deprecation")
7171
private fun createExtender(context: Context): AirshipExtender? {
72-
val ai: ApplicationInfo
72+
val className = context.extenderClassName() ?: return null
7373
try {
74-
ai = context.packageManager.getApplicationInfo(context.packageName, PackageManager.GET_META_DATA)
75-
76-
if (ai.metaData == null) {
77-
return null
78-
}
79-
} catch (e: PackageManager.NameNotFoundException) {
80-
return null
81-
}
82-
83-
val classname = ai.metaData.getString(EXTENDER_MANIFEST_KEY) ?: return null
84-
85-
try {
86-
val extenderClass = Class.forName(classname)
74+
val extenderClass = Class.forName(className)
8775
return extenderClass.getDeclaredConstructor().newInstance() as AirshipExtender
8876
} catch (e: Exception) {
89-
ProxyLogger.error(e, "Unable to create extender: $classname")
77+
ProxyLogger.error(e, "Unable to create extender: $className")
9078
}
9179
return null
9280
}
93-
94-
private fun isHeadlessJSTaskEnabledOnStart(context: Context): Boolean {
95-
val ai: ApplicationInfo
96-
try {
97-
ai = context.packageManager.getApplicationInfo(context.packageName, PackageManager.GET_META_DATA)
98-
99-
if (ai.metaData == null) {
100-
return true
101-
}
102-
} catch (e: PackageManager.NameNotFoundException) {
103-
return true
104-
}
105-
106-
return ai.metaData.getBoolean(HEADLESS_JS_TASK_ON_START_MANIFEST_KEY, true)
107-
}
108-
109-
110-
companion object {
111-
const val HEADLESS_JS_TASK_ON_START_MANIFEST_KEY = "com.urbanairship.reactnative.ALLOW_HEADLESS_JS_TASK_BEFORE_MODULE"
112-
const val EXTENDER_MANIFEST_KEY = "com.urbanairship.reactnative.AIRSHIP_EXTENDER"
113-
}
11481
}
11582

11683
internal class PendingEmbeddedUpdated(pending: List<AirshipEmbeddedInfo>) : Event {

0 commit comments

Comments
 (0)