我一直在尝试在我的 Android Flutter 应用程序中实现 AppLink,该应用程序目前处于开发/调试模式,但 AppLink 不起作用。当我通过在调试/开发模式下运行 Flutter 应用程序的手机中的浏览器点击 URL 时,该应用程序无法打开。我认为 Flutter 应用程序实现内部存在一些问题。
创建了
assetlinks.json
文件并添加了以下详细信息:
[
{
"relation": [
"delegate_permission/common.handle_all_urls"
],
"target": {
"namespace": "android_app",
"package_name": "com.foxjek.fox_jek_user",
"sha256_cert_fingerprints": [
"73:EA:74:0C:B4:6B:99:37:C0:CF:B9:BD:54:EB:2C:51:04:5A:3A:6A:0E:64:0B:27:B8:73:B2:51:4B:B3:CE:F9"
]
}
}
]
并上传到服务器上
root
文件夹下的.well-known
目录下,并通过Google Digital Asset Links进行测试,测试成功。
现在我已经按照此处定义的步骤安装了用于AppLinking实现的uni_link 0.5.1
库之后,我按照以下步骤设置一切:
这是我的
AndroidManifest.xml
目录下的android/app/src/main
文件。
在 <activity>
标签(通常名为 MainActivity
)内,我添加了 <intent-filter>
元素:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.foxjek.fox_jek_user">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-feature android:name="android.hardware.location.gps" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<!-- Provide required visibility configuration for API level 30 and above -->
<queries>
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="sms" />
</intent>
<!-- If your app makes calls -->
<intent>
<action android:name="android.intent.action.DIAL" />
<data android:scheme="tel" />
</intent>
<!-- If your app emails -->
<intent>
<action android:name="android.intent.action.SEND" />
<data android:mimeType="*/*" />
</intent>
<!-- If your sends SMS messages -->
<intent>
<action android:name="android.intent.action.SENDTO" />
<data android:scheme="smsto" />
</intent>
<intent>
<action android:name="android.media.action.IMAGE_CAPTURE" />
</intent>
<package android:name="com.facebook.katana" />
</queries>
<application
android:name="androidx.multidex.MultiDexApplication"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:usesCleartextTraffic="true">
<receiver
android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver
android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver"
android:exported="false" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="high_importance_channel" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_notification" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorPrimary" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_api_key" />
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id" />
<meta-data
android:name="com.facebook.sdk.ClientToken"
android:value="@string/facebook_client_token" />
<activity
android:name="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:exported="false"
android:label="@string/app_name" />
<activity
android:name="com.facebook.CustomTabActivity"
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="@string/fb_login_protocol_scheme" />
</intent-filter>
</activity>
<!--<provider android:authorities="com.facebook.app.FacebookContentProvider2226807840719365"
android:name="com.facebook.FacebookContentProvider"
android:exported="true" />-->
<activity
android:name="com.yalantis.ucrop.UCropActivity"
android:screenOrientation="portrait"
android:theme="@style/Theme.AppCompat.Light.NoActionBar" />
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:exported="true"
android:hardwareAccelerated="true"
android:launchMode="singleTop"
android:showWhenLocked="true"
android:theme="@style/LaunchTheme"
android:turnScreenOn="true"
android:windowSoftInputMode="adjustResize">
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme" />
<intent-filter>
<action android:name="FLUTTER_NOTIFICATION_CLICK" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data
android:name="io.flutter.embedding.android.SplashScreenDrawable"
android:resource="@drawable/launch_background" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- App 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" />
<!-- Accepts URIs that begin with https://YOUR_HOST -->
<data
android:scheme="https"
android:host="user-api.slim-surface-app.com" />
</intent-filter>
</activity>
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>
</manifest>
Dart 代码更新为:
void main() {
runApp(const MyApp());
initUniLinks();
}
void initUniLinks() async {
// Initialize the UniLinks plugin
print("Initialize the UniLinks plugin");
try {
await getInitialLink();
} on PlatformException {
// Handle exception/error as needed
print("Handle exception/error as needed");
}
// Listen for incoming deep links
// Attach a listener to the stream
print("Listen for incoming deep links");
linkStream.listen((String? uri) {
// Handle the deep link URI
print("Handle the deep link URI");
print(uri);
} , onError: (err) {
// Handle error, if any
print("Handle error, if any");
});
}
在设备上重建并重新安装 Flutter 应用程序以使更新生效。
但是,现在当我在设备浏览器中访问域 your-domain.com 时(在调试模式下运行 Flutter 应用程序的同一设备),设备无法识别 DeepLink/AppLink 来打开应用程序。
您可以通过运行此命令来测试应用程序 https://docs.flutter.dev/cookbook/navigation/set-up-app-links
adb shell 'am start -a android.intent.action.VIEW \
-c android.intent.category.BROWSABLE \
-d "http://<web-domain>/details"' \
<package name>