Created
February 20, 2017 13:36
-
-
Save Abdenasser/547ffa6460107759fbb91eb7a1130c8d to your computer and use it in GitHub Desktop.
Admob interstitial ads for Fuse
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Admob from 'Admob' | |
Admob.displayInterstitialAd() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment