Jetpack Compose导航无法处理PendingIntent中的DeepLink问题
解决Firebase通知PendingIntent无法触发DeepLink导航的问题
核心问题排查与修复方案
1. 修正PendingIntent创建逻辑
创建PendingIntent时,不要手动指定Component(否则会绕过DeepLink匹配规则),同时要适配Android 12+的FLAG_IMMUTABLE标记,确保Intent的Action和Data正确传递:
val bookingId = remoteMessage.data["bookingId"] if (!bookingId.isNullOrEmpty()) { val deepLinkIntent = Intent(Intent.ACTION_VIEW).apply { data = Uri.parse("https://website.com/booking?b=$bookingId") addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP) } val pendingIntent = PendingIntent.getActivity( context, 0, deepLinkIntent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE ) // 将pendingIntent绑定到通知的contentIntent }
2. 完善MainActivity的DeepLink处理
确保MainActivity在启动和接收新Intent时,都将DeepLink传递给导航控制器:
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val navController = findNavController(R.id.nav_host_fragment) intent?.takeIf { it.action == Intent.ACTION_VIEW && it.data != null }?.let { navController.handleDeepLink(it) } } override fun onNewIntent(intent: Intent?) { super.onNewIntent(intent) val navController = findNavController(R.id.nav_host_fragment) intent?.takeIf { it.action == Intent.ACTION_VIEW && it.data != null }?.let { navController.handleDeepLink(it) } }
3. 检查Manifest配置与启动模式
- 确保MainActivity的启动模式为
singleTop或singleTask,并正确配置DeepLink的Intent Filter:
<activity android:name=".MainActivity" android:launchMode="singleTop"> <intent-filter> <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" android:host="website.com" android:pathPrefix="/booking" /> </intent-filter> </activity>
- 确认导航图中的DeepLink规则参数匹配:
<deepLink app:uri="https://website.com/booking?b={bookingId}" />
4. 验证Intent传递有效性
在MainActivity中添加日志,打印收到的Intent的Data和Action,确认Firebase通知传递的DeepLink URI是否正确到达:
Log.d("DeepLinkTest", "Received intent data: ${intent.data}, action: ${intent.action}")
内容的提问来源于stack exchange,提问作者Aris Guimerá
相关产品推荐
相关产品推荐

