UNPKG

4.02 kBTypeScriptView Raw
1import { requireNativeViewManager } from '@unimodules/core';
2import * as React from 'react';
3import { View } from 'react-native';
4
5type PropsType = React.ComponentProps<typeof View> & {
6 /**
7 * AdMob iOS library banner size constants
8 * (https://developers.google.com/admob/ios/banner)
9 * banner (320x50, Standard Banner for Phones and Tablets)
10 * largeBanner (320x100, Large Banner for Phones and Tablets)
11 * mediumRectangle (300x250, IAB Medium Rectangle for Phones and Tablets)
12 * fullBanner (468x60, IAB Full-Size Banner for Tablets)
13 * leaderboard (728x90, IAB Leaderboard for Tablets)
14 * smartBannerPortrait (Screen width x 32|50|90, Smart Banner for Phones and Tablets)
15 * smartBannerLandscape (Screen width x 32|50|90, Smart Banner for Phones and Tablets)
16 *
17 * banner is default
18 */
19 bannerSize:
20 | 'banner'
21 | 'largeBanner'
22 | 'mediumRectangle'
23 | 'fullBanner'
24 | 'leaderboard'
25 | 'smartBannerPortrait'
26 | 'smartBannerLandscape';
27 /**
28 * AdMob ad unit ID
29 */
30 adUnitID?: string;
31
32 /**
33 * Additional request params added to underlying request for the ad.
34 */
35 additionalRequestParams?: { [key: string]: string };
36
37 /**
38 * Whether the SDK should serve personalized ads (use only with user's consent). If this value is
39 * `false` or `undefined`, this sets the `npa` key of `additionalRequestParams` to `'1'` following
40 * https://developers.google.com/admob/ios/eu-consent#forward_consent_to_the_google_mobile_ads_sdk
41 * and
42 * https://developers.google.com/admob/android/eu-consent#forward_consent_to_the_google_mobile_ads_sdk.
43 */
44 servePersonalizedAds?: boolean;
45
46 /**
47 * AdMob iOS library events
48 */
49 onAdViewDidReceiveAd?: () => void;
50 onDidFailToReceiveAdWithError?: (string) => void;
51 onAdViewWillPresentScreen?: () => void;
52 onAdViewWillDismissScreen?: () => void;
53 onAdViewDidDismissScreen?: () => void;
54 onAdViewWillLeaveApplication?: () => void;
55};
56
57type StateType = {
58 style: { width?: number; height?: number };
59};
60
61let _hasWarnedAboutTestDeviceID = false;
62
63export default class AdMobBanner extends React.Component<PropsType, StateType> {
64 static defaultProps = { bannerSize: 'smartBannerPortrait' };
65
66 state = { style: {} };
67
68 _handleSizeChange = ({ nativeEvent }: { nativeEvent: { width: number; height: number } }) => {
69 const { height, width } = nativeEvent;
70 this.setState({ style: { width, height } });
71 };
72
73 _handleDidFailToReceiveAdWithError = ({ nativeEvent }: { nativeEvent: { error: string } }) =>
74 this.props.onDidFailToReceiveAdWithError &&
75 this.props.onDidFailToReceiveAdWithError(nativeEvent.error);
76
77 render() {
78 const additionalRequestParams: { [key: string]: string } = {
79 ...this.props.additionalRequestParams,
80 };
81 if (!this.props.servePersonalizedAds) {
82 additionalRequestParams.npa = '1';
83 }
84 if ((this.props as any).testDeviceID && !_hasWarnedAboutTestDeviceID) {
85 console.warn(
86 'The `testDeviceID` prop of AdMobBanner is deprecated. Test device IDs are now set globally. Use AdMob.setTestDeviceIDAsync instead.'
87 );
88 _hasWarnedAboutTestDeviceID = true;
89 }
90 return (
91 <View style={this.props.style}>
92 <ExpoBannerView
93 style={this.state.style}
94 adUnitID={this.props.adUnitID}
95 bannerSize={this.props.bannerSize}
96 onSizeChange={this._handleSizeChange}
97 additionalRequestParams={additionalRequestParams}
98 onAdViewDidReceiveAd={this.props.onAdViewDidReceiveAd}
99 onDidFailToReceiveAdWithError={this._handleDidFailToReceiveAdWithError}
100 onAdViewWillPresentScreen={this.props.onAdViewWillPresentScreen}
101 onAdViewWillDismissScreen={this.props.onAdViewWillDismissScreen}
102 onAdViewDidDismissScreen={this.props.onAdViewDidDismissScreen}
103 onAdViewWillLeaveApplication={this.props.onAdViewWillLeaveApplication}
104 />
105 </View>
106 );
107 }
108}
109
110const ExpoBannerView = requireNativeViewManager('ExpoAdsAdMobBannerView');