From 01d5f37ed1eb20abf97ebb7a9dcd361ca535aafc Mon Sep 17 00:00:00 2001 From: Zalan Meggyesi Date: Mon, 28 Oct 2019 15:56:26 +0100 Subject: [PATCH 1/6] Add targeting info to Banner ads --- .../com/shatsy/admobflutter/AdmobBanner.kt | 30 ++++++++++++++++--- lib/src/admob_banner.dart | 5 ++++ 2 files changed, 31 insertions(+), 4 deletions(-) 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/lib/src/admob_banner.dart b/lib/src/admob_banner.dart index 26ed4c3..661a443 100644 --- a/lib/src/admob_banner.dart +++ b/lib/src/admob_banner.dart @@ -1,3 +1,4 @@ +import 'package:firebase_admob/firebase_admob.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.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, From 216478cfe986e7e24829ccacaaaa08d8876dccec Mon Sep 17 00:00:00 2001 From: Zalan Meggyesi Date: Fri, 1 Nov 2019 13:41:47 +0100 Subject: [PATCH 2/6] Add missing dependency declaration after porting --- example/pubspec.yaml | 1 + pubspec.yaml | 2 ++ 2 files changed, 3 insertions(+) diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 8777a8a..a1c3a9b 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -9,6 +9,7 @@ environment: dependencies: flutter: sdk: flutter + firebase_admob: 0.9.0+7 # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. diff --git a/pubspec.yaml b/pubspec.yaml index f650886..3783e5a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -14,6 +14,8 @@ dependencies: flutter: sdk: flutter meta: ^1.1.6 + firebase_admob: 0.9.0+7 + dev_dependencies: pedantic: ^1.8.0 From 720acb8ab778e4ad87c32f36ebfcee64c169737f Mon Sep 17 00:00:00 2001 From: Zalan Meggyesi Date: Sat, 14 Dec 2019 15:08:34 +0100 Subject: [PATCH 3/6] Signature change after flutter upgrade --- .../main/kotlin/com/shatsy/admobflutter/AdmobFlutterPlugin.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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() } } From 48aa340a0e1a899f0c58ef985fcc44efed7d4bff Mon Sep 17 00:00:00 2001 From: Zalan Meggyesi Date: Mon, 16 Dec 2019 07:19:16 +0100 Subject: [PATCH 4/6] CR Changes - Lifted MobileAdTargetingInfo from FlutterFire library - Broke dependency on FlutterFire - Update example --- example/pubspec.lock | 61 +++++++++++++++++++++++++++--- example/pubspec.yaml | 1 - lib/src/MobileAdTargetingInfo.dart | 46 ++++++++++++++++++++++ lib/src/admob_banner.dart | 2 +- pubspec.lock | 4 +- pubspec.yaml | 2 - 6 files changed, 104 insertions(+), 12 deletions(-) create mode 100644 lib/src/MobileAdTargetingInfo.dart 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/example/pubspec.yaml b/example/pubspec.yaml index a1c3a9b..8777a8a 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -9,7 +9,6 @@ environment: dependencies: flutter: sdk: flutter - firebase_admob: 0.9.0+7 # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. diff --git a/lib/src/MobileAdTargetingInfo.dart b/lib/src/MobileAdTargetingInfo.dart new file mode 100644 index 0000000..1ca1792 --- /dev/null +++ b/lib/src/MobileAdTargetingInfo.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/lib/src/admob_banner.dart b/lib/src/admob_banner.dart index 661a443..7a5cf22 100644 --- a/lib/src/admob_banner.dart +++ b/lib/src/admob_banner.dart @@ -1,7 +1,7 @@ -import 'package:firebase_admob/firebase_admob.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; +import 'MobileAdTargetingInfo.dart'; import 'admob_banner_controller.dart'; import 'admob_banner_size.dart'; import 'admob_events.dart'; 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 diff --git a/pubspec.yaml b/pubspec.yaml index 3783e5a..f650886 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -14,8 +14,6 @@ dependencies: flutter: sdk: flutter meta: ^1.1.6 - firebase_admob: 0.9.0+7 - dev_dependencies: pedantic: ^1.8.0 From 4174528f2898ee5f6edf4efdb534e91fe504e7ed Mon Sep 17 00:00:00 2001 From: Zalan Meggyesi Date: Mon, 16 Dec 2019 07:28:51 +0100 Subject: [PATCH 5/6] Missed exports --- lib/admob_flutter.dart | 1 + lib/src/admob_banner.dart | 2 +- .../{MobileAdTargetingInfo.dart => admob_targeting_info.dart} | 0 3 files changed, 2 insertions(+), 1 deletion(-) rename lib/src/{MobileAdTargetingInfo.dart => admob_targeting_info.dart} (100%) 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 7a5cf22..a86fbe3 100644 --- a/lib/src/admob_banner.dart +++ b/lib/src/admob_banner.dart @@ -1,7 +1,7 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; -import 'MobileAdTargetingInfo.dart'; +import 'admob_targeting_info.dart'; import 'admob_banner_controller.dart'; import 'admob_banner_size.dart'; import 'admob_events.dart'; diff --git a/lib/src/MobileAdTargetingInfo.dart b/lib/src/admob_targeting_info.dart similarity index 100% rename from lib/src/MobileAdTargetingInfo.dart rename to lib/src/admob_targeting_info.dart From 6e6423ceb5f4913c99c3f28334c999c927606e5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zal=C3=A1n=20Meggyesi?= Date: Fri, 25 Sep 2020 07:16:41 +0200 Subject: [PATCH 6/6] Unpin dependency to allow pod resolution --- ios/admob_flutter.podspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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'