DEV Community

myougaTheAxo
myougaTheAxo

Posted on

Compose Deep Link完全ガイド — NavDeepLink/URI引数/Pending Intent連携

この記事で学べること

Compose Deep Link(NavDeepLink定義、URI引数、通知からのDeep Link、テスト方法)を解説します。


NavDeepLink定義

@Composable
fun AppNavigation() {
    val navController = rememberNavController()

    NavHost(navController, startDestination = "home") {
        composable("home") { HomeScreen(navController) }

        composable(
            route = "item/{itemId}",
            arguments = listOf(navArgument("itemId") { type = NavType.IntType }),
            deepLinks = listOf(
                navDeepLink {
                    uriPattern = "https://example.com/item/{itemId}"
                    action = Intent.ACTION_VIEW
                },
                navDeepLink {
                    uriPattern = "myapp://item/{itemId}"
                }
            )
        ) { backStackEntry ->
            val itemId = backStackEntry.arguments?.getInt("itemId") ?: 0
            ItemDetailScreen(itemId)
        }

        composable(
            route = "search?query={query}",
            arguments = listOf(navArgument("query") { type = NavType.StringType; defaultValue = "" }),
            deepLinks = listOf(
                navDeepLink { uriPattern = "myapp://search?query={query}" }
            )
        ) { backStackEntry ->
            val query = backStackEntry.arguments?.getString("query") ?: ""
            SearchScreen(initialQuery = query)
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Manifest宣言

<activity android:name=".MainActivity">
    <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="example.com" />
        <data android:scheme="myapp" />
    </intent-filter>
</activity>
Enter fullscreen mode Exit fullscreen mode

通知からのDeep Link

fun createDeepLinkNotification(context: Context, itemId: Int) {
    val deepLinkIntent = Intent(
        Intent.ACTION_VIEW,
        "myapp://item/$itemId".toUri(),
        context,
        MainActivity::class.java
    )

    val pendingIntent = TaskStackBuilder.create(context).run {
        addNextIntentWithParentStack(deepLinkIntent)
        getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
    }

    val notification = NotificationCompat.Builder(context, "items")
        .setContentTitle("新しいアイテム")
        .setContentText("アイテム #$itemId が追加されました")
        .setSmallIcon(R.drawable.ic_notification)
        .setContentIntent(pendingIntent)
        .setAutoCancel(true)
        .build()

    NotificationManagerCompat.from(context).notify(itemId, notification)
}
Enter fullscreen mode Exit fullscreen mode

まとめ

機能 実装
Deep Link定義 navDeepLink { uriPattern }
カスタムスキーム myapp://path
HTTPリンク https://domain/path
通知連携 TaskStackBuilder
  • navDeepLinkでNavigation Compose内にDeep Link定義
  • カスタムスキーム+HTTPSリンク両対応
  • TaskStackBuilderで通知からのバックスタック構築
  • adb shell am startでDeep Linkテスト

8種類のAndroidアプリテンプレート(Deep Link対応)を公開しています。

テンプレート一覧Gumroad

関連記事:


I publish 8 Android app templates (Room DB, Material3, MVVM) on Gumroad.

Browse templatesGumroad

Top comments (0)