Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Commit

Permalink
#19 Conflict with nativescript-admob
Browse files Browse the repository at this point in the history
#55 Support for AdMob
#85 Google Service conflits on using nativescript-admob with this plugin
#284 nativescript-plugin-firebase and nativescript admob conflict
  • Loading branch information
eddyverbruggen committed Feb 17, 2017
1 parent 3610ca4 commit 1ff57c9
Show file tree
Hide file tree
Showing 9 changed files with 1,073 additions and 586 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ For readability the supported features have been moved to their own README's:
* [Storage](docs/STORAGE.md)
* [Crash Reporting](docs/CRASHREPORTING.md)
* [Analytics](docs/ANALYTICS.md)
* [AdMob](docs/ADMOB.md)

## Prerequisites
Head on over to [https://console.firebase.google.com/](https://console.firebase.google.com/) and sign up for a free account.
Expand Down Expand Up @@ -45,6 +46,8 @@ cd node_modules/nativescript-plugin-firebase

If you run NativeScript 2.5.0 (only that exact version) then run (with other versions this runs automatically through a postinstall script):

__NativeScript 2.5.0 is no longer the latest release, so you might as well update before adding this plugin to avoid this issue.__

```
npm run setup
```
Expand Down
10 changes: 10 additions & 0 deletions docs/ADMOB.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<img src="images/firebase-logo.png" width="116px" height="32px" alt="Firebase"/>

<img src="images/features/admob.png" width="296px" height="124px" alt="admob"/>

## Enabling AdMob
Since plugin version 3... you can use Firebase _AdMob_ features.

_AdMob_ lets you ..

WORK IN PROGRESS.. updating right now (Feb 17, 2017)
37 changes: 37 additions & 0 deletions firebase-common.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,42 @@
var firebase = {};
firebase.analytics = {};
firebase.admob = {};
firebase.admob.AD_SIZE = {
SMART_BANNER: "SMART",
LARGE_BANNER: "LARGE",
BANNER: "BANNER",
MEDIUM_RECTANGLE: "MEDIUM",
FULL_BANNER: "FULL",
LEADERBOARD: "LEADERBOARD",
SKYSCRAPER: "SKYSCRAPER",
FLUID: "FLUID"
};
firebase.admob.defaults = {
margins: {
top: -1,
bottom: -1
},
testing: false,
size: firebase.admob.AD_SIZE.SMART_BANNER
};

firebase.merge = function merge(obj1, obj2){ // Our merge function
var result = {}; // return result
for(var i in obj1){ // for every property in obj1
if((i in obj2) && (typeof obj1[i] === "object") && (i !== null)){
result[i] = merge(obj1[i],obj2[i]); // if it's an object, merge
}else{
result[i] = obj1[i]; // add it to result
}
}
for(i in obj2){ // add the remaining properties from object 2
if(i in result){ //conflict
continue;
}
result[i] = obj2[i];
}
return result;
};

firebase.LoginType = {
ANONYMOUS: "anonymous",
Expand Down
184 changes: 184 additions & 0 deletions firebase.android.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var appModule = require("application");
var utils = require("utils/utils");
var frame = require("ui/frame");
var fs = require("file-system");
var firebase = require("./firebase-common");

Expand Down Expand Up @@ -369,6 +370,189 @@ firebase.analytics.setUserProperty = function (arg) {
});
};

//see https://firebase.google.com/docs/admob/android/quick-start
firebase.admob.showBanner = function (arg) {
return new Promise(function (resolve, reject) {
try {
var settings = firebase.merge(arg, firebase.admob.defaults);

// this should also be possible in init
com.google.android.gms.ads.MobileAds.initialize(
appModule.android.context,
settings.androidBannerId); // TODO not sure its bound to packagename.. this is from the admob-demo

// always close a previously opened banner
if (firebase.admob.adView !== null && firebase.admob.adView !== undefined) {
var parent = firebase.admob.adView.getParent();
if (parent !== null) {
parent.removeView(firebase.admob.adView);
}
}

firebase.admob.adView = new com.google.android.gms.ads.AdView(appModule.android.foregroundActivity);
firebase.admob.adView.setAdUnitId(settings.androidBannerId);
var bannerType = firebase.admob._getBannerType(settings.size);
firebase.admob.adView.setAdSize(bannerType);
console.log("----- bannerType: " + bannerType);
// TODO consider implementing events
//firebase.admob.adView.setAdListener(new com.google.android.gms.ads.BannerListener());

var ad = firebase.admob._buildAdRequest(settings);
firebase.admob.adView.loadAd(ad);

var density = utils.layout.getDisplayDensity(),
top = settings.margins.top * density,
bottom = settings.margins.bottom * density;

var relativeLayoutParams = new android.widget.RelativeLayout.LayoutParams(
android.widget.RelativeLayout.LayoutParams.MATCH_PARENT,
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);

if (bottom > -1) {
relativeLayoutParams.bottomMargin = bottom;
relativeLayoutParams.addRule(android.widget.RelativeLayout.ALIGN_PARENT_BOTTOM);
} else {
if (top > -1) {
relativeLayoutParams.topMargin = top;
}
relativeLayoutParams.addRule(android.widget.RelativeLayout.ALIGN_PARENT_TOP);
}

var adViewLayout = new android.widget.RelativeLayout(appModule.android.foregroundActivity);
adViewLayout.addView(firebase.admob.adView, relativeLayoutParams);

var relativeLayoutParamsOuter = new android.widget.RelativeLayout.LayoutParams(
android.widget.RelativeLayout.LayoutParams.MATCH_PARENT,
android.widget.RelativeLayout.LayoutParams.MATCH_PARENT);

// wrapping it in a timeout makes sure that when this function is loaded from
// a Page.loaded event 'frame.topmost()' doesn't resolve to 'undefined'
setTimeout(function() {
frame.topmost().currentPage.android.getParent().addView(adViewLayout, relativeLayoutParamsOuter);
}, 0);

resolve();
} catch (ex) {
console.log("Error in firebase.admob.showBanner: " + ex);
reject(ex);
}
});
};

firebase.admob.showInterstitial = function (arg) {
return new Promise(function (resolve, reject) {
try {
var settings = firebase.merge(arg, firebase.admob.defaults);
firebase.admob.interstitialView = new com.google.android.gms.ads.InterstitialAd(appModule.android.foregroundActivity);
firebase.admob.interstitialView.setAdUnitId(settings.androidInterstitialId);

// Interstitial ads must be loaded before they can be shown, so adding a listener
var InterstitialAdListener = com.google.android.gms.ads.AdListener.extend({
onAdLoaded: function () {
firebase.admob.interstitialView.show();
resolve();
},
onAdFailedToLoad: function (errorCode) {
reject(errorCode);
},
onAdClosed: function() {
firebase.admob.interstitialView.setAdListener(null);
firebase.admob.interstitialView = null;
}
});
firebase.admob.interstitialView.setAdListener(new InterstitialAdListener());

var ad = firebase.admob._buildAdRequest(settings);
firebase.admob.interstitialView.loadAd(ad);
} catch (ex) {
console.log("Error in firebase.admob.showInterstitial: " + ex);
reject(ex);
}
});
};

firebase.admob.hideBanner = function (arg) {
return new Promise(function (resolve, reject) {
try {
if (firebase.admob.adView !== null) {
var parent = firebase.admob.adView.getParent();
if (parent !== null) {
parent.removeView(firebase.admob.adView);
}
firebase.admob.adView = null;
}
resolve();
} catch (ex) {
console.log("Error in firebase.admob.hideBanner: " + ex);
reject(ex);
}
});
};

firebase.admob._getBannerType = function (size) {
if (size == firebase.admob.AD_SIZE.BANNER) {
return com.google.android.gms.ads.AdSize.BANNER;
} else if (size == firebase.admob.AD_SIZE.LARGE_BANNER) {
return com.google.android.gms.ads.AdSize.LARGE_BANNER;
} else if (size == firebase.admob.AD_SIZE.MEDIUM_RECTANGLE) {
return com.google.android.gms.ads.AdSize.MEDIUM_RECTANGLE;
} else if (size == firebase.admob.AD_SIZE.FULL_BANNER) {
return com.google.android.gms.ads.AdSize.FULL_BANNER;
} else if (size == firebase.admob.AD_SIZE.LEADERBOARD) {
return com.google.android.gms.ads.AdSize.LEADERBOARD;
} else if (size == firebase.admob.AD_SIZE.SMART_BANNER) {
return com.google.android.gms.ads.AdSize.SMART_BANNER;
} else {
return null;
}
};

firebase.admob._buildAdRequest = function (settings) {
var builder = new com.google.android.gms.ads.AdRequest.Builder();
if (settings.testing) {
builder.addTestDevice(com.google.android.gms.ads.AdRequest.DEVICE_ID_EMULATOR);
// This will request test ads on the emulator and device by passing this hashed device ID.
var ANDROID_ID = android.provider.Settings.Secure.getString(appModule.android.foregroundActivity.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
var deviceId = firebase.admob._md5(ANDROID_ID);
if (deviceId !== null) {
deviceId = deviceId.toUpperCase();
console.log("Treating this deviceId as testdevice: " + deviceId);
builder.addTestDevice(deviceId);
}
}
var bundle = new android.os.Bundle();
bundle.putInt("nativescript", 1);
var adextras = new com.google.android.gms.ads.mediation.admob.AdMobExtras(bundle);
//builder = builder.addNetworkExtras(adextras);
return builder.build();
};

firebase.admob._md5 = function(input) {
try {
var digest = java.security.MessageDigest.getInstance("MD5");
var bytes = [];
for (var j = 0; j < input.length; ++j) {
bytes.push(input.charCodeAt(j));
}

var s = new java.lang.String(input);
digest.update(s.getBytes());
var messageDigest = digest.digest();
var hexString = "";
for (var i = 0; i < messageDigest.length; i++) {
var h = java.lang.Integer.toHexString(0xFF & messageDigest[i]);
while (h.length < 2)
h = "0" + h;
hexString += h;
}
return hexString;

} catch (noSuchAlgorithmException) {
console.log("error generating md5: " + noSuchAlgorithmException);
return null;
}
};

firebase.getRemoteConfig = function (arg) {
return new Promise(function (resolve, reject) {

Expand Down
Loading

0 comments on commit 1ff57c9

Please sign in to comment.