diff --git a/android/src/main/kotlin/com/shatsy/admobflutter/AdmobBanner.kt b/android/src/main/kotlin/com/shatsy/admobflutter/AdmobBanner.kt index 7319607..0d91914 100644 --- a/android/src/main/kotlin/com/shatsy/admobflutter/AdmobBanner.kt +++ b/android/src/main/kotlin/com/shatsy/admobflutter/AdmobBanner.kt @@ -2,7 +2,6 @@ package com.shatsy.admobflutter import android.content.Context import android.view.View -import com.google.android.gms.ads.AdListener import com.google.android.gms.ads.AdRequest import com.google.android.gms.ads.AdSize import com.google.android.gms.ads.AdView @@ -23,8 +22,31 @@ class AdmobBanner(context: Context, messenger: BinaryMessenger, id: Int, args: H adView.adSize = getSize(args?.get("adSize") as HashMap<*, *>) adView.adUnitId = args?.get("adUnitId") as String? - val adRequest = AdRequest.Builder().build() - adView.loadAd(adRequest) + val adRequest = AdRequest.Builder(); + + val rawTargetingInfo = args?.get("targetingInfo") ?: null; + val targetingInfo = rawTargetingInfo as? HashMap<*, *>; + val testDevices: List = targetingInfo?.get("testDevices") as? List ?: listOf(); + for (testDeviceId in testDevices) { + adRequest.addTestDevice(testDeviceId); + } + + val keywords: List = targetingInfo?.get("keywords") as? List ?: listOf(); + for (keyword in keywords) { + adRequest.addKeyword(keyword); + } + + val childDirected: Boolean = targetingInfo?.get("childDirected") as? Boolean ?: false + if (childDirected) { + adRequest.tagForChildDirectedTreatment(childDirected); + } + + val contentUrl: String? = targetingInfo?.get("contentUrl") as? String + if (!contentUrl.isNullOrEmpty()) { + adRequest.setContentUrl(contentUrl) + } + + adView.loadAd(adRequest.build()) } private fun getSize(size: HashMap<*, *>) : AdSize { @@ -60,4 +82,4 @@ class AdmobBanner(context: Context, messenger: BinaryMessenger, id: Int, args: H adView.destroy() channel.setMethodCallHandler(null) } -} \ No newline at end of file +} diff --git a/android/src/main/kotlin/com/shatsy/admobflutter/AdmobFlutterPlugin.kt b/android/src/main/kotlin/com/shatsy/admobflutter/AdmobFlutterPlugin.kt index 39c5419..24cca78 100644 --- a/android/src/main/kotlin/com/shatsy/admobflutter/AdmobFlutterPlugin.kt +++ b/android/src/main/kotlin/com/shatsy/admobflutter/AdmobFlutterPlugin.kt @@ -43,7 +43,7 @@ class AdmobFlutterPlugin(private val context: Context): MethodCallHandler { override fun onMethodCall(call: MethodCall, result: Result) { when(call.method) { "getPlatformVersion" -> result.success("Android ${android.os.Build.VERSION.RELEASE}") - "initialize" -> MobileAds.initialize(context, call.arguments()) + "initialize" -> MobileAds.initialize(context) else -> result.notImplemented() } } diff --git a/example/pubspec.lock b/example/pubspec.lock index 79fbdd0..a905fc2 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -8,13 +8,27 @@ packages: relative: true source: path version: "0.3.4" + archive: + dependency: transitive + description: + name: archive + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.11" + args: + dependency: transitive + description: + name: args + url: "https://pub.dartlang.org" + source: hosted + version: "1.5.2" async: dependency: transitive description: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.3.0" + version: "2.4.0" boolean_selector: dependency: transitive description: @@ -36,13 +50,27 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.14.11" + convert: + dependency: transitive + description: + name: convert + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.1" + crypto: + dependency: transitive + description: + name: crypto + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.3" cupertino_icons: dependency: "direct main" description: name: cupertino_icons url: "https://pub.dartlang.org" source: hosted - version: "0.1.2" + version: "0.1.3" flutter: dependency: "direct main" description: flutter @@ -53,20 +81,27 @@ packages: description: flutter source: sdk version: "0.0.0" + image: + dependency: transitive + description: + name: image + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.4" matcher: dependency: transitive description: name: matcher url: "https://pub.dartlang.org" source: hosted - version: "0.12.5" + version: "0.12.6" meta: dependency: transitive description: name: meta url: "https://pub.dartlang.org" source: hosted - version: "1.1.7" + version: "1.1.8" path: dependency: transitive description: @@ -81,6 +116,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.8.0+1" + petitparser: + dependency: transitive + description: + name: petitparser + url: "https://pub.dartlang.org" + source: hosted + version: "2.4.0" quiver: dependency: transitive description: @@ -134,7 +176,7 @@ packages: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.2.5" + version: "0.2.11" typed_data: dependency: transitive description: @@ -149,6 +191,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.0.8" + xml: + dependency: transitive + description: + name: xml + url: "https://pub.dartlang.org" + source: hosted + version: "3.5.0" sdks: - dart: ">=2.2.2 <3.0.0" + dart: ">=2.4.0 <3.0.0" flutter: ">=1.0.0 <2.0.0" diff --git a/ios/admob_flutter.podspec b/ios/admob_flutter.podspec index cdff68f..f61e2ec 100644 --- a/ios/admob_flutter.podspec +++ b/ios/admob_flutter.podspec @@ -20,7 +20,7 @@ Admob plugin that shows banner ads using native platform views. # https://firebase.google.com/docs/ios/setup # https://github.com/CocoaPods/Specs/blob/master/Specs/0/3/5/Firebase/6.10.0/Firebase.podspec.json s.dependency 'Firebase/Analytics' - s.dependency 'FirebaseAnalytics', '~> 6.1.3' + s.dependency 'FirebaseAnalytics' s.dependency 'Firebase/AdMob' s.dependency 'Google-Mobile-Ads-SDK', '~> 7.50' diff --git a/lib/admob_flutter.dart b/lib/admob_flutter.dart index 785cd1f..910e71b 100644 --- a/lib/admob_flutter.dart +++ b/lib/admob_flutter.dart @@ -5,3 +5,4 @@ export 'src/admob_banner_controller.dart'; export 'src/admob_interstitial.dart'; export 'src/admob_reward.dart'; export 'src/admob_events.dart'; +export 'src/admob_targeting_info.dart'; diff --git a/lib/src/admob_banner.dart b/lib/src/admob_banner.dart index 26ed4c3..a86fbe3 100644 --- a/lib/src/admob_banner.dart +++ b/lib/src/admob_banner.dart @@ -1,6 +1,7 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; +import 'admob_targeting_info.dart'; import 'admob_banner_controller.dart'; import 'admob_banner_size.dart'; import 'admob_events.dart'; @@ -8,6 +9,7 @@ import 'admob_events.dart'; class AdmobBanner extends StatefulWidget { final String adUnitId; final AdmobBannerSize adSize; + final MobileAdTargetingInfo targetingInfo; final void Function(AdmobAdEvent, Map) listener; final void Function(AdmobBannerController) onBannerCreated; @@ -15,6 +17,7 @@ class AdmobBanner extends StatefulWidget { Key key, @required this.adUnitId, @required this.adSize, + this.targetingInfo, this.listener, this.onBannerCreated, }) : super(key: key); @@ -38,6 +41,7 @@ class _AdmobBannerState extends State { creationParams: { "adUnitId": widget.adUnitId, "adSize": widget.adSize.toMap, + "targetingInfo": widget.targetingInfo?.toJson(), }, creationParamsCodec: StandardMessageCodec(), onPlatformViewCreated: _onPlatformViewCreated, @@ -53,6 +57,7 @@ class _AdmobBannerState extends State { creationParams: { "adUnitId": widget.adUnitId, "adSize": widget.adSize.toMap, + "targetingInfo": widget.targetingInfo.toJson(), }, creationParamsCodec: StandardMessageCodec(), onPlatformViewCreated: _onPlatformViewCreated, diff --git a/lib/src/admob_targeting_info.dart b/lib/src/admob_targeting_info.dart new file mode 100644 index 0000000..1ca1792 --- /dev/null +++ b/lib/src/admob_targeting_info.dart @@ -0,0 +1,46 @@ +/// Targeting info per the native AdMob API. +/// +/// This class's properties mirror the native AdRequest API. See for example: +/// [AdRequest.Builder for Android](https://firebase.google.com/docs/reference/android/com/google/android/gms/ads/AdRequest.Builder). +class MobileAdTargetingInfo { + const MobileAdTargetingInfo( + {this.keywords, + this.contentUrl, + @Deprecated('This functionality is deprecated in AdMob without replacement.') this.birthday, + @Deprecated('Use `childDirected` instead.') this.designedForFamilies, + this.childDirected, + this.testDevices, + this.nonPersonalizedAds}); + + final List keywords; + final String contentUrl; + @Deprecated('This functionality is deprecated in AdMob without replacement.') + final DateTime birthday; + @Deprecated('This functionality is deprecated in AdMob. Use `childDirected` instead.') + final bool designedForFamilies; + final bool childDirected; + final List testDevices; + final bool nonPersonalizedAds; + + Map toJson() { + final Map json = { + 'requestAgent': 'flutter-alpha', + }; + + if (keywords != null && keywords.isNotEmpty) { + assert(keywords.every((String s) => s != null && s.isNotEmpty)); + json['keywords'] = keywords; + } + if (nonPersonalizedAds != null) json['nonPersonalizedAds'] = nonPersonalizedAds; + if (contentUrl != null && contentUrl.isNotEmpty) json['contentUrl'] = contentUrl; + if (birthday != null) json['birthday'] = birthday.millisecondsSinceEpoch; + if (designedForFamilies != null) json['designedForFamilies'] = designedForFamilies; + if (childDirected != null) json['childDirected'] = childDirected; + if (testDevices != null && testDevices.isNotEmpty) { + assert(testDevices.every((String s) => s != null && s.isNotEmpty)); + json['testDevices'] = testDevices; + } + + return json; + } +} diff --git a/pubspec.lock b/pubspec.lock index be67d92..00f412f 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -19,14 +19,14 @@ packages: name: meta url: "https://pub.dartlang.org" source: hosted - version: "1.1.7" + version: "1.1.8" pedantic: dependency: "direct dev" description: name: pedantic url: "https://pub.dartlang.org" source: hosted - version: "1.8.0+1" + version: "1.9.0" sky_engine: dependency: transitive description: flutter