Flutter延迟深度链接问题:安装后获取推荐码与事件ID
Android: Fixing Install Referrer Retrieval
If android_play_install_referrer didn’t work, verify your implementation with these steps:
Add the package to
pubspec.yaml:dependencies: android_play_install_referrer: ^latest_versionInitialize and listen for the referrer in your splash screen or initial screen:
import 'package:android_play_install_referrer/android_play_install_referrer.dart'; @override void initState() { super.initState(); _fetchInstallReferrer(); } Future<void> _fetchInstallReferrer() async { try { final referrerDetails = await AndroidPlayInstallReferrer.installReferrer; if (referrerDetails != null) { // Parse the referrer string to extract your parameters final params = Uri.splitQueryString(referrerDetails.installReferrer); final referrerCode = params['REFERRERCODE']; final eventId = params['eventId']; // Use these values to continue the referral flow if (referrerCode != null && eventId != null) { // Save or process the referral data } } } catch (e) { // Handle errors (e.g., API unavailable on older Android versions) } }Critical note: The referrer is only retrieved once on the first app launch after installation. Test this by uninstalling the app, clicking your Play Store link, then reinstalling and launching.
iOS: Handling Referrals via Firebase Dynamic Links
iOS doesn’t support passing referrer parameters directly through App Store links. Use Firebase Dynamic Links for a reliable cross-platform solution that works for both Android and iOS:
Step 1: Set Up Firebase Dynamic Links
- Enable Dynamic Links in your Firebase project and configure your app’s bundle ID.
- Add the package to
pubspec.yaml:dependencies: firebase_dynamic_links: ^latest_version
Step 2: Generate a Dynamic Link
Create a link that includes your referral parameters. You can do this via the Firebase Console or programmatically:
final dynamicLinkParams = DynamicLinkParameters( uriPrefix: 'https://yourapp.page.link', // Replace with your Firebase prefix link: Uri.parse('https://yourapp.com/refer?referrerCode=$REFERRERCODE&eventId=$EVENTID'), androidParameters: AndroidParameters( packageName: 'com.yourapp.android', minimumVersion: 1, ), iosParameters: IosParameters( bundleId: 'com.yourapp.ios', minimumVersion: '1.0', ), ); final shortLink = await dynamicLinkParams.buildShortLink(); final referralUrl = shortLink.shortUrl.toString();
Step 3: Retrieve Parameters in the App
Listen for dynamic links when the app launches:
@override void initState() { super.initState(); _initDynamicLinks(); } Future<void> _initDynamicLinks() async { // Check for initial link (app opened from link while closed) final initialLinkData = await FirebaseDynamicLinks.instance.getInitialLink(); final deepLink = initialLinkData?.link; if (deepLink != null) { final referrerCode = deepLink.queryParameters['referrerCode']; final eventId = deepLink.queryParameters['eventId']; // Process referral data } // Listen for links while the app is open FirebaseDynamicLinks.instance.onLink.listen((linkData) { final deepLink = linkData.link; if (deepLink != null) { // Handle ongoing referral links } }).onError((error) { // Handle link errors }); }
Alternative: Custom Web Redirect (No Firebase)
If you prefer not to use Firebase, build a simple web endpoint that:
- Stores referral parameters (along with a device identifier like advertising ID or IP) when the user clicks the link.
- Redirects the user to the appropriate app store.
- When the app first launches, sends a request to your endpoint to retrieve the stored parameters using the same device identifier.
This requires backend development but gives you full control over the referral flow.
内容的提问来源于stack exchange,提问作者Siddhesh Kumbhar

