-
Notifications
You must be signed in to change notification settings - Fork 3
Add getLaunchDeepLink for cold start deep links #138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6ca0eec
Fix FeatureTests compile against SDK 20 (AirshipJSONUtils removed)
crow 0ea3b37
Add iOS LaunchDeepLinkTracker
crow 8f27936
Wire launch deep link tracking into iOS proxy
crow 2f980a5
Add Android LaunchDeepLinkTracker
crow 46765c6
Wire launch deep link tracking into Android proxy
crow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...ork-proxy/src/main/java/com/urbanairship/android/framework/proxy/LaunchDeepLinkTracker.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| /* Copyright Airship and Contributors */ | ||
|
|
||
| package com.urbanairship.android.framework.proxy | ||
|
|
||
| import com.urbanairship.push.PushMessage | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.flow.first | ||
| import kotlinx.coroutines.flow.getAndUpdate | ||
|
|
||
| /** | ||
| * Tracks the deep link that launched the app from a notification tap. | ||
| * | ||
| * The stash is consume-once and expires after a short staleness window so a | ||
| * JS reload can't replay an old launch link. | ||
| */ | ||
| public class LaunchDeepLinkTracker internal constructor( | ||
| private val clock: () -> Long = { System.currentTimeMillis() } | ||
| ) { | ||
|
|
||
| private data class Stash(val deepLink: String, val timeMs: Long) | ||
|
|
||
| private val stash = MutableStateFlow<Stash?>(null) | ||
| private val launchResolved = MutableStateFlow(false) | ||
|
|
||
| /** | ||
| * Called when a notification is opened, before the SDK runs the | ||
| * notification's actions. Resolves the launch, stashing the payload's | ||
| * deep link when the app was not already foregrounded. | ||
| */ | ||
| internal fun onNotificationOpened(message: PushMessage, isAppForegrounded: Boolean) { | ||
| if (!isAppForegrounded) { | ||
| DEEP_LINK_ACTION_KEYS.firstNotNullOfOrNull { message.actions[it]?.string }?.let { | ||
| stash.value = Stash(it, clock()) | ||
| } | ||
| } | ||
| launchResolved.value = true | ||
| } | ||
|
|
||
| /** | ||
| * Resolves the launch without a deep link. Called once the app is | ||
| * foregrounded so normal launches resolve null without waiting. | ||
| */ | ||
| internal fun markLaunchResolved() { | ||
| launchResolved.value = true | ||
| } | ||
|
|
||
| /** | ||
| * Returns the deep link that launched the app, or null. Consumes the value. | ||
| */ | ||
| public suspend fun takeLaunchDeepLink(): String? { | ||
| take()?.let { return it } | ||
| launchResolved.first { it } | ||
| return take() | ||
| } | ||
|
|
||
| private fun take(): String? { | ||
| val current = stash.getAndUpdate { null } ?: return null | ||
| return if (clock() - current.timeMs <= MAX_STASH_AGE_MS) current.deepLink else null | ||
| } | ||
|
|
||
| public companion object { | ||
| private val DEEP_LINK_ACTION_KEYS = listOf("^d", "deep_link_action") | ||
| private const val MAX_STASH_AGE_MS = 10_000L | ||
|
|
||
| private val sharedInstance = LaunchDeepLinkTracker() | ||
|
|
||
| /** | ||
| * Shared tracker instance. | ||
| */ | ||
| @JvmStatic | ||
| public fun shared(): LaunchDeepLinkTracker = sharedInstance | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
...proxy/src/test/java/com/urbanairship/android/framework/proxy/LaunchDeepLinkTrackerTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| package com.urbanairship.android.framework.proxy | ||
|
|
||
| import androidx.core.os.bundleOf | ||
| import com.urbanairship.push.PushMessage | ||
| import kotlinx.coroutines.async | ||
| import kotlinx.coroutines.test.runTest | ||
| import kotlinx.coroutines.yield | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Assert.assertNull | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.robolectric.RobolectricTestRunner | ||
|
|
||
| @RunWith(RobolectricTestRunner::class) | ||
| public class LaunchDeepLinkTrackerTest { | ||
|
|
||
| private fun message(deepLink: String? = null, key: String = "^d"): PushMessage { | ||
| val extras = if (deepLink != null) { | ||
| bundleOf(PushMessage.EXTRA_ACTIONS to """{"$key":"$deepLink"}""") | ||
| } else { | ||
| bundleOf() | ||
| } | ||
| return PushMessage(extras) | ||
| } | ||
|
|
||
| @Test | ||
| public fun testTapWithDeepLinkStashes(): Unit = runTest { | ||
| val tracker = LaunchDeepLinkTracker() | ||
| tracker.onNotificationOpened(message("myapp://home"), isAppForegrounded = false) | ||
| assertEquals("myapp://home", tracker.takeLaunchDeepLink()) | ||
| } | ||
|
|
||
| @Test | ||
| public fun testConsumeOnce(): Unit = runTest { | ||
| val tracker = LaunchDeepLinkTracker() | ||
| tracker.onNotificationOpened(message("myapp://home"), isAppForegrounded = false) | ||
| tracker.takeLaunchDeepLink() | ||
| assertNull(tracker.takeLaunchDeepLink()) | ||
| } | ||
|
|
||
| @Test | ||
| public fun testLongActionNameKey(): Unit = runTest { | ||
| val tracker = LaunchDeepLinkTracker() | ||
| tracker.onNotificationOpened( | ||
| message("myapp://home", key = "deep_link_action"), | ||
| isAppForegrounded = false | ||
| ) | ||
| assertEquals("myapp://home", tracker.takeLaunchDeepLink()) | ||
| } | ||
|
|
||
| @Test | ||
| public fun testForegroundTapSkipsStash(): Unit = runTest { | ||
| val tracker = LaunchDeepLinkTracker() | ||
| tracker.onNotificationOpened(message("myapp://home"), isAppForegrounded = true) | ||
| assertNull(tracker.takeLaunchDeepLink()) | ||
| } | ||
|
|
||
| @Test | ||
| public fun testTapWithoutDeepLinkResolvesNull(): Unit = runTest { | ||
| val tracker = LaunchDeepLinkTracker() | ||
| tracker.onNotificationOpened(message(), isAppForegrounded = false) | ||
| assertNull(tracker.takeLaunchDeepLink()) | ||
| } | ||
|
|
||
| @Test | ||
| public fun testWaiterResolvedByLaterTap(): Unit = runTest { | ||
| val tracker = LaunchDeepLinkTracker() | ||
| val pending = async { tracker.takeLaunchDeepLink() } | ||
| yield() | ||
| tracker.onNotificationOpened(message("myapp://home"), isAppForegrounded = false) | ||
| assertEquals("myapp://home", pending.await()) | ||
| } | ||
|
|
||
| @Test | ||
| public fun testWaiterResolvedNullOnLaunchResolved(): Unit = runTest { | ||
| val tracker = LaunchDeepLinkTracker() | ||
| val pending = async { tracker.takeLaunchDeepLink() } | ||
| yield() | ||
| tracker.markLaunchResolved() | ||
| assertNull(pending.await()) | ||
| } | ||
|
|
||
| @Test | ||
| public fun testStaleStashReturnsNull(): Unit = runTest { | ||
| var now = 0L | ||
| val tracker = LaunchDeepLinkTracker(clock = { now }) | ||
| tracker.onNotificationOpened(message("myapp://home"), isAppForegrounded = false) | ||
| now = 10_001L | ||
| assertNull(tracker.takeLaunchDeepLink()) | ||
| } | ||
|
|
||
| @Test | ||
| public fun testFreshStashReturnsLink(): Unit = runTest { | ||
| var now = 0L | ||
| val tracker = LaunchDeepLinkTracker(clock = { now }) | ||
| tracker.onNotificationOpened(message("myapp://home"), isAppForegrounded = false) | ||
| now = 9_999L | ||
| assertEquals("myapp://home", tracker.takeLaunchDeepLink()) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.