Jetpack Compose中Firebase动态链接Deeplink处理问题
电商APP Firebase动态链接 Deeplink 处理问题
问题背景
我正在开发一款电商APP,支持用户通过消息、邮件等方式分享商品链接。核心需求是:接收方点击分享链接时,APP启动并跳转到对应商品详情页,通过URI中的商品ID获取商品信息。
我使用Firebase动态链接生成短链接,最初点击消息APP里的链接时,APP会在消息APP内打开而非独立启动,将Activity启动模式改为android:launchMode="singleTask"后解决了这个问题,但新问题出现:当APP处于后台时,点击链接无任何响应。
当前临时实现
我不确定如何正确实现override fun onNewIntent(intent: Intent?)处理Deeplink,目前用了一个临时方案,但不确定是否合规:
class MainActivity : ComponentActivity() { override fun onNewIntent(intent: Intent?) { super.onNewIntent(intent) val pendingIntent = TaskStackBuilder.create(applicationContext).run { addNextIntentWithParentStack(intent) getPendingIntent( 0, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE ) } pendingIntent.send() } }
Manifest配置
<activity android:launchMode="singleTask" android:name=".presentation.MainActivity" android:exported="true" android:theme="@style/Theme.B2CApp"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <intent-filter android:autoVerify="true"> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="https" /> <data android:host="*****.page.link" /> </intent-filter> </activity>
Deeplink导航配置
composable( CategoryRoute.ProductDetail.route, deepLinks = listOf( navDeepLink { uriPattern = "****.page.link/{$DEEP_LINK}" action = Intent.ACTION_VIEW } ), arguments = listOf( navArgument(PRODUCT_ID) { type = NavType.StringType defaultValue = "" }, navArgument(DEEP_LINK) { type = NavType.StringType defaultValue = "" } ) )
我尝试在onNewIntent中调用navHostController处理Deeplink,但MainActivity里无法访问该控制器。
更新后的实现及新Bug
按照@Watermelon的建议修改后,Manifest未改动,MainActivity代码如下:
@AndroidEntryPoint class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { val newIntent by produceState(initialValue = null as Intent?) { val consumer = androidx.core.util.Consumer<Intent> { this.value = it } addOnNewIntentListener(consumer) awaitDispose { removeOnNewIntentListener(consumer) } } B2CTheme { MainScreen(newIntent) } } } }
MainScreen代码:
@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter") @OptIn(ExperimentalMaterial3Api::class) @Composable fun MainScreen(newIntent: Intent?) { val navController = rememberNavController() val systemUiController = rememberSystemUiController() val currentIntent = rememberUpdatedState(newValue = newIntent) LaunchedEffect(Unit) { snapshotFlow { currentIntent.value }.filterNotNull() .map { /* do extra jobs*/ println(it.dataString) it } .collect{ navController.handleDeepLink(it) } } SideEffect { systemUiController.setStatusBarColor( color = LightColorScheme.surface, darkIcons = true ) } Scaffold( bottomBar = { B2CBottomBar(navController) { navController.navigate(it.route) { launchSingleTop = true popUpTo(it.route) { inclusive = true } } } } ) { MainNavHost( navController = navController, Modifier.padding(top = it.calculateTopPadding())) } }
现在新Bug出现:用户从外部点击链接后,APP正常打开并显示商品详情页,点击返回键回到首页;但切换其他底部导航栏标签后再切回首页,仍会显示该商品详情页。
内容的提问来源于stack exchange,提问作者Dave Kababyan
相关产品推荐
相关产品推荐

