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

Commit 63270fe

Browse files
EddyVerbruggeneddyverbruggen
authored andcommitted
#19 Conflict with nativescript-admob
#55 Support for AdMob #85 Google Service conflits on using nativescript-admob with this plugin #284 nativescript-plugin-firebase and nativescript admob conflict
1 parent 104059c commit 63270fe

File tree

9 files changed

+1073
-586
lines changed

9 files changed

+1073
-586
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ For readability the supported features have been moved to their own README's:
1414
* [Storage](docs/STORAGE.md)
1515
* [Crash Reporting](docs/CRASHREPORTING.md)
1616
* [Analytics](docs/ANALYTICS.md)
17+
* [AdMob](docs/ADMOB.md)
1718

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

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

49+
__NativeScript 2.5.0 is no longer the latest release, so you might as well update before adding this plugin to avoid this issue.__
50+
4851
```
4952
npm run setup
5053
```

docs/ADMOB.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<img src="images/firebase-logo.png" width="116px" height="32px" alt="Firebase"/>
2+
3+
<img src="images/features/admob.png" width="296px" height="124px" alt="admob"/>
4+
5+
## Enabling AdMob
6+
Since plugin version 3... you can use Firebase _AdMob_ features.
7+
8+
_AdMob_ lets you ..
9+
10+
WORK IN PROGRESS.. updating right now (Feb 17, 2017)

firebase-common.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,42 @@
11
var firebase = {};
22
firebase.analytics = {};
3+
firebase.admob = {};
4+
firebase.admob.AD_SIZE = {
5+
SMART_BANNER: "SMART",
6+
LARGE_BANNER: "LARGE",
7+
BANNER: "BANNER",
8+
MEDIUM_RECTANGLE: "MEDIUM",
9+
FULL_BANNER: "FULL",
10+
LEADERBOARD: "LEADERBOARD",
11+
SKYSCRAPER: "SKYSCRAPER",
12+
FLUID: "FLUID"
13+
};
14+
firebase.admob.defaults = {
15+
margins: {
16+
top: -1,
17+
bottom: -1
18+
},
19+
testing: false,
20+
size: firebase.admob.AD_SIZE.SMART_BANNER
21+
};
22+
23+
firebase.merge = function merge(obj1, obj2){ // Our merge function
24+
var result = {}; // return result
25+
for(var i in obj1){ // for every property in obj1
26+
if((i in obj2) && (typeof obj1[i] === "object") && (i !== null)){
27+
result[i] = merge(obj1[i],obj2[i]); // if it's an object, merge
28+
}else{
29+
result[i] = obj1[i]; // add it to result
30+
}
31+
}
32+
for(i in obj2){ // add the remaining properties from object 2
33+
if(i in result){ //conflict
34+
continue;
35+
}
36+
result[i] = obj2[i];
37+
}
38+
return result;
39+
};
340

441
firebase.LoginType = {
542
ANONYMOUS: "anonymous",

firebase.android.js

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var appModule = require("application");
22
var utils = require("utils/utils");
3+
var frame = require("ui/frame");
34
var fs = require("file-system");
45
var firebase = require("./firebase-common");
56

@@ -369,6 +370,189 @@ firebase.analytics.setUserProperty = function (arg) {
369370
});
370371
};
371372

373+
//see https://firebase.google.com/docs/admob/android/quick-start
374+
firebase.admob.showBanner = function (arg) {
375+
return new Promise(function (resolve, reject) {
376+
try {
377+
var settings = firebase.merge(arg, firebase.admob.defaults);
378+
379+
// this should also be possible in init
380+
com.google.android.gms.ads.MobileAds.initialize(
381+
appModule.android.context,
382+
settings.androidBannerId); // TODO not sure its bound to packagename.. this is from the admob-demo
383+
384+
// always close a previously opened banner
385+
if (firebase.admob.adView !== null && firebase.admob.adView !== undefined) {
386+
var parent = firebase.admob.adView.getParent();
387+
if (parent !== null) {
388+
parent.removeView(firebase.admob.adView);
389+
}
390+
}
391+
392+
firebase.admob.adView = new com.google.android.gms.ads.AdView(appModule.android.foregroundActivity);
393+
firebase.admob.adView.setAdUnitId(settings.androidBannerId);
394+
var bannerType = firebase.admob._getBannerType(settings.size);
395+
firebase.admob.adView.setAdSize(bannerType);
396+
console.log("----- bannerType: " + bannerType);
397+
// TODO consider implementing events
398+
//firebase.admob.adView.setAdListener(new com.google.android.gms.ads.BannerListener());
399+
400+
var ad = firebase.admob._buildAdRequest(settings);
401+
firebase.admob.adView.loadAd(ad);
402+
403+
var density = utils.layout.getDisplayDensity(),
404+
top = settings.margins.top * density,
405+
bottom = settings.margins.bottom * density;
406+
407+
var relativeLayoutParams = new android.widget.RelativeLayout.LayoutParams(
408+
android.widget.RelativeLayout.LayoutParams.MATCH_PARENT,
409+
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
410+
411+
if (bottom > -1) {
412+
relativeLayoutParams.bottomMargin = bottom;
413+
relativeLayoutParams.addRule(android.widget.RelativeLayout.ALIGN_PARENT_BOTTOM);
414+
} else {
415+
if (top > -1) {
416+
relativeLayoutParams.topMargin = top;
417+
}
418+
relativeLayoutParams.addRule(android.widget.RelativeLayout.ALIGN_PARENT_TOP);
419+
}
420+
421+
var adViewLayout = new android.widget.RelativeLayout(appModule.android.foregroundActivity);
422+
adViewLayout.addView(firebase.admob.adView, relativeLayoutParams);
423+
424+
var relativeLayoutParamsOuter = new android.widget.RelativeLayout.LayoutParams(
425+
android.widget.RelativeLayout.LayoutParams.MATCH_PARENT,
426+
android.widget.RelativeLayout.LayoutParams.MATCH_PARENT);
427+
428+
// wrapping it in a timeout makes sure that when this function is loaded from
429+
// a Page.loaded event 'frame.topmost()' doesn't resolve to 'undefined'
430+
setTimeout(function() {
431+
frame.topmost().currentPage.android.getParent().addView(adViewLayout, relativeLayoutParamsOuter);
432+
}, 0);
433+
434+
resolve();
435+
} catch (ex) {
436+
console.log("Error in firebase.admob.showBanner: " + ex);
437+
reject(ex);
438+
}
439+
});
440+
};
441+
442+
firebase.admob.showInterstitial = function (arg) {
443+
return new Promise(function (resolve, reject) {
444+
try {
445+
var settings = firebase.merge(arg, firebase.admob.defaults);
446+
firebase.admob.interstitialView = new com.google.android.gms.ads.InterstitialAd(appModule.android.foregroundActivity);
447+
firebase.admob.interstitialView.setAdUnitId(settings.androidInterstitialId);
448+
449+
// Interstitial ads must be loaded before they can be shown, so adding a listener
450+
var InterstitialAdListener = com.google.android.gms.ads.AdListener.extend({
451+
onAdLoaded: function () {
452+
firebase.admob.interstitialView.show();
453+
resolve();
454+
},
455+
onAdFailedToLoad: function (errorCode) {
456+
reject(errorCode);
457+
},
458+
onAdClosed: function() {
459+
firebase.admob.interstitialView.setAdListener(null);
460+
firebase.admob.interstitialView = null;
461+
}
462+
});
463+
firebase.admob.interstitialView.setAdListener(new InterstitialAdListener());
464+
465+
var ad = firebase.admob._buildAdRequest(settings);
466+
firebase.admob.interstitialView.loadAd(ad);
467+
} catch (ex) {
468+
console.log("Error in firebase.admob.showInterstitial: " + ex);
469+
reject(ex);
470+
}
471+
});
472+
};
473+
474+
firebase.admob.hideBanner = function (arg) {
475+
return new Promise(function (resolve, reject) {
476+
try {
477+
if (firebase.admob.adView !== null) {
478+
var parent = firebase.admob.adView.getParent();
479+
if (parent !== null) {
480+
parent.removeView(firebase.admob.adView);
481+
}
482+
firebase.admob.adView = null;
483+
}
484+
resolve();
485+
} catch (ex) {
486+
console.log("Error in firebase.admob.hideBanner: " + ex);
487+
reject(ex);
488+
}
489+
});
490+
};
491+
492+
firebase.admob._getBannerType = function (size) {
493+
if (size == firebase.admob.AD_SIZE.BANNER) {
494+
return com.google.android.gms.ads.AdSize.BANNER;
495+
} else if (size == firebase.admob.AD_SIZE.LARGE_BANNER) {
496+
return com.google.android.gms.ads.AdSize.LARGE_BANNER;
497+
} else if (size == firebase.admob.AD_SIZE.MEDIUM_RECTANGLE) {
498+
return com.google.android.gms.ads.AdSize.MEDIUM_RECTANGLE;
499+
} else if (size == firebase.admob.AD_SIZE.FULL_BANNER) {
500+
return com.google.android.gms.ads.AdSize.FULL_BANNER;
501+
} else if (size == firebase.admob.AD_SIZE.LEADERBOARD) {
502+
return com.google.android.gms.ads.AdSize.LEADERBOARD;
503+
} else if (size == firebase.admob.AD_SIZE.SMART_BANNER) {
504+
return com.google.android.gms.ads.AdSize.SMART_BANNER;
505+
} else {
506+
return null;
507+
}
508+
};
509+
510+
firebase.admob._buildAdRequest = function (settings) {
511+
var builder = new com.google.android.gms.ads.AdRequest.Builder();
512+
if (settings.testing) {
513+
builder.addTestDevice(com.google.android.gms.ads.AdRequest.DEVICE_ID_EMULATOR);
514+
// This will request test ads on the emulator and device by passing this hashed device ID.
515+
var ANDROID_ID = android.provider.Settings.Secure.getString(appModule.android.foregroundActivity.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
516+
var deviceId = firebase.admob._md5(ANDROID_ID);
517+
if (deviceId !== null) {
518+
deviceId = deviceId.toUpperCase();
519+
console.log("Treating this deviceId as testdevice: " + deviceId);
520+
builder.addTestDevice(deviceId);
521+
}
522+
}
523+
var bundle = new android.os.Bundle();
524+
bundle.putInt("nativescript", 1);
525+
var adextras = new com.google.android.gms.ads.mediation.admob.AdMobExtras(bundle);
526+
//builder = builder.addNetworkExtras(adextras);
527+
return builder.build();
528+
};
529+
530+
firebase.admob._md5 = function(input) {
531+
try {
532+
var digest = java.security.MessageDigest.getInstance("MD5");
533+
var bytes = [];
534+
for (var j = 0; j < input.length; ++j) {
535+
bytes.push(input.charCodeAt(j));
536+
}
537+
538+
var s = new java.lang.String(input);
539+
digest.update(s.getBytes());
540+
var messageDigest = digest.digest();
541+
var hexString = "";
542+
for (var i = 0; i < messageDigest.length; i++) {
543+
var h = java.lang.Integer.toHexString(0xFF & messageDigest[i]);
544+
while (h.length < 2)
545+
h = "0" + h;
546+
hexString += h;
547+
}
548+
return hexString;
549+
550+
} catch (noSuchAlgorithmException) {
551+
console.log("error generating md5: " + noSuchAlgorithmException);
552+
return null;
553+
}
554+
};
555+
372556
firebase.getRemoteConfig = function (arg) {
373557
return new Promise(function (resolve, reject) {
374558

0 commit comments

Comments
 (0)