adstage
Mobile SDKDeep Link

Flutter Deep Link

คู่มือการใช้งาน deep link ด้วย NBase AdStage SDK ในแอปพลิเคชัน Flutter

1. ภาพรวม

  • Deep link คือ URL ที่นำผู้ใช้ไปยังหน้าจอเฉพาะในแอปโดยตรง
  • AdStage SDK มีฟีเจอร์การสร้าง จัดการ และติดตาม deep link

2. เริ่มต้นใช้งาน

2.1 ติดตั้ง SDK

# pubspec.yaml
dependencies:
  nbase_ad_flutter_sdk: ^1.0.1
flutter pub get

2.2 การตั้งค่าพื้นฐาน

การตั้งค่า Android

android/app/src/main/kotlin/.../MainActivity.kt:

import android.content.Intent
import android.os.Bundle
import io.flutter.embedding.android.FlutterActivity
import io.nbase.adapter.adstage.AdStageSDK
 
class MainActivity : FlutterActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
 
        AdStageSDK.initializeAdStage("YOUR_API_KEY")
    }
 
    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        setIntent(intent)
        handleDeepLink(intent)
    }
 
    private fun handleDeepLink(intent: Intent) {
        AdStageSDK.handleDeepLink(intent)
    }
}

android/app/src/main/AndroidManifest.xml:

<activity
    android:name=".MainActivity"
    android:exported="true"
    android:launchMode="singleTop">
 
    <!-- Deep link intent filter -->
    <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="your-app-scheme" />
    </intent-filter>
</activity>

การตั้งค่า iOS

ios/Runner/Info.plist:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLName</key>
        <string>nbase-adstage</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>your-app-scheme</string>
        </array>
    </dict>
    <!-- เพิ่ม scheme อื่นๆ หากต้องการ -->
</array>

ios/Runner/AppDelegate.swift:

import UIKit
import Flutter
 
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
    override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
        GeneratedPluginRegistrant.register(with: self)
 
        AdStageSDK.shared.initializeAdStage(apiKey: "YOUR_API_KEY")
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }
}

3. การใช้งาน

void _setupDeepLinkListener() {
  NbaseAdFlutterSdk.onDeepLinkReceived.listen((event) {
    print('รับ deep link: ${event.path}');
    print('พารามิเตอร์: ${event.parameters}');
 
    // นำทางตาม deep link
    _handleDeepLink(event);
  });
}
 
void _handleDeepLink(DeepLinkEvent event) {
  switch (event.path) {
    case '/product':
      Navigator.pushNamed(context, '/product', arguments: event.parameters);
      break;
    case '/user':
      Navigator.pushNamed(context, '/user', arguments: event.parameters);
      break;
    default:
      Navigator.pushNamedAndRemoveUntil(context, '/', (route) => false);
  }
}

เมื่อแอปถูกเปิดผ่าน deep link ขณะที่แอปถูกปิด ให้ตรวจสอบ pending deep link ตอนเริ่มต้น

Future<void> checkPendingDeepLink() async {
  try {
    final result = await NbaseAdFlutterSdk.handleDeepLinkIntent();
    if (result != null) {
      _handleDeepLink(result);
    }
  } catch (e) {
    print('ไม่สามารถจัดการ pending deep link: $e');
  }
}
 
@override
void initState() {
  super.initState();
  _setupDeepLinkListener();
 
  // ตรวจสอบ pending deep link หลังเริ่มแอป
  WidgetsBinding.instance.addPostFrameCallback((_) {
    checkPendingDeepLink();
  });
}

4. การแก้ไขปัญหา

  • ไม่ได้รับ deep link: ตรวจสอบ URL scheme/domain, API key และการเริ่มต้น SDK
  • ข้อผิดพลาดใน Android build: รัน flutter clean && flutter pub get และตรวจสอบการตั้งค่า Manifest
  • ข้อผิดพลาดใน iOS build: รัน cd ios && pod install และตรวจสอบการตั้งค่า Info.plist

สารบัญ