繁体   English   中英

Flutter - 应用程序链接在 Android 上不起作用

[英]Flutter - App links does not work on Android

我试图让 App Links 在 Android 上工作一整天,使用uni_links package,但没有成功。

当我打开链接时,浏览器打开它而不是启动应用程序。

你可以看到我完整的 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>

我用我的 appID 替换了com.example.app ,用我的主机名替换了example.com 我还在后台添加了assetlinks.json ,在https://developers.google.com/digital-asset-links/tools/generator上测试通过。

我创建了一个简单的 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);
    }
  }

然后我运行模拟器,从 VSCode 启动应用程序。 在这里我可以看到应用程序调用/.well-known/assetlinks.json并成功返回文件的内容。

最后我用 CLI 测试:

adb shell am start -a android.intent.action.VIEW -c android.intent.category.BROWSABLE -d "https://example.com/ping"

链接是直接用浏览器打开的,而不是应用程序。

我错过了什么或做错了什么?

谢谢您的帮助!

我终于让它工作了。 我必须在assetlinks.json文件中添加未签名应用程序的指纹(我认为)。 必须记住在部署后更改/删除它。 在我的例子中,我在 2 个不同的主机名上使用 2 个不同的资产链接,所以这不会成为问题。

使用以下命令检查应用程序链接时,我得到了指纹:

adb shell pm get-app-links com.example.app

如果我犯了错误或者是否有其他方法可以做到这一点,请告诉我!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM