Admob Ads do not show up (XamarinForms + C# + Android) - c#

I'm trying to show a bottom banner on my App but I can't make it show up.
Funny that it was working just fine but after a few weeks without coding it just stopped working and not even test codes from Google show up. Sometimes it works after a compilation and when I unplug the phone and close/open the App again it stop showing the ads again.
I also used the same Admob code into a Unity quiz game I created and over there it works fine everytime.
PS: I'm a bit new to Xamarin and C#, this is my first App.
Here's the relevant code:
MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:DDP"
x:Class="DDP.MainPage"
BackgroundColor="#3f183d"
Title="My App Title">
<StackLayout BackgroundColor="Transparent" HeightRequest="70" HorizontalOptions="Start" VerticalOptions="Center" WidthRequest="1000">
<local:AdMobView x:Name="adMobView" HorizontalOptions="FillAndExpand" VerticalOptions="EndAndExpand"/>
</StackLayout>
</ContentPage>
MainPage.xaml.cs
public MainPage()
{
InitializeComponent();
BindingContext = this;
adMobView.AdUnitId = AdMobView.codigoAdmob;
}
AdMobView.cs
using Xamarin.Forms;
namespace DDP
{
public class AdMobView : View
{
public static readonly BindableProperty AdUnitIdProperty = BindableProperty.Create(
nameof(AdUnitId),
typeof(string),
typeof(AdMobView),
string.Empty);
public string AdUnitId
{
get => (string)GetValue(AdUnitIdProperty);
set => SetValue(AdUnitIdProperty, value);
}
//admob google test code
public static string codigoAdmob = "ca-app-pub-xxxxx/xxxxx";
}
}
AdMobViewRenderer.cs
using System.ComponentModel;
using DDP;
using DDP.Droid;
using Android.Content;
using Android.Gms.Ads;
using Android.Widget;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(AdMobView), typeof(AdMobViewRenderer))]
namespace DDP.Droid
{
public class AdMobViewRenderer : ViewRenderer<AdMobView, AdView>
{
public AdMobViewRenderer(Context context) : base(context) { }
protected override void OnElementChanged(ElementChangedEventArgs<AdMobView> e)
{
base.OnElementChanged(e);
if (e.NewElement != null && Control == null)
{
SetNativeControl(CreateAdView());
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == nameof(AdView.AdUnitId))
Control.AdUnitId = Element.AdUnitId;
}
private AdView CreateAdView()
{
var adView = new AdView(Context)
{
AdSize = AdSize.SmartBanner,
AdUnitId = Element.AdUnitId
};
adView.LayoutParameters = new LinearLayout.LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent);
adView.LoadAd(new AdRequest.Builder().Build());
return adView;
}
}
}
MainActivity.cs
//somecode
base.OnCreate(bundle);
MobileAds.Initialize(ApplicationContext, "ca-app-pub-xxxxx/xxxxx");
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
//somecode
AndroidManifest.xml
Other info
I'm using 'Xamarin.GooglePlayServices.Ads.Lite' NuGet package and I checked permissions to 'Network_State' and 'Internet'
Thanks !
Update 1: Catlog log:
--------- beginning of crash
--------- beginning of system
--------- beginning of main
11-19 11:31:04.852 11568 11568 I Ads : Updating ad debug logging enablement.
11-19 11:31:06.407 11568 11596 W Ads : Update ad debug logging enablement as false
11-19 11:31:07.109 11568 11568 I Ads : Use AdRequest.Builder.addTestDevice("1BE57C53121A02D9EF3DD79A87C60D3C") to get test ads on this device.
11-19 11:31:07.916 11568 11593 W Ads : Not retrying to fetch app settings
11-19 11:31:08.118 27571 11755 I Ads : SDK version: afma-sdk-a-v14574021.11400000.1
11-19 11:31:08.901 11568 11568 I Ads : Ad failed to load : 3

Filter by the TAG of Ads and you will see different Information and/or Warning log entries concerning AdMob. These will range from timed outs and load failures (usually related to no Internet access) to not refreshing the ad since it is not currently visible in the UI (but an AdView instance was created), etc...
Example:
adb logcat -s Ads
Example Output (Not a complete list):
I Ads : Starting ad request.
I Ads : SDK version: XXXXXXXXXX
I Ads : This request is sent from a test device.
I Ads : Scheduling ad refresh 70000 milliseconds from now.
I Ads : Ad is not visible. Not refreshing ad.
W Ads : There was a problem getting an ad response. ErrorCode: 0
W Ads : Failed to load ad: 0
W Ads : Not retrying to fetch app settings
W Ads : Invoke Firebase method getInstance error.
W Ads : The Google Mobile Ads SDK will not integrate with Firebase. Admob/Firebase integration requires the latest Firebase SDK jar, but Firebase SDK is either missing or out of date
W Ads : App does not have the required permissions to get location
W Ads : Timed out waiting for ad response.
W Ads : Failed to load ad: 2

I'm sorry that can't say how to fix this but I think it is related to Chrome version, chrome ships webview which admob is using. I had a perfectly working admob ads in my xamarin forms app on Nexus 5X device with Chrome 60 but upgrading to Chrome 70+ broke all ads.

Related

I am getting a break in my android emulator: failed to load libc++_shared exception dalvik.system.PathClassLoader[DexPathList[[zip file \"/system/…"

I am trying to implement a routine to cast a video stream using LibVLCSharp but when it gets to where I invoke the core.initialise command, it triggers the above error.
Here's the code in question:
private void BroadcastStream() //string[] args)
{
var serverIpAddress = "192.168.1.101";
var streamUrl = $"rtsp://{serverIpAddress}:554/stream.sdp";
try
{
LibVLCSharp.Shared.Core.Initialize();
var libVlc = new LibVLC();
var media = new Media(libVlc, "fe48f269-4f89-4c58-afd4-04c2e2e3f10f.MP4");
media.AddOption(
$":sout=#transcode{{vcodec=h264,vb=0,scale=0,acodec=mp4a,ab=128,channels=2,samplerate=44100}}:rtp{{mux=ts,sdp={streamUrl}}}");
media.AddOption(":sout-keep");
var player = new MediaPlayer(media);
player.Play();
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
I am using using "LibVLCSharp.Shared;" in the code file and as per instructions I invoked "LibVLCSharpFormsRenderer.Init();" inside "protected override void OnCreate(Bundle savedInstanceState)" in the Android project's MainActivity.cs file.
So, how do I get around this issue?
I loaded the default LibVLSharp.forms library from Nuget for all the projects (iOS, Android & UWP) and that's what caused the issue. I uninstalled the library for iOS and Android and installed their own specific LibVLSharp libraries and it appears to be ok now (at least it no more breaks at core.initialise).

Test ads show on app but real ones dont show

i am using unity and admob ,and now i have a problem
When i try to show test ads they show without a problem , everything works fine, but when i try to show real ads they are not displayed.
I tried the following:
I created admob account
I coppied the AppID into my code , and also in the xml file
I did everything from the turorials , with some changes because google dont know how to explain.Then i debugged with logcat , and i got the following error
java.lang.RuntimeException: Unable to get provider com.google.android.gms.ads.MobileAdsInitProvider: java.lang.IllegalStateException:
10-08 22:24:53.917 30153 30153 E AndroidRuntime:
10-08 22:24:53.917 30153 30153 E AndroidRuntime: ******************************************************************************
10-08 22:24:53.917 30153 30153 E AndroidRuntime: * Invalid application ID. Follow instructions here: *
10-08 22:24:53.917 30153 30153 E AndroidRuntime: * https://googlemobileadssdk.page.link/admob-android-update-manifest *
10-08 22:24:53.917 30153 30153 E AndroidRuntime: * to find your app ID.
This is my Code for showing banners
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GoogleMobileAds.Api;
public class AdmobManager : MonoBehaviour
{
private BannerView bannerView;
void Start()
{
#if UNITY_ANDROID
string appId = "ca-app-pub-6068823890509836~9548685797";
#elif UNITY_IPHONE
string appId = "ca-app-pub-3940256099942544~1458002511";
#else
string appId = "unexpected_platform";
#endif
// Initialize the Google Mobile Ads SDK.
MobileAds.Initialize(appId);
this.RequestBanner();
}
// Update is called once per frame
private void RequestBanner()
{
#if UNITY_ANDROID
string adUnitId = "ca-app-pub-6068823890509836/3679968367";
#elif UNITY_IPHONE
string adUnitId = "ca-app-pub-3940256099942544/2934735716";
#else
string adUnitId = "unexpected_platform";
#endif
// Create a 320x50 banner at the top of the screen.
bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Bottom);
AdRequest request = new AdRequest.Builder().Build();
// Load the banner with the request.
bannerView.LoadAd(request);
}
}
and this is my xml file
> <?xml version="1.0" encoding="utf-8"?> <manifest
> xmlns:android="http://schemas.android.com/apk/res/android"
> package="com.google.unity.ads" android:versionName="1.0"
> android:versionCode="1"> <application>
> <uses-library android:required="false" android:name="org.apache.http.legacy" />
> <meta-data android:name="com.google.android.gms.ads.APPLICATION_ID"
> android:value="ca-app-pub-6068823890509836~9548685797" />
> </application> </manifest>
Also i made the banner yesterday so 24hours has passed .
If test ads were already shown, I don't think there is anything wrong on your part.
Once your app is making more requests, you should see more consistent results. Please note that test ads operate through the same channels as live ads. Being able to return a test ad ensures that your application is communicating properly with our network.
Source: https://support.google.com/admob/answer/2993019?hl=en

How to add Apple Wallet Passes with custom name and QR code in Xamarin.IOS

How can I add an Apple Wallet pass with a custom username and contents of a QR code? If possible can they be downloaded from a server?
I have tried altering existing passes but there is no capability to do that in Xamarin.
So the beauty of using Xamarin is that you have access to all the native iOS APIs that are available to Swift developers, so you can do everything in C# that you can with Swift without the need for custom binding.
So anything that you can do using Swift on XCode, you can do that in an identical way using C#. So in order to implement the Apple Wallet passes, you have to go through the same procedures. The procedure is slightly long since this is for Card Issuers only and you need a special entitlement issued by Apple:
Your app must include this entitlement before you can use this class.
For more information on requesting this entitlement, see the Card
Issuers section at developer.apple.com/apple-pay/.
Also, from here:
PKAddPaymentPassViewController requires the com.apple.developer.payment-pass-provisioning entitlement
key for your app. The bad news is that not anyone can submit apps with
this entitlement as it requires special permission from Apple, which I
believe is reserved for card issuers like banks and similar. If you
believe that you qualify you need to contact Apple directly at
apple-pay-inquiries#apple.com
Once you get that done, you need to implement the delegate methods, and initialize it with a configuration as you can see in the code/picture below (Converted from Swift):
using System;
using CoreGraphics;
using Foundation;
using ObjCRuntime;
using PassKit;
using UIKit;
namespace BlankNativeApp.iOS
{
public class PKViewController : UIViewController, IPKAddPaymentPassViewControllerDelegate
{
public void DidFinishAddingPaymentPass(PKAddPaymentPassViewController controller, PKPaymentPass pass, NSError error)
{
// Perform Post Addition Functionality
}
public void GenerateRequestWithCertificateChain(PKAddPaymentPassViewController controller, NSData[] certificates, NSData nonce, NSData nonceSignature, [BlockProxy(typeof(NIDActionArity1V173))] Action<PKAddPaymentPassRequest> handler)
{
// Do work that needs to be done with certifications
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
if (!PKAddPaymentPassViewController.CanAddPaymentPass)
{
// use other payment method / alert user
}
var config = new PKAddPaymentPassRequestConfiguration(PKEncryptionScheme.Ecc_V2);
var addPaymentPassVC = new PKAddPaymentPassViewController(config, this);
View.BackgroundColor = UIColor.White;
Title = "My Custom View Controller";
var btn = UIButton.FromType(UIButtonType.System);
btn.Frame = new CGRect(20, 200, 280, 44);
btn.SetTitle("Click Me", UIControlState.Normal);
btn.TouchUpInside += (sender, e) => {
//this.ShowViewController(addPaymentPassVC, (Foundation.NSObject)sender); This line will also work
this.PresentViewControllerAsync(addPaymentPassVC, true);
};
View.AddSubview(btn);
}
}
}

Admob Banner Ads are not working properly in Unity3D

I integrate Ad-mob banner ads in my Unity game project android build
But Sometimes the ad is showing but most of time it's not
showing percentage will be just just 4% out of 100%
I don't know why this happening
I got this following code from developers.google and i am able to show ads but it does not show the banner app on every startup frankly speaking it only shows ads in first or 2nd startup after build and not anymore.
I try to find solutions but looks like all solutions are same.
So, I am Stuck in here
using System.Collections;
using System.Collections.Generic;
using GoogleMobileAds;
using GoogleMobileAds.Api;
using UnityEngine;
public class BannerAd : MonoBehaviour
{
private BannerView bannerView;
// Use this for initialization
void Start ()
{
string appID = "ca-app-pub-id"; // this is appId
MobileAds.Initialize (appID);
string adUnitId = "ca-app-pub-adUnitId"; // adUnitID
this.showBannerAd (adUnitId);
}
private void showBannerAd (string adUnitId)
{
//Create a custom ad size at the bottom of the screen
AdSize adSize = new AdSize (250, 50);
bannerView = new BannerView (adUnitId, adSize, AdPosition.Bottom);
// Create an empty ad request.
AdRequest request = new AdRequest.Builder ().Build ();
// Load the banner with the request.
bannerView.LoadAd (request);
StartCoroutine(bannerAdTime());
}
IEnumerator bannerAdTime ()
{
yield return new WaitForSeconds (300f);
RequestNewAd();
}
private void RequestNewAd ()
{
// Create an empty ad request.
AdRequest request = new AdRequest.Builder ().Build ();
// Load the banner with the request.
bannerView.LoadAd (request);
StartCoroutine(bannerAdTime());
}
// Update is called once per frame
void Update ()
{
}
}
1.Check you have install plugin as document https://github.com/unity-plugins/Unity-Admob
2.Edit AndroidManifest.xml and config Admob APP ID
This configuration is reqired by admob from version 17.0,APP will crash if not config .Add a meta-data tag in application and set value to your admob appid
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="your admob app id"/>
Sample code
<application android:theme="#style/UnityThemeSelector"
android:icon="#drawable/app_icon"
android:label="#string/app_name" >
<activity
android:name="com.unity3d.player.UnityPlayerActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713"/>
</application>
3.Init Admob Unity Plugin
Create A C# script ,drag the script to a object on scene , add the follow code in the script file
using admob;
Admob.Instance().initSDK("admob appid", new AdProperties());//admob id with format ca-app-pub-3940256099942544~3347511713
//Admob.Instance().initAdmob("ca-app-pub-3940256099942544/2934735716", new AdProperties());
4.Add Admob Banner in Unity App
Admob.Instance().showBannerRelative("your admob banner unit id",AdSize.BANNER, AdPosition.BOTTOM_CENTER, 0);
I found the solution
The problem was with my Emulator
With The Real Device it work fine
and there's no need to use the co-routine
the following codes will be enough to show the BannderAds
using System.Collections;
using System.Collections.Generic;
using GoogleMobileAds;
using GoogleMobileAds.Api;
using UnityEngine;
public class BannerAd : MonoBehaviour
{
private BannerView bannerView;
// Use this for initialization
void Start ()
{
string appID = "ca-app-pub-id"; // this is appId
MobileAds.Initialize (appID);
string adUnitId = "ca-app-pub-adUnitId"; // adUnitID
this.showBannerAd (adUnitId);
}
private void showBannerAd (string adUnitId)
{
//Create a custom ad size at the bottom of the screen
AdSize adSize = new AdSize (250, 50);
bannerView = new BannerView (adUnitId, adSize, AdPosition.Bottom);
// Create an empty ad request.
AdRequest request = new AdRequest.Builder ().Build ();
// Load the banner with the request.
bannerView.LoadAd (request);
}
}

Xamarin.Forms XAML Workbook Demonstration (Android)

How Can i do this for the Android Xamarin Workbook ?
I'm stack at
Xamarin.Forms.Forms.Init();
var a = new App();
KeyWindow.RootViewController = a.MainPage.CreateViewController();
How to this for Android :
Xamarin.Forms.Forms.Init(needs 2 pram)
KeyWindow.RootViewController
a.MainPage.CreateViewController()
Here Is the Code For iOS
https://developer.xamarin.com/workbooks/xamarin-forms/user-interface/xaml/LoadXaml.workbook
Xamarin.Forms XAML Workbook Demonstration (iOS)
Steps to use XAML
1. Start by importing the NuGets for Xamarin.Forms and the iOS Platform Renderers
```csharp
#r "Xamarin.Forms.Platform.iOS"
#r "Xamarin.Forms.Core"
#r "Xamarin.Forms.Xaml"
#r "Xamarin.Forms.Platform"
```
And for this hack to work, add the Dynamic Xamarin Forms (preview)
NuGet (which contains the magic to load XAML from a string):
```csharp
#r "Xamarin.Forms.Dynamic"
```
2. Add the using statements next:
```csharp
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
```
3. Write up a simple XAML ContentPageto render on iOS:
```csharp
static string xaml = #"<?xml version='1.0' encoding='UTF-8' ?>
<ContentPage xmlns='http://xamarin.com/schemas/2014/forms'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
x:Class='XamlPage'
Title='Xaml Text' Padding='40'>
<StackLayout Orientation='Vertical'>
<Label Text='Hello from XAML' x:Name='helloLabel'/>
<BoxView Color='Blue' WidthRequest='300' HeightRequest='2' />
</StackLayout>
</ContentPage>";
```
4. Bootstrap the Xamarin.Forms app object and and for the main page class, then use the Dynamic Xamarin Forms LoadFromXaml extension method to parse the xaml string:
```csharp
public class App : Application
{
public ContentPage XamlPage {get;set;}
public App ()
{
XamlPage = new ContentPage();
XamlPage.LoadFromXaml (xaml); // loads XAML
MainPage = XamlPage;
}
}
```
5. Finally, set the iOS root view controller directly (in a real Xamarin.Forms app, this would be taken care of by the FormsApplicationDelegate subclass):
```csharp
Xamarin.Forms.Forms.Init();
var a = new App();
KeyWindow.RootViewController = a.MainPage.CreateViewController();
```
One More Thing...
Loading XAML in this way does not allow strongly-typed access to the elements by their x:Name, instead they can only be referenced using FindByNameas shown here to update the label:
```csharp
var l = a.XamlPage.FindByName<Xamarin.Forms.Label>("helloLabel");
l.Text = "Updated by the Workbook!";
a.XamlPage.Content
```

Categories