簡體   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