[英]Flutter - App links does not work on Android
I am trying to make App Links work on Android the whole day, using uni_links package, unsuccessfully.我试图让 App Links 在 Android 上工作一整天,使用uni_links package,但没有成功。
When I open a link, the browser open it instead of launching the app.当我打开链接时,浏览器打开它而不是启动应用程序。
You can see my full AndroidManifest:你可以看到我完整的 AndroidManifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app">
<!-- Permissions options for the `storage` group -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!-- Permissions options for the `camera` group -->
<uses-permission android:name="android.permission.CAMERA"/>
<!-- Permissions options for the `access notification policy` group -->
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY"/>
<application
android:label="example"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<!-- Specifies an Android theme to apply to this Activity as soon as
the Android process has started. This theme is visible to the user
while the Flutter UI initializes. After that, this theme continues
to determine the Window background behind the Flutter UI. -->
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme"
/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<!-- Deep links -->
<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="https" android:host="example.com" />
</intent-filter>
</activity>
<!-- Apple Sign In: Set up the Sign in with Apple activity, such that it's callable from the browser-redirect -->
<activity
android:name="com.aboutyou.dart_packages.sign_in_with_apple.SignInWithAppleCallback"
android:exported="true"
>
<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="signinwithapple" />
<data android:path="callback" />
</intent-filter>
</activity>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>
</manifest>
I replaced com.example.app
by my appID, and example.com
by my hostname.我用我的 appID 替换了
com.example.app
,用我的主机名替换了example.com
。 I also added assetlinks.json
to my backend, and the test on https://developers.google.com/digital-asset-links/tools/generator passed.我还在后台添加了
assetlinks.json
,在https://developers.google.com/digital-asset-links/tools/generator上测试通过。
I created a simple function to check in main.dart
:我创建了一个简单的 function 来检查
main.dart
:
Future<void> initUniLinks() async {
// Platform messages may fail, so we use a try/catch PlatformException.
try {
final initialLink = await getInitialLink();
print('initialLink');
print(initialLink);
// Parse the link and warn the user, if it is not correct,
// but keep in mind it could be `null`.
// Attach a listener to the stream
_sub = linkStream.listen((String? link) {
// Parse the link and warn the user, if it is not correct
print(link);
}, onError: (err) {
// Handle exception by warning the user their action did not succeed
print('error');
print(err);
});
} on PlatformException catch (err) {
// return?
// Handle exception by warning the user their action did not succeed
print('error');
print(err);
}
}
Then I run the emulator, starts the app from VSCode.然后我运行模拟器,从 VSCode 启动应用程序。 Here I can see the app calls
/.well-known/assetlinks.json
and it returns the content of the file successfully.在这里我可以看到应用程序调用
/.well-known/assetlinks.json
并成功返回文件的内容。
Finally I test with the CLI:最后我用 CLI 测试:
adb shell am start -a android.intent.action.VIEW -c android.intent.category.BROWSABLE -d "https://example.com/ping"
The link is directly opened with the browser instead of the app.链接是直接用浏览器打开的,而不是应用程序。
What am I missing or doing wrong?我错过了什么或做错了什么?
Thank you for your help!谢谢您的帮助!
I finally got it to work.我终于让它工作了。 I had to add the fingerprint of the non-signed app (I think), in the assetlinks.json file.
我必须在assetlinks.json文件中添加未签名应用程序的指纹(我认为)。 Will have to remember to change/remove it once deployed.
必须记住在部署后更改/删除它。 In my case I use 2 different assetlinks, on 2 different hostnames so it won't be a problem.
在我的例子中,我在 2 个不同的主机名上使用 2 个不同的资产链接,所以这不会成为问题。
I got the fingerprint when checking app links with the command:使用以下命令检查应用程序链接时,我得到了指纹:
adb shell pm get-app-links com.example.app
Let me know if I ever make a mistake or if there is another way to do this!如果我犯了错误或者是否有其他方法可以做到这一点,请告诉我!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.