当用户在我们的应用程序中输入地理围栏时,我们会向他们显示一个关于该区域的报价通知,单击该通知时,应将他们 bootstrap 到一个名为SingleNotification的特定可组合屏幕.我一直在关注google的codelabdocumentation,但我还没有成功地导航到特定的屏幕.现在,单击通知或运行adb shell am start -d “eway://station_offers/date_str/www.test.com/TITLE/CONTENT” -a android.intent.action.VIEW命令即可打开应用程序.

该活动在 list 中声明如下:

    <activity
        android:name=".MainActivity"
        android:exported="true"
        android:label="@string/app_name"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
        </intent-filter>

        <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:host="station_offers"
                android:scheme="eway" />
        </intent-filter>
    </activity>

我们的MainNavController类包含NavHost,而NavHost又包含各种NavGraph.我只包括以下相关图表:

        NavHost(
            navController = navController,
            startDestination = NavigationGraphs.SPLASH_SCREEN.route
        ) {
....
            notificationsNavigation()
....    
    }

notificationsNavigation图定义如下:

fun NavGraphBuilder.notificationsNavigation() {
    navigation(
        startDestination = Screens.NOTIFICATION_DETAILS.navRoute,
        route = NavigationGraphs.NOTIFICATIONS.route
    ) {
        composable(
            route = "${Screens.NOTIFICATION_DETAILS.navRoute}/{date}/{imageUrl}/{title}/{content}",
            arguments = listOf(
                navArgument("date") { type = NavType.StringType },
                navArgument("imageUrl") { type = NavType.StringType },
                navArgument("title") { type = NavType.StringType },
                navArgument("content") { type = NavType.StringType }
            ),
            deepLinks = listOf(navDeepLink {
                uriPattern = "eway://${Screens.NOTIFICATION_DETAILS.navRoute}/{date}/{imageUrl}/{title}/{content}"
            })
        ) { backstackEntry ->
            val args = backstackEntry.arguments
            SingleNotification(
                date = args?.getString("date")!!,
                imageUrl = args.getString("imageUrl")!!,
                title = args.getString("title")!!,
                description = args.getString("content")!!
            )
        }
    }
}

Screes.NOTIFICATION_DETAILS.navRoute对应notification_details的值.

在geo fence广播接收器内,我构建了如下待定意图:

                        val deepLinkIntent = Intent(
                            Intent.ACTION_VIEW,
                            "eway://station_offers/${
                                offer.date
                            }/${
                                offer.image
                            }/${offer.title}/${offer.content}".toUri(),
                            context,
                            MainActivity::class.java
                        )
                        val deepLinkPendingIntent: PendingIntent =
                            TaskStackBuilder.create(context!!).run {
                                addNextIntentWithParentStack(deepLinkIntent)
                                getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)!!
                            }
                        showNotification(offer.title, offer.content, deepLinkPendingIntent)

我想不出我在这里遗漏了什么.

推荐答案

好的,在多次测试和运行Google相关代码实验室的解决方案后,我找到了如何使其工作的方法.

        <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:host="notification_details"
                android:scheme="eway" />
        </intent-filter>

其次,深度链接的uri模式应该与可组合的路由格式相匹配.在这种情况下,由于组合器的路由被定义为route = "${Screens.NOTIFICATION_DETAILS.navRoute}/{date}/{imageUrl}/{title}/{content}",因此正确的深度链接uriPattern将是:

deepLinks = listOf(navDeepLink {
                    uriPattern =
                        "eway://${Screens.NOTIFICATION_DETAILS.navRoute}/{date}/{imageUrl}/{title}/{content}"
                })

此外,可组合目的地似乎必须在NavHost本身中声明,而不是在NavGraph中声明.正如您所看到的,最初我认为系统能够通过嵌套的NavGraph找到目的地,但它不能(引发了一个相对异常),因此我得出结论,必须这样做(就像在代码实验室中所做的那样).如果我错了,请纠正我!

最后,我相应地更改了GeofenceBroadcastReceiver中的val uri定义.现在看来是这样的:

val uri = "eway://${Screens.NOTIFICATION_DETAILS.navRoute}/${
                                    offer.date.replace(
                                        "/",
                                        "@"
                                    )
                                }/${
                                    offer.image.replace(
                                        "/",
                                        "@"
                                    )
                                }/${offer.title}/${offer.content.replace("/", "@")}".toUri()

综上所述,就我的理解而言,这些步骤似乎可以解决这个问题:

  1. 深度链接的目标组件必须是主NavHost的直接子级
  2. AndroidManifest的android:host应该与objective-c omposable的路由匹配,最后,
  3. 深度链接的Uri模式应该与objective-c omposable的路由相匹配(如果使用格式scheme://host/....,那么遵循数字2就可以了)

Android相关问答推荐

如何正确增加LazyStream中的变量

在Android上使用XSLT文件转换XML文件

使用Android Jetpack Compose,为子Composable定义ViewModel是不是一种糟糕的做法?

为什么柱子的高度不都一样?

在内部创建匿名对象的繁忙循环调用函数会产生开销吗?

在androidStudio中,如何使用带有ResolutionStrategy的ResolutionSelector而不是setTargetResolve()?

SDK 33 的问题 - 面向 S+(版本 31 及更高版本)要求在创建 PendingIntent 时指定 FLAG_IMMUTABLE 或 FLAG_MUTABLE 之一

Android v31 及更低版本中 ImageView 中的圆角

解决失败:Landroidx/compose/runtime/PrimitiveSnapshotStateKt

是否可以在 Android 应用程序的 Wifi 设置中为 DNS 服务器设置自定义 IP?

Android Studio 在 list 文件中已经声明了活动类,但仍出现无法找到明确的活动类的错误

Jetpack Compose 如何使一个项目相对于另一个项目垂直居中

发布 MAUI 应用程序时出现遇到重复程序集错误

为什么集成React Native时compileSdkVersion错误?

Android:ActivityCompat.requestPermissions 不显示弹窗(Android 13,targetSdkVersion=33)

Compose Accompaniist Pager 中的 TabRow/Tab 重组问题

有人可以向我解释这两个结果有什么区别吗?

是什么让 Android Studio 中的按钮变成紫色?加上新手的其他奇怪行为

如何在 Android Studio 中创建新的可组合函数?

如果我在网络请求中指定它们是否与判断网络功能相关