我正在尝试在我的 Flutter 应用程序中实现深度链接,而不使用 Firebase 动态链接。我已按照必要的步骤使用 Android 应用程序链接启用应用程序链接。但是,当我尝试在某些 Android 设备(尤其是三星设备)上打开深层链接时,链接会在浏览器 (Chrome) 中打开,而不是启动应用程序。
AndroidManifest.xml
<meta-data android:name="flutter_deeplinking_enabled" android:value="true" />
<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="apartment.sakany.app"
/>
</intent-filter>
我正在使用 app_links Flutter 包来处理深层链接:
DeepLinkService 类 我正在使用 app_links Flutter 包来处理深层链接:
class DeepLinkService {
static const String domainUrl = 'https://apartment.sakany.app';
static Future generateDynamicLink({
required String urlParams,
bool withShare = true,
}) async {
try {
final shortUrl = Uri.parse("$domainUrl$urlParams");
if (withShare) {
shareLink(shortUrl);
} else {
await FlutterClipboard.copy(shortUrl.toString());
return shortUrl;
}
} catch (e) {
print(e.toString());
}
}
static shareLink(Uri link) async {
await Share.share(link.toString());
}
}
AppLinks服务类 我使用以下代码处理深层链接导航:
class AppLinksService {
static String _code = '';
static String get code => _code;
static bool get hasCode => _code.isNotEmpty;
static void resetCode() => _code = '';
static final AppLinks _appLinks = AppLinks();
static Uri? _lastProcessedLink;
static bool fromDeepLink = false;
static init() async {
try {
final Uri? initialLink = await _appLinks.getInitialLink();
if (initialLink != null) {
handleDeepLink(initialLink);
}
} catch (e) {
print('Error handling initial app link: $e');
}
_appLinks.uriLinkStream.listen((Uri? link) {
if (link != null) {
handleDeepLink(link);
}
}, onError: (Object err) {
print('Error handling app link: $err');
});
}
static void handleDeepLink(Uri? link) {
if (link != null && link != _lastProcessedLink) {
fromDeepLink = true; // Mark the navigation as triggered by a deep link
_lastProcessedLink = link;
print('Received link: $link');
WidgetsBinding.instance.addPostFrameCallback((_) async {
Map<String, String> params = link.queryParameters;
String receivedId = params['id'] ?? '';
if (receivedId.isNotEmpty) {
print('Received id: $receivedId');
NavigationService.navigateTo(AppRouter.kAppartmentDetailsScreen, extra: receivedId).then((_) {
fromDeepLink = false;
});
} else {
print("No valid ID found in the deep link.");
}
});
}
}
static void clearLastProcessedLink() {
_lastProcessedLink = null;
}
}
1.为什么应用程序链接无法在设备上打开应用程序?
如何确保链接自动打开应用程序而不是所有设备上的浏览器?
如果您的应用程序已发布
(适用于 Android) 确保将
assetlinks.json
文件上传到您的网站。 必须可以通过 https://yourdomain/.well-known/assetlinks.json
访问
(适用于 IOS) 确保上传
apple-app-site-association
它必须可以通过 https://yourDomain.com/.well-known/apple-app-site-association
访问
如果您仍在
development mode
并使用 Android 13 或更高版本的 模拟器:
1- 转到“设置”>“应用程序”>“默认应用程序”>“打开链接”。
2- 选择您的应用程序。
3- 点击“+ 添加链接”。
4- 选择“open.my.app.example.com”。