Skip to content

Commit 5721ab2

Browse files
authored
Fix LiveUpdate isFeatureEnabled check and push listener (#1333)
1 parent 45b59f3 commit 5721ab2

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

urbanairship-live-update/src/main/java/com/urbanairship/liveupdate/LiveUpdateManager.kt

+9-11
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import com.urbanairship.config.AirshipRuntimeConfig
1616
import com.urbanairship.json.JsonMap
1717
import com.urbanairship.liveupdate.data.LiveUpdateDatabase
1818
import com.urbanairship.liveupdate.notification.LiveUpdatePayload
19-
import com.urbanairship.push.PushListener
2019
import com.urbanairship.push.PushManager
2120

2221
/**
@@ -38,13 +37,7 @@ internal constructor(
3837
) : AirshipComponent(context, dataStore) {
3938

4039
private val isFeatureEnabled: Boolean
41-
get() = privacyManager.isEnabled(FEATURE_PUSH) && channel.id != null
42-
43-
private val pushListener = PushListener { message, _ ->
44-
message.liveUpdatePayload
45-
?.let { LiveUpdatePayload.fromJson(it) }
46-
?.let { registrar.onLiveUpdatePushReceived(message, it) }
47-
}
40+
get() = privacyManager.isEnabled(FEATURE_PUSH)
4841

4942
public constructor(
5043
context: Context,
@@ -158,7 +151,15 @@ internal constructor(
158151
public override fun init() {
159152
super.init()
160153

154+
channel.addChannelListener { updateLiveActivityEnablement() }
161155
privacyManager.addListener { updateLiveActivityEnablement() }
156+
157+
pushManager.addPushListener { message, _ ->
158+
message.liveUpdatePayload
159+
?.let { LiveUpdatePayload.fromJson(it) }
160+
?.let { registrar.onLiveUpdatePushReceived(message, it) }
161+
}
162+
162163
updateLiveActivityEnablement()
163164
}
164165

@@ -169,16 +170,13 @@ internal constructor(
169170

170171
private fun updateLiveActivityEnablement() {
171172
if (isFeatureEnabled) {
172-
pushManager.addPushListener(pushListener)
173-
174173
// Check for any active live Updates that have had their notifications cleared.
175174
// This makes sure we'll end the live update if the notification is dropped due
176175
// to an app upgrade or other cases where we don't get notified of the dismiss.
177176
registrar.stopLiveUpdatesForClearedNotifications()
178177
} else {
179178
// Clear all live updates.
180179
registrar.clearAll()
181-
pushManager.removePushListener(pushListener)
182180
}
183181
}
184182

urbanairship-live-update/src/test/java/com/urbanairship/liveupdate/LiveUpdateManagerTest.kt

+6-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import com.urbanairship.liveupdate.data.LiveUpdateDatabase
1313
import com.urbanairship.push.PushManager
1414
import io.mockk.every
1515
import io.mockk.mockk
16+
import io.mockk.verify
1617
import junit.framework.TestCase.assertTrue
1718
import org.junit.Before
1819
import org.junit.Test
@@ -26,10 +27,8 @@ public class LiveUpdateManagerTest {
2627
every { platform } returns ANDROID_PLATFORM
2728
every { requestSession } returns TestRequestSession()
2829
}
29-
private val pushManager: PushManager = mockk(relaxed = true)
30-
private val channel: AirshipChannel = mockk {
31-
every { id } returns "channelId"
32-
}
30+
private val pushManager: PushManager = mockk(relaxUnitFun = true)
31+
private val channel: AirshipChannel = mockk(relaxed = true)
3332
private val dao: LiveUpdateDao = mockk()
3433
private val database: LiveUpdateDatabase = mockk {
3534
every { liveUpdateDao() } returns dao
@@ -62,5 +61,8 @@ public class LiveUpdateManagerTest {
6261
public fun testInit() {
6362
liveUpdateManager.init()
6463
assertTrue(liveUpdateManager.isComponentEnabled)
64+
65+
verify { pushManager.addPushListener(any()) }
66+
verify { channel.addChannelListener(any()) }
6567
}
6668
}

0 commit comments

Comments
 (0)