Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Admob interstitial ads for Fuse
using Fuse;
using Fuse.Scripting;
using Fuse.Reactive;
using Uno.UX;
[UXGlobalModule]
public class Admob : NativeModule
{
static readonly Admob _instance;
public Android.InterstitialAdPage ad;
public Admob()
{
if (_instance != null) return;
_instance = this;
Resource.SetGlobalKey(_instance, "Admob");
AddMember(new NativeFunction("displayInterstitialAd", (NativeCallback)displayInterstitialAd));
this.ad = new Android.InterstitialAdPage();
}
public object displayInterstitialAd(Context c, object[] args)
{
this.ad.displayAd();
return null;
}
}
namespace Android {
using Uno.Compiler.ExportTargetInterop;
[Require("Gradle.Dependency.Compile", "com.google.gms:google-services:3.0.0")]
[Require("Gradle.Dependency.Compile", "com.google.firebase:firebase-ads:10.0.1")]
[ForeignInclude(Language.Java, "com.google.android.gms.ads.AdRequest")]
[ForeignInclude(Language.Java, "com.google.android.gms.ads.InterstitialAd")]
[ForeignInclude(Language.Java, "com.google.android.gms.ads.MobileAds")]
[ForeignInclude(Language.Java, "com.fuse.Activity")]
public extern(Android) class InterstitialAdPage
{
public Java.Object iAd;
public InterstitialAdPage()
{
initialize();
requestNewInterstitial();
}
[Foreign(Language.Java)]
public void requestNewInterstitial()
@{
final InterstitialAd mInterstitialAd = new InterstitialAd(Activity.getRootActivity());
mInterstitialAd.setAdUnitId("[****AdUnitID****]");
final AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("[****TestDeviceID****]")
.build();
Activity.getRootActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
mInterstitialAd.loadAd(adRequest);
}
});
@{InterstitialAdPage:Of(_this).iAd:Set(mInterstitialAd)};
@}
[Foreign(Language.Java)]
public void displayAd()
@{
Object ad = @{InterstitialAdPage:Of(_this).iAd:Get()};
final InterstitialAd intAd = (InterstitialAd)ad;
Activity.getRootActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (intAd.isLoaded()) {
intAd.show();
}
}
});
@{InterstitialAdPage:Of(_this).requestNewInterstitial():Call()};
@}
[Foreign(Language.Java)]
static void initialize()
@{
MobileAds.initialize(Activity.getRootActivity(), "[****AppID****]");
@}
}
public extern(!Android) class InterstitialAdPage
{
public InterstitialAdPage() {}
public void displayAd() {}
}
}
import Admob from 'Admob'
Admob.displayInterstitialAd()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment