Skip to content

fix: admob independence , cloud-messaging notification emitting, database unsubscribing & initial dynamicLink #121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion apps/demo/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Application, Utils } from '@nativescript/core';
import { firebase } from '@nativescript/firebase-core';
import '@nativescript/firebase-admob';
import '@nativescript/firebase-analytics';
import '@nativescript/firebase-auth';
import '@nativescript/firebase-crashlytics';
Expand Down
10 changes: 2 additions & 8 deletions apps/demo/src/plugin-demos/firebase-admob.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { Observable, EventData, Page, View, Label } from '@nativescript/core';
import { DemoSharedFirebaseAdmob } from '@demo/shared';
import { firebase } from '@nativescript/firebase-core';
import '@nativescript/firebase-admob';
import { AdEventType, InterstitialAd, RewardedInterstitialAd, RewardedAd, BannerAd, BannerAdSize, Admob, AdsConsent, NativeAd, NativeAdLoader, NativeAdView } from '@nativescript/firebase-admob';
import { AdChoicesPlacement, NativeAdEventType } from '@nativescript/firebase-admob';
import { AdChoicesPlacement, NativeAdEventType, AdEventType, InterstitialAd, RewardedInterstitialAd, RewardedAd, BannerAd, BannerAdSize, Admob, AdsConsent, NativeAd, NativeAdLoader, NativeAdView } from '@nativescript/firebase-admob';

export function navigatingTo(args: EventData) {
const page = <Page>args.object;
Expand Down Expand Up @@ -33,10 +30,7 @@ export class DemoModel extends DemoSharedFirebaseAdmob {
} else {
testDevices.push('EMULATOR');
}
const admob = firebase().admob();
admob.setRequestConfiguration({
testDevices,
});
Admob.getInstance().requestConfiguration = { testDevices };
}

nativeAdLayoutChanged(event) {
Expand Down
15 changes: 6 additions & 9 deletions packages/firebase-admob/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -506,12 +506,11 @@ Use the argument `tagForChildDirectedTreatment: undefined` or do not set this ta
The following example indicates that you want your content treated as child-directed for purposes of COPPA:

```ts
import { firebase } from '@nativescript/firebase-core'
import '@nativescript/firebase-admob'
import { Admob, RequestConfiguration } from '@nativescript/firebase-admob';
const requestConfiguration: RequestConfiguration = {
tagForChildDirectedTreatment: true
}
firebase().admob().setRequestConfiguration(requestConfiguration)
Admob.getInstance().requestConfiguration = requestConfiguration;
```

### Users under the age of consent
Expand All @@ -527,12 +526,11 @@ Use the argument `tagForUnderAgeOfConsent: false` to indicates that you don't wa
Use the argument `tagForUnderAgeOfConsent: undefined` or do not set this tag to indicate that you have not specified whether the ad request should receive treatment for users in the European Economic Area (EEA) under the age of consent. The following example indicates that you want TFUA included in your ad request:

```ts
import { firebase } from '@nativescript/firebase-core'
import '@nativescript/firebase-admob'
import { Admob, RequestConfiguration } from '@nativescript/firebase-admob';
const requestConfiguration: RequestConfiguration = {
tagForUnderAgeOfConsent: true
}
firebase().admob().setRequestConfiguration(requestConfiguration)
Admob.getInstance().requestConfiguration = requestConfiguration;
```

The tags to enable the Child-directed setting and `tagForUnderAgeOfConsent` should not both simultaneously be set to true. If they are, the child-directed setting takes precedence.
Expand All @@ -551,12 +549,11 @@ AdMob ads returned for these requests have a content rating at or below that lev
The following code configures a `RequestConfiguration` object to specify that ad content returned should correspond to a digital content label designation no higher than G:

```ts
import { firebase } from '@nativescript/firebase-core'
import { MaxAdContentRating } from '@nativescript/firebase-admob'
import { Admob, MaxAdContentRating, RequestConfiguration } from '@nativescript/firebase-admob';
const requestConfiguration: RequestConfiguration = {
maxAdContentRating: MaxAdContentRating.G
}
firebase().admob().setRequestConfiguration(requestConfiguration)
Admob.getInstance().requestConfiguration = requestConfiguration;
```

## License
Expand Down
54 changes: 35 additions & 19 deletions packages/firebase-admob/index.android.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Application, Utils } from '@nativescript/core';
import lazy from '@nativescript/core/utils/lazy';
import { firebase, FirebaseApp, FirebaseError } from '@nativescript/firebase-core';
import { IAdmob, AdEventListener, RequestConfiguration, AdShowOptions, IInterstitialAd, RequestOptions, IRewardedAd, IRewardedInterstitialAd, IRewardedItem, ServerSideVerificationOptions, AdapterStatus } from '.';
import { AdEventType, BannerAdBase, RewardedAdEventType, MaxAdContentRating, unitIdProperty, BannerAdSizeBase, sizeProperty } from './common';

Expand All @@ -9,11 +8,30 @@ export { MaxAdContentRating, AdEventType };
export * from './adsconsent';
export * from './nativead';

export class AdmobError extends Error {
#native: java.lang.Exception;
static fromNative(native: java.lang.Exception, message?: string) {
const error = new AdmobError(message || native?.getMessage?.());
error.#native = native;
return error;
}

get native() {
return this.#native;
}

intoNative() {
if (!this.#native) {
return new java.lang.Exception(this.message);
}
return this.#native;
}
}

let defaultAdmob: Admob;

const fb = firebase();
if (!fb.admob) {
Object.defineProperty(fb, 'admob', {
if (!global.__admob) {
Object.defineProperty(global, '__admob', {
value: () => {
if (!defaultAdmob) {
defaultAdmob = new Admob();
Expand Down Expand Up @@ -52,7 +70,7 @@ class AdListener extends com.google.android.gms.ads.AdListener {
this._owner?.get?.().notify({
eventName: BannerAd.onAdFailedToLoadEvent,
object: this._owner?.get?.(),
error: FirebaseError.fromNative(error),
error: AdmobError.fromNative(error as any),
});
}

Expand Down Expand Up @@ -179,7 +197,7 @@ export class InterstitialAd implements IInterstitialAd {
owner?._setLoaded(false);
break;
case AdEventType.FAILED_TO_SHOW_FULL_SCREEN_CONTENT:
owner?._onAdEvent(AdEventType.FAILED_TO_SHOW_FULL_SCREEN_CONTENT, FirebaseError.fromNative(dataOrError), owner);
owner?._onAdEvent(AdEventType.FAILED_TO_SHOW_FULL_SCREEN_CONTENT, AdmobError.fromNative(dataOrError), owner);
break;
case AdEventType.IMPRESSION:
owner?._onAdEvent(AdEventType.IMPRESSION, null, owner);
Expand All @@ -188,7 +206,7 @@ export class InterstitialAd implements IInterstitialAd {
owner?._onAdEvent(AdEventType.OPENED, null, owner);
break;
case AdEventType.FAILED_TO_LOAD_EVENT:
owner?._onAdEvent(AdEventType.FAILED_TO_LOAD_EVENT, FirebaseError.fromNative(dataOrError), owner);
owner?._onAdEvent(AdEventType.FAILED_TO_LOAD_EVENT, AdmobError.fromNative(dataOrError), owner);
break;
}
},
Expand Down Expand Up @@ -270,7 +288,7 @@ export class RewardedInterstitialAd implements IRewardedInterstitialAd {
owner?._setLoaded(false);
break;
case AdEventType.FAILED_TO_SHOW_FULL_SCREEN_CONTENT:
owner?._onAdEvent(AdEventType.FAILED_TO_SHOW_FULL_SCREEN_CONTENT, FirebaseError.fromNative(dataOrError), owner);
owner?._onAdEvent(AdEventType.FAILED_TO_SHOW_FULL_SCREEN_CONTENT, AdmobError.fromNative(dataOrError), owner);
break;
case AdEventType.IMPRESSION:
owner?._onAdEvent(AdEventType.IMPRESSION, null, owner);
Expand All @@ -279,7 +297,7 @@ export class RewardedInterstitialAd implements IRewardedInterstitialAd {
owner?._onAdEvent(AdEventType.OPENED, null, owner);
break;
case AdEventType.FAILED_TO_LOAD_EVENT:
owner?._onAdEvent(AdEventType.FAILED_TO_LOAD_EVENT, FirebaseError.fromNative(dataOrError), owner);
owner?._onAdEvent(AdEventType.FAILED_TO_LOAD_EVENT, AdmobError.fromNative(dataOrError), owner);
break;
}
},
Expand Down Expand Up @@ -386,7 +404,7 @@ export class RewardedAd implements IRewardedAd {
owner?._setLoaded(false);
break;
case AdEventType.FAILED_TO_SHOW_FULL_SCREEN_CONTENT:
owner?._onAdEvent(AdEventType.FAILED_TO_SHOW_FULL_SCREEN_CONTENT, FirebaseError.fromNative(dataOrError), owner);
owner?._onAdEvent(AdEventType.FAILED_TO_SHOW_FULL_SCREEN_CONTENT, AdmobError.fromNative(dataOrError), owner);
break;
case AdEventType.IMPRESSION:
owner?._onAdEvent(AdEventType.IMPRESSION, null, owner);
Expand All @@ -395,7 +413,7 @@ export class RewardedAd implements IRewardedAd {
owner?._onAdEvent(AdEventType.OPENED, null, owner);
break;
case AdEventType.FAILED_TO_LOAD_EVENT:
owner?._onAdEvent(AdEventType.FAILED_TO_LOAD_EVENT, FirebaseError.fromNative(dataOrError), owner);
owner?._onAdEvent(AdEventType.FAILED_TO_LOAD_EVENT, AdmobError.fromNative(dataOrError), owner);
break;
}
},
Expand Down Expand Up @@ -627,8 +645,6 @@ export class BannerAd extends BannerAdBase {
}

export class Admob implements IAdmob {
#app: FirebaseApp;

constructor() {
if (defaultAdmob) {
return defaultAdmob;
Expand All @@ -653,6 +669,10 @@ export class Admob implements IAdmob {
});
}

static getInstance(): Admob {
return new Admob();
}

set requestConfiguration(requestConfiguration: RequestConfiguration) {
try {
const parsedConfiguration: any = { ...requestConfiguration };
Expand Down Expand Up @@ -736,11 +756,7 @@ export class Admob implements IAdmob {
return this.requestConfiguration;
}

get app(): FirebaseApp {
if (!this.#app) {
// @ts-ignore
this.#app = FirebaseApp.fromNative(com.google.firebase.FirebaseApp.getInstance());
}
return this.#app;
get app() {
return global?.__defaultFirebaseApp;
}
}
13 changes: 3 additions & 10 deletions packages/firebase-admob/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Application } from '@nativescript/core';

import { FirebaseApp } from '@nativescript/firebase-core';
import { IAdmob, IInterstitialAd, IRewardedAd, BannerAdBase, IRewardedInterstitialAd, RewardedAdEventType, MaxAdContentRating, ServerSideVerificationOptions } from './common';

export { MaxAdContentRating, RewardedAdEventType };
Expand Down Expand Up @@ -214,10 +213,12 @@ export declare class BannerAdSize extends BannerAdSizeBase {
}

export declare class Admob implements IAdmob {
readonly app: FirebaseApp;
readonly app: any;

static init(): Promise<{ [key: string]: AdapterStatus }>;

static getInstance(): Admob;

requestConfiguration: RequestConfiguration;

/**
Expand All @@ -231,10 +232,6 @@ export declare class Admob implements IAdmob {
getRequestConfiguration(requestConfiguration: RequestConfiguration);
}

declare module '@nativescript/firebase-core' {
export interface Firebase extends FirebaseAdmob {}
}

export enum AdapterStatusState {
NOT_READY,
READY,
Expand All @@ -245,7 +242,3 @@ export interface AdapterStatus {
latency: number;
initializationState: AdapterStatusState;
}

export interface FirebaseAdmob {
static admob(): Admob;
}
Loading