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 3 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
1 change: 1 addition & 0 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions lib/src/admob_banner.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -8,13 +9,15 @@ 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
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ dependencies:
flutter:
sdk: flutter
meta: ^1.1.6
firebase_admob: 0.9.0+7
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we want to depend on firebase_admob If you'd like to add targeting info it should be implemented in this code base.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to add MobileAdTargetingInfo to the dart side here I can help with the Swift side.



dev_dependencies:
pedantic: ^1.8.0
Expand Down