marius-bughiu / Plugin.AdMob

.NET MAUI plugin for AdMob
https://startdebugging.net
MIT License
32 stars 4 forks source link

Interstitials not working #3

Open alexberardi opened 4 months ago

alexberardi commented 4 months ago

Hello, I am trying to get Interstitial ads working in my .NET Maui application. It is currently failing on both iOS and Android.

In my view model, I am retrieving the singleton IInterstitialAdService from Service provider and running: _interstitialAdService.PrepareAd("{MY_AD_UNIT_ID}");

Then, on button click I am running: _interstitialAdService.ShowAd();

On that method call, my app crashes with an error: System.ArgumentNullException: Value cannot be null. (Parameter 'adUnitId') at ObjCRuntime.ThrowHelper.ThrowArgumentNullException(String argumentName) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/ObjCRuntime/ThrowHelper.cs:line 28 at Google.MobileAds.InterstitialAd.Load(String adUnitId, Request request, InterstitialAdLoadCompletionHandler completionHandler) at Plugin.AdMob.Services.InterstitialAdService.PrepareAd(String adUnitId) at Plugin.AdMob.Services.InterstitialAdService.ShowAd()

When I inspect _insterstitialAdService, I can see the private property "_ad" is indeed populated and it's AdUnitId property is populated with my ad unit id.

I've also tested using the test unit id's here: https://developers.google.com/admob/ios/test-ads#demo_ad_units with the same result.

Am I doing something wrong or is this a bug?

Additionally, it doesn't seem like AddTestDevice is working. When I run the application on my simulator, I am able to see a Test Ad banner. However, when I try running on my device, I am not able to see the banner.

In my MauiProgram.cs, I am executing: AdConfig.AddTestDevice("{MY_TEST_DEVICE_AD_ID}");

This device has been added to AdMob as a test device with a matching Ad ID.

In the console, I see a message:

To get test ads on this device, set: Objective-C GADMobileAds.sharedInstance.requestConfiguration.testDeviceIdentifiers = @[ @"{string of characters}" ]; Swift GADMobileAds.sharedInstance().requestConfiguration.testDeviceIdentifiers = [ @"{string of characters}" ] I've tried updating my call to AddTestDevice with the supplied characters from the message, but there was no change.
marius-bughiu commented 4 months ago

Hi @alexberardi, it's a bug, thank you for reporting it. You can work around it by setting AdConfig.DefaultInterstitialAdUnitId to match your ad unit id.

alexberardi commented 4 months ago

That worked! Thank you!

Do you have any information on the test device situation? I'm able to see ads in the iOS simulator, Android simulator, and a physical Android device but not an iOS device.

marius-bughiu commented 4 months ago

I would need to look into it, unfortunately I don't have an iOS device to test with, only the simulator. From what I figure, the test device IDs are set correctly - https://github.com/marius-bughiu/Plugin.AdMob/blob/887bfeecc801b77bccd93c2cc085bf2defa4201e/src/Plugin.AdMob/Platforms/iOS/InterstitialAdService.cs#L26

A wild guess would be that maybe that test devide IDs need to be set before Request.GetDefaultRequest()? I'll try to have a look at it this weekend and see if I can figure it out, but I can't make any promises.

In the meantime you could try:

marius-bughiu commented 3 months ago

alexberardi, have you managed to get this working? If not, I'm going to mark this with help wanted until I get my hands on an iOS device so I can troubleshoot; maybe someone else finds some spare time to do it.

alexberardi commented 3 months ago

I have not been able to get it working on a physical device, but I'm not sure if that's because my app is not published yet or if there's an underlying issue.

alexberardi commented 2 months ago

@marius-bughiu My app is now published and I can confirm the ads are not working on iOS. I'm able to see the requests come through on AdMob but nothing shows on the device.

marius-bughiu commented 2 months ago

@alexberardi, I've got my hands on an iPhone XR running iOS 17.4.1 and at least the test ads are showing - these are photos running the sample app in the repo:

1 2 3
20240421_181255 20240421_181229 20240421_181239
marius-bughiu commented 2 months ago

Could it be something related to the iOS version of the device or some privacy settings?

alexberardi commented 1 month ago

@marius-bughiu - finally was able to get ads working on iOS. The issue is that you need to set the DefaultBannerAdUnitId for production ads to work. I set the value and now I'm getting ads. Thank you! Leaving open because I'm not sure if you'd like to fix that but feel free to close if you are too busy.