Skip to content

Commit

Permalink
Fix ad unit ids
Browse files Browse the repository at this point in the history
  • Loading branch information
nilsreichardt committed Oct 14, 2024
1 parent 178bc60 commit b454800
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 37 deletions.
12 changes: 8 additions & 4 deletions app/lib/ads/ad_banner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@

import 'package:flutter/widgets.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'package:platform_check/platform_check.dart';
import 'package:provider/provider.dart';
import 'package:sharezone/ads/ads_controller.dart';

class AdBanner extends StatefulWidget {
const AdBanner({super.key});
const AdBanner({
super.key,
required this.adUnitId,
});

final String adUnitId;

@override
State<AdBanner> createState() => _AdBannerState();
Expand All @@ -26,7 +30,7 @@ class _AdBannerState extends State<AdBanner> {
@override
void initState() {
super.initState();
if (PlatformCheck.isMobile) {
if (context.read<AdsController>().isQualifiedForAds()) {
WidgetsBinding.instance.addPostFrameCallback((_) async {
final size =
await AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(
Expand All @@ -42,7 +46,7 @@ class _AdBannerState extends State<AdBanner> {
}

ad = BannerAd(
adUnitId: context.read<AdsController>().getAdUnitId(AdFormat.banner),
adUnitId: widget.adUnitId,
request: context.read<AdsController>().createAdRequest(),
size: size,
listener: BannerAdListener(
Expand Down
82 changes: 53 additions & 29 deletions app/lib/ads/ads_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,18 @@ class AdsController extends ChangeNotifier {
required this.remoteConfiguration,
required this.keyValueStore,
}) {
if (_isQualifiedForAds()) {
if (isQualifiedForAds()) {
_initializeMobileAdsSDK();
_listenToSubscriptionService();
}
}

bool _isQualifiedForAds() {
bool isQualifiedForAds() {
if (!PlatformCheck.isMobile) {
// Google AdMob SDK is only available on mobile platforms.
return false;
}

final isRemoteConfigFlagEnabled =
remoteConfiguration.getBool('ads_enabled');
final isActivationFlagEnabled = keyValueStore.getBool('show-ads') ?? false;
Expand All @@ -52,7 +57,7 @@ class AdsController extends ChangeNotifier {
///
/// We show an info dialog about our ads experiment after the first app open.
bool shouldShowInfoDialog() {
if (!_isQualifiedForAds()) {
if (!isQualifiedForAds()) {
return false;
}

Expand All @@ -66,29 +71,36 @@ class AdsController extends ChangeNotifier {
return false;
}

String getAdUnitId(AdFormat format) {
if (kDebugMode) {
return switch (PlatformCheck.currentPlatform) {
// Test ad unit IDs from: https://developers.google.com/admob/flutter/
Platform.android => switch (format) {
AdFormat.banner => 'ca-app-pub-3940256099942544/6300978111',
AdFormat.native => 'ca-app-pub-3940256099942544/2247696110',
AdFormat.interstitial => 'ca-app-pub-3940256099942544/1033173712',
},
Platform.iOS => switch (format) {
AdFormat.banner => 'ca-app-pub-3940256099942544/2934735716',
AdFormat.native => 'ca-app-pub-3940256099942544/3986624511',
AdFormat.interstitial => 'ca-app-pub-3940256099942544/4411468910',
},
_ => 'N/A',
};
}
// Returns the test ad unit ID for the given [format].
//
// Used for testing purposes only.
String getTestAdUnitId(AdFormat format) {
// From: https://developers.google.com/admob/flutter/banner#always_test_with_test_ads
const testAdUnitIdBannerAndroid = 'ca-app-pub-3940256099942544/6300978111';
const testAdUnitIdBannerIOS = 'ca-app-pub-3940256099942544/2934735716';

// From: https://developers.google.com/admob/flutter/native/templates#always_test_with_test_ads
const testAdUnitIdNativeAndroid = 'ca-app-pub-3940256099942544/2247696110';
const testAdUnitIdNativeIOS = 'ca-app-pub-3940256099942544/3986624511';

// From: https://developers.google.com/admob/flutter/interstitial#always_test_with_test_ads
const testAdUnitIdInterstitialAndroid =
'ca-app-pub-3940256099942544/1033173712';
const testAdUnitIdInterstitialIOS =
'ca-app-pub-3940256099942544/4411468910';

return switch (PlatformCheck.currentPlatform) {
// In case you need to change the ad unit ID, also update the one in the
// AndroidManifest.xml file.
Platform.android => 'ca-app-pub-7730914075870960~2331360962',
Platform.iOS => 'ca-app-pub-7730914075870960~4953718516',
// Test ad unit IDs from: https://developers.google.com/admob/flutter/
Platform.android => switch (format) {
AdFormat.banner => testAdUnitIdBannerAndroid,
AdFormat.native => testAdUnitIdNativeAndroid,
AdFormat.interstitial => testAdUnitIdInterstitialAndroid,
},
Platform.iOS => switch (format) {
AdFormat.banner => testAdUnitIdBannerIOS,
AdFormat.native => testAdUnitIdNativeIOS,
AdFormat.interstitial => testAdUnitIdInterstitialIOS,
},
_ => 'N/A',
};
}
Expand All @@ -100,7 +112,7 @@ class AdsController extends ChangeNotifier {
if (hasUnlocked) {
areAdsVisible = false;
} else {
areAdsVisible = _isQualifiedForAds();
areAdsVisible = isQualifiedForAds();
}
notifyListeners();
});
Expand All @@ -113,6 +125,7 @@ class AdsController extends ChangeNotifier {
// form.
tagForUnderAgeOfConsent: 1,
));
debugPrint("Google Mobile Ads SDK initialized.");
}

AdRequest createAdRequest() {
Expand All @@ -133,7 +146,7 @@ class AdsController extends ChangeNotifier {
}

if (areAdsVisible && _shouldShowFullscreenAd()) {
await _showFullscreenAd(adUnitId: getAdUnitId(AdFormat.interstitial));
await _showFullscreenAd();
}
}

Expand All @@ -152,9 +165,20 @@ class AdsController extends ChangeNotifier {
return checkedHwCounter % 5 == 0;
}

Future<void> _showFullscreenAd({
required String adUnitId,
}) async {
Future<void> _showFullscreenAd() async {
String adUnitId;

if (kDebugMode) {
adUnitId = getTestAdUnitId(AdFormat.interstitial);
} else {
adUnitId = switch (PlatformCheck.currentPlatform) {
// Copied from the AdMob Console
Platform.android => 'ca-app-pub-7730914075870960/5232564896',
Platform.iOS => 'ca-app-pub-7730914075870960/4462307071',
_ => 'N/A',
};
}

InterstitialAd.load(
adUnitId: adUnitId,
request: createAdRequest(),
Expand Down
18 changes: 16 additions & 2 deletions app/lib/dashboard/sections/ad_section.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
// SPDX-License-Identifier: EUPL-1.2

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'package:platform_check/platform_check.dart';
Expand All @@ -27,17 +28,30 @@ class _DashboardAdsState extends State<DashboardAds> {
@override
void initState() {
super.initState();
if (PlatformCheck.isMobile) {
if (context.read<AdsController>().isQualifiedForAds()) {
WidgetsBinding.instance.addPostFrameCallback((_) {
loadAd();
});
}
}

String getAdUnitId() {
if (kDebugMode) {
return context.read<AdsController>().getTestAdUnitId(AdFormat.native);
}

return switch (PlatformCheck.currentPlatform) {
// Copied from the AdMob Console
Platform.android => 'ca-app-pub-7730914075870960/4681798929',
Platform.iOS => 'ca-app-pub-7730914075870960/2027715422',
_ => 'N/A',
};
}

/// Loads a native ad.
void loadAd() {
nativeAd = NativeAd(
adUnitId: context.read<AdsController>().getAdUnitId(AdFormat.native),
adUnitId: getAdUnitId(),
listener: NativeAdListener(
onAdLoaded: (ad) {
debugPrint('$NativeAd loaded.');
Expand Down
18 changes: 17 additions & 1 deletion app/lib/homework/student/student_homework_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
// SPDX-License-Identifier: EUPL-1.2

import 'package:bloc_provider/bloc_provider.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:hausaufgabenheft_logik/hausaufgabenheft_logik.dart';
import 'package:platform_check/platform_check.dart';
import 'package:provider/provider.dart';
import 'package:rxdart/rxdart.dart';
import 'package:sharezone/ads/ad_banner.dart';
import 'package:sharezone/ads/ads_controller.dart';
import 'package:sharezone/homework/shared/shared.dart';
import 'package:sharezone/homework/student/src/homework_bottom_action_bar.dart';
import 'package:sharezone/navigation/logic/navigation_bloc.dart';
Expand All @@ -29,6 +32,19 @@ import 'src/open_homework_list.dart';
class StudentHomeworkPage extends StatelessWidget {
const StudentHomeworkPage({super.key});

String getAdUnitId(BuildContext context) {
if (kDebugMode) {
return context.read<AdsController>().getTestAdUnitId(AdFormat.banner);
}

return switch (PlatformCheck.currentPlatform) {
// Copied from the AdMob Console
Platform.android => 'ca-app-pub-7730914075870960/6002721082',
Platform.iOS => 'ca-app-pub-7730914075870960/5068913364',
_ => 'N/A',
};
}

@override
Widget build(BuildContext context) {
final bloc = BlocProvider.of<StudentHomeworkPageBloc>(context);
Expand Down Expand Up @@ -59,7 +75,7 @@ class StudentHomeworkPage extends StatelessWidget {
bottomBar: Column(
mainAxisSize: MainAxisSize.min,
children: [
const AdBanner(),
AdBanner(adUnitId: getAdUnitId(context)),
AnimatedTabVisibility(
visibleInTabIndicies: const [0],
// Else the Sort shown in the button and the current sort
Expand Down
2 changes: 2 additions & 0 deletions app/lib/main/plugin_initializations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ class PluginInitializations {
// provides a better user experience.
'ads_enabled': false,
'ad_content_url': 'https://sharezone.net',
'ad_neighboring_urls':
'https://sharezone.net/android,https://sharezone.net/ios',
});

try {
Expand Down
19 changes: 18 additions & 1 deletion app/lib/timetable/timetable_page/timetable_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ import 'dart:math';

import 'package:bloc_provider/bloc_provider.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:group_domain_models/group_domain_models.dart';
import 'package:platform_check/platform_check.dart';
import 'package:provider/provider.dart';
import 'package:sharezone/ads/ad_banner.dart';
import 'package:sharezone/ads/ads_controller.dart';
import 'package:sharezone/calendrical_events/models/calendrical_event.dart';
import 'package:sharezone/main/application_bloc.dart';
import 'package:sharezone/navigation/logic/navigation_bloc.dart';
Expand Down Expand Up @@ -50,6 +54,19 @@ class TimetablePage extends StatelessWidget {

TimetablePage({super.key});

String getAdUnitId(BuildContext context) {
if (kDebugMode) {
return context.read<AdsController>().getTestAdUnitId(AdFormat.banner);
}

return switch (PlatformCheck.currentPlatform) {
// Copied from the AdMob Console
Platform.android => 'ca-app-pub-7730914075870960/7645268953',
Platform.iOS => 'ca-app-pub-7730914075870960/6326053086',
_ => 'N/A',
};
}

@override
Widget build(BuildContext context) {
final bottomBarBackgroundColor =
Expand Down Expand Up @@ -98,7 +115,7 @@ class TimetablePage extends StatelessWidget {
bottomBar: Column(
mainAxisSize: MainAxisSize.min,
children: [
const AdBanner(),
AdBanner(adUnitId: getAdUnitId(context)),
SchoolClassFilterBottomBar(
backgroundColor: bottomBarBackgroundColor,
),
Expand Down

0 comments on commit b454800

Please sign in to comment.