如何从Flutter应用启动Google Wallet及打开指定凭证?
Flutter 启动 Google Wallet 应用(含特定凭证跳转)的官方实现方法
依赖准备
首先在pubspec.yaml中添加url_launcher包,用于处理URL Scheme和Intent跳转:
dependencies: flutter: sdk: flutter url_launcher: ^6.2.5 # 可替换为最新稳定版本
Android 平台实现
直接打开 Google Wallet
通过应用包名构建Intent启动,Google Wallet的官方包名为com.google.android.apps.walletnfcrel:
import 'package:url_launcher/url_launcher.dart'; Future<void> openGoogleWalletAndroid() async { const intentUrl = 'intent://#Intent;package=com.google.android.apps.walletnfcrel;action=android.intent.action.MAIN;end;'; final uri = Uri.parse(intentUrl); if (await canLaunchUrl(uri)) { await launchUrl(uri, mode: LaunchMode.externalApplication); } else { // 应用未安装时跳转到Google Play const playStoreUrl = 'https://play.google.com/store/apps/details?id=com.google.android.apps.walletnfcrel'; await launchUrl(Uri.parse(playStoreUrl), mode: LaunchMode.externalApplication); } }
注意:Android 11及以上版本,需在
AndroidManifest.xml中添加包名查询权限:<queries> <package android:name="com.google.android.apps.walletnfcrel" /> </queries>
打开特定凭证(会员卡/票据等)
官方推荐使用JWT Deeplink跳转,需先在Google Wallet开发者控制台创建凭证类型并生成合法JWT:
Future<void> openGoogleWalletPassAndroid(String jwt) async { final passUrl = Uri.parse('https://pay.google.com/gp/v/save/$jwt'); if (await canLaunchUrl(passUrl)) { await launchUrl(passUrl, mode: LaunchMode.externalApplication); } else { // 处理跳转失败逻辑,如提示安装Google Wallet } }
该方式会自动处理凭证的添加/已存在判断,无需额外逻辑。
iOS 平台实现
直接打开 Google Wallet
使用Google Wallet的官方URL Scheme googlewallet://:
import 'package:url_launcher/url_launcher.dart'; Future<void> openGoogleWalletIOS() async { const schemeUrl = 'googlewallet://'; final uri = Uri.parse(schemeUrl); if (await canLaunchUrl(uri)) { await launchUrl(uri, mode: LaunchMode.externalApplication); } else { // 应用未安装时跳转到App Store const appStoreUrl = 'https://apps.apple.com/us/app/google-wallet/id1131157706'; await launchUrl(Uri.parse(appStoreUrl), mode: LaunchMode.externalApplication); } }
需在
Info.plist中添加Scheme查询权限:<key>LSApplicationQueriesSchemes</key> <array> <string>googlewallet</string> </array>
打开特定凭证
与Android逻辑一致,使用JWT Deeplink即可:
Future<void> openGoogleWalletPassIOS(String jwt) async { final passUrl = Uri.parse('https://pay.google.com/gp/v/save/$jwt'); if (await canLaunchUrl(passUrl)) { await launchUrl(passUrl, mode: LaunchMode.externalApplication); } else { // 处理跳转失败逻辑 } }
跨平台统一调用
通过平台判断封装统一方法,方便业务调用:
import 'dart:io'; import 'package:url_launcher/url_launcher.dart'; Future<void> openGoogleWallet([String? jwt]) async { if (Platform.isAndroid) { jwt != null ? await openGoogleWalletPassAndroid(jwt) : await openGoogleWalletAndroid(); } else if (Platform.isIOS) { jwt != null ? await openGoogleWalletPassIOS(jwt) : await openGoogleWalletIOS(); } }
关键注意事项
- JWT需通过Google Wallet开发者控制台生成,确保签名合法、凭证类型配置正确
- 测试时需确保设备已安装Google Wallet应用,否则会触发应用商店跳转
- 跳转模式使用
LaunchMode.externalApplication确保完全跳转到Google Wallet应用
内容的提问来源于stack exchange,提问作者Sudeep Kumar
相关产品推荐
相关产品推荐

