dream_monet_flutter_sdk 0.0.11-alpha
dream_monet_flutter_sdk: ^0.0.11-alpha copied to clipboard
Dream monent sdk for Flutter
Dream Monet Flutter SDK #
1. Install #
Trong pubspec.yaml:
dependencies:
dream_monet_flutter_sdk: ^0.0.9-alpha
2. Setup Application ID KEY #
Android #
- Đảm bảo
minSdkVersion >= 21,compileSdkVersionmới nhất vàMainActivityđã khai báoandroid:exported. - Thêm App ID của Google Mobile Ads vào
android/app/src/main/AndroidManifest.xml:
<application>
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713" />
<!-- MainActivity -->
</application>
iOS (Chưa test) #
- Thêm
GADApplicationIdentifiervàoios/Runner/Info.plist. - Bổ sung
SKAdNetworkItemstheo hướng dẫn của Google nếu cần attribution.
3. Khởi tạo SDK & Consent #
In your splash screen
final DreamMonetSDK sdk = DreamMonetSDK();
Future<void> bootstrap() async {
WidgetsFlutterBinding.ensureInitialized();
await MobileAds.instance.initialize();
final config = SDKConfig(
appId: SampleAdUnits.admobAppId,
isDebugMode: true,
testMode: true
);
await sdk.initialize(config);
final result = await sdk.consentManager.gatherConsent(
testDeviceHashedId: SampleAdUnits.testDeviceId,
debugGeography: DebugGeography.debugGeographyEea,
);
if (!result.isSuccess) throw result.message ?? 'Consent error';
if (!await sdk.consentManager.canRequestAds()) {
throw 'Consent chưa sẵn sàng để request ads';
}
}
4. Tích hợp #
Interstitial #
late final CommonInterstitialAd _interstitialAd = CommonInterstitialAd(
key: 'demo-interstitial-key',
defaultUnitId: SampleAdUnits.interstitialAdUnit,
);
Future<void> _loadInterstitial() async {
final result = await _interstitialAd.load(context);
}
void _showInterstitial() {
_interstitialAd.show(
context,
_InterstitialCallback(
onShowed: () { /* do something */ },
onFailed: (error) { }
),
timeoutMs: 5000,
);
}
CommonInterstitialAd.loadtrả vềDreamLoadResultđể bạn quyết định flow.
Rewarded #
late final CommonRewardedAd _rewardedAd = CommonRewardedAd(
key: 'demo-rewarded-key',
defaultUnitId: SampleAdUnits.rewardedAdUnit,
);
//optional for reward ad
_rewardedAd.setServerSideVerificationOptions(ServerSideVerificationOptions(userId: "hello my feng"));
//load
Future<void> _loadRewarded() async {
final result = await _rewardedAd.load(context);
}
//show
void _showRewarded() {
_rewardedAd.show(
context,
_RewardedCallback(
onShowed: () { /* do something */ },
onFailed: (error) { },
onRewarded: (amount, type) => _log('Rewarded user (+$amount $type)'),
),
timeoutMs: 5000,
);
}
- In App Purchase
- Cấu hình trong
SDKConfigkhi khởi tạo SDK:
final config = SDKConfig(
appId: 'your_app_id',
purchaseConfig: DreamPurchaseConfig(
android: PlatformPurchaseConfig(
consumableIds: ['coin_pack_1', 'coin_pack_2'],
oneTimeProductIds: ['remove_ads'],
subscriptionIds: ['premium_monthly'],
removeAdIds: ['remove_ads'],
),
ios: PlatformPurchaseConfig(
consumableIds: ['coin_pack_1'],
oneTimeProductIds: ['remove_ads'],
subscriptionIds: ['premium_monthly'],
removeAdIds: ['remove_ads'],
),
),
);
final purchase = DreamMonetSDK().purchase;
final price = await purchase.getPrice('product_id');
final currency = await purchase.getCurrency('product_id');
//buy
final result = await purchase.buy('product_id');
if (result == null) {
} else if (result.status == DreamPurchaseStatus.error) {
print('Lỗi: ${result.error?.message}');
} else if (result.isPurchased()){
print('Mua hàng thành công!');
}
// check if remove ad by purchase:
purchase.isRemoveAd // a bool value
purchase.isRemoveAdStream.listen((isRemoveAd) => _log("removed ad")); //stream
6. Ad Revenue Listener #
To listen for ad revenue events (e.g., to send data to MMPs like AppsFlyer or Adjust), implement the AdRevenueListener interface and register it with the SDK before initialization.
1. Create a Listener Class #
import 'package:dream_monet_flutter_sdk/dream_monet_flutter_sdk.dart';
class MyAppRevenueListener extends AdRevenueListener {
@override
void onAdRevenue(
String adId,
int valueMicros,
String currencyCode,
String precision,
) {
print('Ad Revenue: $valueMicros $currencyCode (Precision: $precision) for Ad Unit: $adId');
// Example: Log to AppsFlyer
// final revenue = valueMicros / 1000000.0;
// Map<String, dynamic> additionalParams = {
// 'adUnitId': adId,
// 'precision': precision,
// };
// appsFlyerSdk.logAdRevenue(revenue, currencyCode, additionalParams);
}
}
2. Register the Listener #
Call setAdRevenueListener before initializing the SDK.
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Register revenue listener
DreamMonetSDK().setAdRevenueListener(MyAppRevenueListener());
// Initialize SDK
final config = SDKConfig(
// ... your config
);
await DreamMonetSDK().initialize(config);
// ... rest of your app initialization
}