こんにちは、KID.Aです。
今回はAndroid 6.0から導入されたApp Linksについて紹介したいと思います。
通常、ウェブサイトのURIが選択された場合、ユーザに対してChooserによる選択肢が提供されますが、関係の無いアプリまで候補に出てきて何を選択して良いのか分かりにくい場合が多いです。
App Linksはそんなことにならないように、ユーザに親和性が高いアプリをリーチさせることできる機能です。
以下はAndroid DeveloperサイトのApp linksの扱いについて記載されています
http://developer.android.com/intl/ja/training/app-links/index.html
こちらの動画で実装方法の説明しています。
https://www.youtube.com/watch?v=LQoohRwojmw&feature=youtu.be
実際にApp Linksを設定してみます。
マニフェストに以下のような設定を追加します。
<activityandroid:name=".SecondActivity" android:label="@string/title_activity_second"> <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="http" android:host="www.targethost.com" /> </intent-filter> </activity>
この設定でアプリをインストールすると設定には以下のようにでます。この「対象リンクを開く」を変更すると優先順位がきまります。
設定した項目は以下のコマンドで確認できます。
(今回のアプリのパッケージ名はjp.co.applinkstestにしています)
> adb shell dumpsys package domain-preferred-apps ・結果 App linkages for user 0: Package: jp.co.applinkstest Domains: www.targethost.com Status: always : 20000000c
設定画面で設定したアプリがApp linkages for userに表示されます。statsuは、設定で「このアプリで開く」をするとalways、「その都度確認」はask、「このアプリで開かない」はneverになります。
これらの設定をした時の挙動は、下記のintentを送った場合でわかります。
adb shell am start -a android.intent.action.VIEW \ -c android.intent.category.BROWSABLE \ -d "http://www.targethost.com/"
通常ならば以下のように図1のように表示されます。しかし「このアプリで開く」を設定する図2のように直接アプリが起動します。
図1
secondActivityはintent-filterにwww.targethost.comを設定したjp.co.applinkstestパッケージのアプリです。
▪️自動認証
自動認証を行う場合は、マニフェストに以下のような設定を行います。(android:autoVerify=”true”を追加する)
<activityandroid:name=".SecondActivity" android:label="@string/title_activity_second"> <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:scheme="http" android:host="www.targethost.com" /> <data android:scheme="https" android:host="www.targethost.com" /> </intent-filter> </activity>
次にサーバーに以下のjsonファイルを置きます。
[{ "relation": ["delegate_permission/common.handle_all_urls"], "target": { "namespace": "android_app", "package_name": "jp.co.applinkstest", "sha256_cert_fingerprints": ["14:6D:E9:83:C5:73:06:50:D8:EE:B9:95:2F:34:FC:64:16:A0:83:42:E6:1D:BE:A8:8A:04:96:B2:3F:CF:44:E5"] } }]
サーバは以下の場所に配置します。
https://www.targethost.com/.well-known/assetlinks.json
SHA-256はストアキーから下記のコマンドで確認を行います。
$ keytool -list -v -keystore my-release-key.keystore
正しく置けたかは、
https://digitalassetlinks.googleapis.com/v1/statements:list? source.web.site=http://www.targethost.com& relation=delegate_permission/common.handle_all_urls
で確認できます。source.web.siteに設定する値はマニフェストに設定したホストに変更してください。
確認すると以下のように返答がきます。
https://digitalassetlinks.googleapis.com/v1/statements:list? source.web.site=http://www.targethost.com& relation=delegate_permission/common.handle_all_urls { "statements": [ { "source": { "web": { "site": "http://www.targethost.com." } }, "relation": "delegate_permission/common.handle_all_urls", "target": { "androidApp": { "packageName": "jp.co.applinkstest", "certificate": { "sha256Fingerprint": "56:E5:74:35:D2:A6:3C:4A:ED:B1:15:F2:45:F6:B0:0B:FC:4C:A7:E1:2C:F9:A0:C9:CE:D0:84:CF:C7:E9:5F:41" } } } } ], "maxAge": "59.821095595s", "debugString": "********************* ERRORS *********************\nNone!\n********************* INFO MESSAGES *********************\n* Info: The following statements were considered when processing the request:\n\n---\nSource: Web asset with site http://www.targethost.com. (which is equivalent to 'http://www.targethost.com')\nRelation: delegate_permission/common.handle_all_urls\nTarget: Android app asset with package name jp.co.applinkstest and certificate fingerprint 56:E5:74:35:D2:A6:3C:4A:ED:B1:15:F2:45:F6:B0:0B:FC:4C:A7:E1:2C:F9:A0:C9:CE:D0:84:CF:C7:E9:5F:41\nWhere this statement came from:\n Origin of the statement: Web asset with site http://www.targethost.com. (which is equivalent to 'http://www.targethost.com')\n Include directives followed (in order):\n \u003cNone\u003e\nMatches source query: Yes\nMatches relation query: Yes\nMatches target query: Yes\n\n--- End of statement list. ---\n\n\n" }
これで準備は完了です。
挙動はアプリ側にautoVerifyが付いているとアプリインストール時にサーバ問い合わせをしてくれます。パッケージマネージャーがサーバに問い合わせてくれるようです。
インストールしてsysdumpのコマンドで確認できます。
> adb shell dumpsys package domain-preferred-apps ・結果 App verification status: Package: jp.co.test.applinkstest Domains: www.targethost.com Status: undefined
App verification statusでアプリの認証状態を確認できます。
インストール時にStatusが変わるはずですが、失敗しているとundefinedになります。
実際に何回か試しましたが、認証が成功しなかったのでこれは調査中です。
※認証の確認の仕方は「App Linksの認証の成否の確認」の記事に記載しました
本来ならインストール時にサーバとの認証が成功するとstausがalwaysに変わるようです。
そのため、下記のようなintentを発行した場合に
adb shell am start -a android.intent.action.VIEW \ -c android.intent.category.BROWSABLE \ -d "http://www.targethost.com/"
指定Domainsがwww.targethost.comで認証が成功しているアプリが起動します。(図2のような結果になります)
App linksでユーザーがわかりやすくアプリを使えるため、導入を検討するといいかもしれないです。
special thanks:@miysobun