Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add targeting info to Banner ads #97

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions android/src/main/kotlin/com/shatsy/admobflutter/AdmobBanner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<String> = targetingInfo?.get("testDevices") as? List<String> ?: listOf();
for (testDeviceId in testDevices) {
adRequest.addTestDevice(testDeviceId);
}

val keywords: List<String> = targetingInfo?.get("keywords") as? List<String> ?: 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 {
Expand Down Expand Up @@ -60,4 +82,4 @@ class AdmobBanner(context: Context, messenger: BinaryMessenger, id: Int, args: H
adView.destroy()
channel.setMethodCallHandler(null)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand Down
61 changes: 55 additions & 6 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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"
2 changes: 1 addition & 1 deletion ios/admob_flutter.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
1 change: 1 addition & 0 deletions lib/admob_flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
5 changes: 5 additions & 0 deletions lib/src/admob_banner.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
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';

class AdmobBanner extends StatefulWidget {
final String adUnitId;
final AdmobBannerSize adSize;
final MobileAdTargetingInfo targetingInfo;
final void Function(AdmobAdEvent, Map<String, dynamic>) listener;
final void Function(AdmobBannerController) onBannerCreated;

AdmobBanner({
Key key,
@required this.adUnitId,
@required this.adSize,
this.targetingInfo,
this.listener,
this.onBannerCreated,
}) : super(key: key);
Expand All @@ -38,6 +41,7 @@ class _AdmobBannerState extends State<AdmobBanner> {
creationParams: <String, dynamic>{
"adUnitId": widget.adUnitId,
"adSize": widget.adSize.toMap,
"targetingInfo": widget.targetingInfo?.toJson(),
},
creationParamsCodec: StandardMessageCodec(),
onPlatformViewCreated: _onPlatformViewCreated,
Expand All @@ -53,6 +57,7 @@ class _AdmobBannerState extends State<AdmobBanner> {
creationParams: <String, dynamic>{
"adUnitId": widget.adUnitId,
"adSize": widget.adSize.toMap,
"targetingInfo": widget.targetingInfo.toJson(),
},
creationParamsCodec: StandardMessageCodec(),
onPlatformViewCreated: _onPlatformViewCreated,
Expand Down
46 changes: 46 additions & 0 deletions lib/src/admob_targeting_info.dart
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> testDevices;
final bool nonPersonalizedAds;

Map<String, dynamic> toJson() {
final Map<String, dynamic> json = <String, dynamic>{
'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;
}
}
4 changes: 2 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down