打开 Android 13 通知中的深链接以打开可组合屏幕。

huangapple go评论62阅读模式
英文:

Open composable screen from notification using deeplink Android 13

问题

Android 13 设备

我试图从通知的深链接中打开一个可组合的屏幕。然而,浏览器应用程序一直打开,而不是应用程序,然后导航到正确的屏幕。

我有以下的可组合屏幕,在其中我指定了深链接。

composable(
    route = "event_screen?id={id}&menuActionType={menuActionType}",
    arguments = listOf(navArgument("id") {
        type = NavType.StringType
        nullable = true
        defaultValue = null
    }, navArgument("menuActionType") {
        type = NavType.StringType
        defaultValue = AgendaMenuActionType.OPEN.name
    }),
    deepLinks = listOf(navDeepLink {
        this.uriPattern = "https://androidbox.me/event_screen/{id}"
        action = Intent.ACTION_VIEW
    })
)

在我的 AlarmReceiver 中,我正在创建要在通知中使用的待定意图。通知正常触发,但当我点击它时,它会打开浏览器应用程序。

val agendaIntent = Intent(
    Intent.ACTION_VIEW,
    Uri.parse("https://androidbox.me/event_screen/597ef216-2ec6-435b-9a48-cfcf0e3caa46"))

val pendingIntent = TaskStackBuilder.create(context).run {
    this.addNextIntentWithParentStack(agendaIntent)
    this.getPendingIntent(agendaId.hashCode(), PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_CANCEL_CURRENT)
}

val notification = NotificationCompat.Builder(context, channelId)
    .setContentTitle(title)
    .setContentText(description)
    .setSmallIcon(R.drawable.bell)
    .setAutoCancel(true)
    .setContentIntent(pendingIntent)
    .build()

notificationManager.notify(agendaId.hashCode(), notification)

在我的清单中,我已经指定了如下:

<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:host="androidbox.me"
        android:scheme="https">
    </data>
</intent-filter>

当我点击通知时,它会打开浏览器。

使用以下方式进行测试:

./adb shell am start -W -a android.intent.action.VIEW -d https://androidbox.me/event_screen/597ef216-2ec6-435b-9a48-cfcf0e3caa46
Starting: Intent { act=android.intent.action.VIEW dat=https://androidbox.me/... }
Status: ok
LaunchState: WARM
Activity: com.android.chrome/org.chromium.chrome.browser.ChromeTabbedActivity
TotalTime: 140
WaitTime: 147
Complete
英文:

Android 13 Device

I am trying to open a composable screen from using a deeplink from a notification. However, the browser app keeps opening instead of the app and then navigating to the correct screen.

I have the following composable screen where I am specifying the deeplinks.

    composable(
        route = &quot;event_screen?id={id}&amp;menuActionType={menuActionType}&quot;,
        arguments = listOf(navArgument(&quot;id&quot;) {
            type = NavType.StringType
            nullable = true
            defaultValue = null
        }, navArgument(&quot;menuActionType&quot;) {
            type = NavType.StringType
            defaultValue = AgendaMenuActionType.OPEN.name
        }),
        deepLinks = listOf(navDeepLink {
            this.uriPattern = &quot;https://androidbox.me/event_screen/{id}&quot;
            action = Intent.ACTION_VIEW
        })
    )

In my AlarmReceiver I am creating the pending Intent to be used in the notification. The notification triggers ok, but when I tap on it will open the browser app.

    val agendaIntent = Intent(
        Intent.ACTION_VIEW,
        Uri.parse(&quot;https://androidbox.me/event_screen/597ef216-2ec6-435b-9a48-cfcf0e3caa46&quot;))

    val pendingIntent = TaskStackBuilder.create(context).run {
        this.addNextIntentWithParentStack(agendaIntent)
        this.getPendingIntent(agendaId.hashCode(), PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_CANCEL_CURRENT)
    }

    val notification = NotificationCompat.Builder(context, channelId)
        .setContentTitle(title)
        .setContentText(description)
        .setSmallIcon(R.drawable.bell)
        .setAutoCancel(true)
        .setContentIntent(pendingIntent)
        .build()

    notificationManager.notify(agendaId.hashCode(), notification)

And in my manifect I have specified like this:

   &lt;intent-filter android:autoVerify=&quot;true&quot;&gt;
        &lt;action android:name=&quot;android.intent.action.VIEW&quot; /&gt;
        &lt;category android:name=&quot;android.intent.category.DEFAULT&quot; /&gt;
        &lt;category android:name=&quot;android.intent.category.BROWSABLE&quot; /&gt;

        &lt;data
            android:host=&quot;androidbox.me&quot;
            android:scheme=&quot;https&quot;&gt;
        &lt;/data&gt;
    &lt;/intent-filter&gt;

What happens when I tap the notification it opens the browser.

Testing with the following

./adb shell am start -W -a android.intent.action.VIEW -d https://androidbox.me/event_screen/597ef216-2ec6-435b-9a48-cfcf0e3caa46
Starting: Intent { act=android.intent.action.VIEW dat=https://androidbox.me/... }
Status: ok
LaunchState: WARM
Activity: com.android.chrome/org.chromium.chrome.browser.ChromeTabbedActivity
TotalTime: 140
WaitTime: 147
Complete

答案1

得分: 0

答案是在清单文件中添加以下内容,因为这是一个深度链接而不是应用链接:

<intent-filter android:autoVerify="true">
    <data
        android:host="androidbox.me"
        android:scheme="busbyapp">
    </data>
</intent-filter>
英文:

The answer was to have the following in the manifest as this is a deeplink not a applink

           &lt;intent-filter android:autoVerify=&quot;true&quot;&gt;
                &lt;data
                    android:host=&quot;androidbox.me&quot;
                    android:scheme=&quot;busbyapp&quot;&gt;
                &lt;/data&gt;
            &lt;/intent-filter&gt;

huangapple
  • 本文由 发表于 2023年7月4日 23:57:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76614281.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定