アシスタントに動的ショートカットをプッシュする

Android のショートカットを使用すると、 操作を実行したり、アプリ内のコンテンツにアクセスしたりするためのメソッドです。 アシスタントは、Android の動的ショートカットを ユーザーが簡単に検索して再生できるように 音声対応機能を利用できます。

たとえば、ユーザーが作成したメモごとにショートカットをプッシュ メモ作成アプリ収益額 アシスタントなどの Google サーフェスに表示されるダイナミック リンク Google Shortcuts Integration Jetpack ライブラリをプロジェクトに追加します。 このライブラリを使用すると、 ShortcutManagerCompat クラス: ShortcutManager API。

アプリで Google Shortcuts Integration ライブラリを使用する場合、 Google にプッシュしたショートカットは、音声ショートカットの候補としてユーザーに表示されます 。動的ショートカットをいくつでもプッシュできる Google Cloud の pushDynamicShortcut() メソッドを使用するアシスタント ShortcutManagerCompat ライブラリ。

開発プロジェクトを構成する

動的ショートカット機能をアプリに追加するには、Android Jetpack ライブラリである Google Shortcuts Integration Library が必要です。このセクションでは、アプリ開発プロジェクトを次のように構成する方法について説明します。 使用できます。

この Jetpack ライブラリを追加してプロジェクトを構成する手順は次のとおりです。

  1. Jetpack ライブラリを処理するように、gradle.properties ファイルを更新します。

    gradle.properties

    android.useAndroidX=true
    # Automatically convert third-party libraries to use AndroidX
    android.enableJetifier=true
    
  2. Jetpack ライブラリの依存関係を build.gradle に追加します。

    app/build.gradle

    dependencies {
     implementation "androidx.core:core:1.6.0"
     implementation "androidx.core:core-google-shortcuts:1.0.1"
     ...
    }
    

    上記のサンプルコードでは、依存関係として 2 つの Jetpack ライブラリをリストしています。androidx.core:core:1.6.0 ライブラリには、 ShortcutManagerCompat クラス: 動的ショートカットを Google

    androidx.core:core-google-shortcuts:1.0.1 は、Google Shortcuts Integration Library です。このライブラリには、デベロッパー向けの API は含まれていません。依存関係として追加することで、アシスタントは ShortcutManagerCompat クラスを使用してプッシュする動的ショートカット。

動的ショートカットをプッシュする

アシスタントに表示できる動的ショートカットをプッシュするには、最初に、ShortcutInfoCompat.Builder() クラスを使用してショートカットを作成する必要があります。

次に、ShortcutManagerCompat.pushDynamicShortcut() メソッドを使用してショートカットをプッシュします。ユーザーがアプリで該当のアクションを完了するたびに、ショートカットがプッシュされます。次のサンプルコードは、ユーザーがメモとリストのアプリでリストを作成するたびに、ショートカットをプッシュします。

ExampleOrderActivity

Kotlin

// Define the dynamic shortcut for an item
var intent = Intent(context, DisplayOrderActivity::class.java)
intent.action = Intent.ACTION_VIEW
var shortcutInfo = ShortcutInfoCompat.Builder(context, id)
    .setShortLabel("Running")
    .setLongLabel("Start running")
    .addCapabilityBinding(
        "actions.intent.CREATE_ITEM_LIST", "itemList.name", Arrays.asList("My First List")
    )
    .setIntent(intent) // Push the shortcut
    .build()

// Push the shortcut
ShortcutManagerCompat.pushDynamicShortcut(context, shortcutInfo)

Java

// Define the dynamic shortcut for an item
Intent intent = new Intent(context, DisplayOrderActivity.class);
intent.setAction(Intent.ACTION_VIEW);

ShortcutInfoCompat.Builder shortcutInfo = new ShortcutInfoCompat.Builder(context, id)
    .setShortLabel("Running")
    .setLongLabel("Start running")
    .addCapabilityBinding(
      "actions.intent.CREATE_ITEM_LIST", "itemList.name", Arrays.asList("My First List"))
    .setIntent(intent)
    .build();

// Push the shortcut
ShortcutManagerCompat.pushDynamicShortcut(context, shortcutInfo);

上記のサンプルコードの ShortcutInfoCompat.Builder メソッドで参照される id は、生成されるショートカット オブジェクトの shortcutId を定義しています。この id は、一意の文字列リテラルを指す必要があります。詳しくは、Android ショートカットのドキュメントをご覧ください。

上記の例では、addCapabilityBinding メソッドにより、shortcuts.xml で定義された同じ android:namecapability に動的ショートカットがバインドされています。この方法では、ショートカットを セマンティック 組み込みインテント(BII)パラメータ。

動的ショートカットは、特定の BII パラメータへの関連付けなしでプッシュされることがあります。ユーザーがアシスタントを呼び出すと、アシスタントはショートカットに定義された intent をトリガーして、アクションを遂行します。次の例は、パラメータへの関連付けのない動的ショートカットを示しています。

Kotlin

var intent: Intent = Intent(context, DisplayOrderActivity::class.java)
intent.setPackage(this, "com.sample.app")
intent.setAction(Intent.ACTION_VIEW)

var shortcutInfo: ShortcutInfoCompat = ShortcutInfoCompat.Builder(context, id)
    .setShortLabel("Create a list")
    .setLongLabel("Create a list")
    .addCapabilityBinding("actions.intent.CREATE_ITEM_LIST")
    .setIntent(intent)
    .build()

ShortcutManagerCompat.pushDynamicShortcut(context, shortcutInfo);

Java

Intent intent = new Intent(context, DisplayOrderActivity.class);
intent.setPackage(this, "com.sample.app");
intent.setAction(Intent.ACTION_VIEW);

ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(context, id)
  .setShortLabel("Create a list")
  .setLongLabel("Create a list")
  .addCapabilityBinding("actions.intent.CREATE_ITEM_LIST")
  .setIntent(intent)
  .build();

ShortcutManagerCompat.pushDynamicShortcut(context, shortcutInfo);

アシスタントで動的ショートカットをテストする

Google アシスタントがアプリから動的ショートカットを正常に取り込むと、そのショートカットは、アシスタント Android アプリに音声ショートカット候補として表示できるようになります。アシスタント アプリは、アプリによってプッシュされた最新のショートカットを提示します。

アシスタントで動的ショートカットをテストする手順は次のとおりです。

  1. App Actions のプレビューを作成し、テストデバイスを準備するか、 エミュレータを使用して、同じ手順に沿ってアクションをテストします。 Google アシスタント プラグインと同じセットアップ要件があります。
  2. アプリを開き、プッシュする動的ショートカットを定義します。その後、アクションを完了します。たとえば、メモ作成アプリでメモを作成するたびにショートカットをプッシュする場合は、新しいメモを作成します。
  3. デバイスのアシスタント設定アプリで、ショートカットを開きます。お客様の アプリのリストに動的ショートカットが表示されます。