|
1 | 1 | import { Observable, EventData, Page } from '@nativescript/core'; |
2 | 2 | import { DemoSharedFirebaseAdmob } from '@demo/shared'; |
3 | | -import { } from '@nativescript/firebase-admob'; |
| 3 | +import { Firebase } from '@nativescript/firebase-core'; |
| 4 | +import { AdEventType, InterstitialAd, RewardedInterstitialAd, RewardedAd, BannerAd, BannerAdSize, Admob, AdsConsent, NativeAd, NativeAdLoader, NativeAdView } from '@nativescript/firebase-admob'; |
| 5 | +import { AdChoicesPlacement, NativeAdEventType } from '@nativescript/firebase-admob/nativead/common'; |
4 | 6 |
|
5 | 7 | export function navigatingTo(args: EventData) { |
6 | 8 | const page = <Page>args.object; |
7 | 9 | page.bindingContext = new DemoModel(); |
8 | 10 | } |
9 | 11 |
|
10 | 12 | export class DemoModel extends DemoSharedFirebaseAdmob { |
11 | | - |
| 13 | + get bannerAdUnit() { |
| 14 | + if (global.isAndroid) { |
| 15 | + return 'ca-app-pub-3940256099942544/6300978111'; |
| 16 | + } else { |
| 17 | + return 'ca-app-pub-3940256099942544/2934735716'; |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + admob: Admob; |
| 22 | + constructor() { |
| 23 | + super(); |
| 24 | + //this.interstitial(); |
| 25 | + //this.rewardedInterstitial(); |
| 26 | + //this.rewarded(); |
| 27 | + this.admob = Firebase.admob(); |
| 28 | + // ATTrackingManager.requestTrackingAuthorizationWithCompletionHandler((status) => { |
| 29 | + // console.log('ATTrackingManager', status); |
| 30 | + // }); |
| 31 | + |
| 32 | + const testDevices = []; |
| 33 | + if (global.isIOS) { |
| 34 | + testDevices.push('b70c144077402b91cf8ecc5b93ac7791'); |
| 35 | + } else { |
| 36 | + testDevices.push('EMULATOR'); |
| 37 | + } |
| 38 | + this.admob.setRequestConfiguration({ |
| 39 | + testDevices, |
| 40 | + }); |
| 41 | + } |
| 42 | + |
| 43 | + nativeAdLoaded(event) { |
| 44 | + const view = event.object; |
| 45 | + const loader = new NativeAdLoader('ca-app-pub-3940256099942544/3986624511', null , { |
| 46 | + nativeAdOptions: { |
| 47 | + adChoicesPlacement: AdChoicesPlacement.TOP_RIGHT |
| 48 | + } |
| 49 | + }); |
| 50 | + loader.onAdEvent((event, error, data) => { |
| 51 | + if (event === NativeAdEventType.LOADED) { |
| 52 | + const ad = data as NativeAd; |
| 53 | + const hlv = view.getViewById('headLineView'); |
| 54 | + hlv.text = ad.headline; |
| 55 | + const mv = view.getViewById('mediaView'); |
| 56 | + mv.mediaContent = ad.mediaContent; |
| 57 | + const bv = view.getViewById('bodyView'); |
| 58 | + bv.text = ad.body; |
| 59 | + view.nativeAd = ad; |
| 60 | + console.log('nativead loaded'); |
| 61 | + } else if (event === 'adFailedToLoad') { |
| 62 | + console.log('nativead failed to load', error); |
| 63 | + } |
| 64 | + }); |
| 65 | + loader.load(); |
| 66 | + } |
| 67 | + |
| 68 | + bannerLoaded(event) { |
| 69 | + const bannerAd = event.object as BannerAd; |
| 70 | + |
| 71 | + bannerAd.on('adLoaded', (args) => { |
| 72 | + console.log('bannerad loaded'); |
| 73 | + }); |
| 74 | + bannerAd.on('adFailedToLoad', (args) => { |
| 75 | + console.log('bannerad failed to load', args.error); |
| 76 | + }); |
| 77 | + bannerAd.size = BannerAdSize.createInLineAdaptiveBanner(100); |
| 78 | + bannerAd.load(); |
| 79 | + } |
| 80 | + |
| 81 | + interstitial() { |
| 82 | + const ad = InterstitialAd.createForAdRequest('ca-app-pub-3940256099942544/4411468910'); |
| 83 | + ad.onAdEvent((event, error, data) => { |
| 84 | + if (event === AdEventType.LOADED) { |
| 85 | + console.log('loaded'); |
| 86 | + ad.show(); |
| 87 | + } else if (event === AdEventType.FAILED_TO_LOAD_EVENT) { |
| 88 | + console.error('loading error', error); |
| 89 | + } |
| 90 | + }); |
| 91 | + ad.load(); |
| 92 | + } |
| 93 | + |
| 94 | + rewardedInterstitial() { |
| 95 | + const ad = RewardedInterstitialAd.createForAdRequest('ca-app-pub-3940256099942544/6978759866'); |
| 96 | + ad.onAdEvent((event, error, data) => { |
| 97 | + if (event === AdEventType.LOADED) { |
| 98 | + console.log('loaded'); |
| 99 | + ad.show(); |
| 100 | + } else if (event === AdEventType.FAILED_TO_LOAD_EVENT) { |
| 101 | + console.error('loading error', error); |
| 102 | + } |
| 103 | + }); |
| 104 | + ad.load(); |
| 105 | + } |
| 106 | + |
| 107 | + rewarded() { |
| 108 | + const ad = RewardedAd.createForAdRequest('ca-app-pub-3940256099942544/1712485313'); |
| 109 | + ad.onAdEvent((event, error, data) => { |
| 110 | + if (event === AdEventType.LOADED) { |
| 111 | + console.log('rewarded', 'loaded'); |
| 112 | + ad.show(); |
| 113 | + } else if (event === AdEventType.FAILED_TO_LOAD_EVENT) { |
| 114 | + console.error('loading error', error); |
| 115 | + } |
| 116 | + }); |
| 117 | + ad.load(); |
| 118 | + } |
12 | 119 | } |
0 commit comments