|
| 1 | +import React, { Component } from 'react'; |
| 2 | +import { |
| 3 | + Button, |
| 4 | + Platform, |
| 5 | + ScrollView, |
| 6 | + StyleSheet, |
| 7 | + Text, |
| 8 | + View, |
| 9 | +} from 'react-native'; |
| 10 | +import { |
| 11 | + AdMobBanner, |
| 12 | + AdMobInterstitial, |
| 13 | + AdMobRewarded, |
| 14 | + PublisherBanner, |
| 15 | +} from 'react-native-admob'; |
| 16 | + |
| 17 | +const BannerExample = ({ style, title, children, ...props }) => ( |
| 18 | + <View {...props} style={[styles.example, style]}> |
| 19 | + <Text style={styles.title}>{title}</Text> |
| 20 | + <View>{children}</View> |
| 21 | + </View> |
| 22 | +); |
| 23 | + |
| 24 | +const bannerWidths = [200, 250, 320]; |
| 25 | + |
| 26 | +export default class Example extends Component { |
| 27 | + constructor() { |
| 28 | + super(); |
| 29 | + this.state = { |
| 30 | + fluidSizeIndex: 0, |
| 31 | + }; |
| 32 | + } |
| 33 | + |
| 34 | + componentDidMount() { |
| 35 | + AdMobRewarded.setTestDevices([AdMobRewarded.simulatorId]); |
| 36 | + AdMobRewarded.setAdUnitID('ca-app-pub-3940256099942544/5224354917'); |
| 37 | + |
| 38 | + AdMobRewarded.addEventListener('rewarded', reward => |
| 39 | + console.log('AdMobRewarded => rewarded', reward), |
| 40 | + ); |
| 41 | + AdMobRewarded.addEventListener('adLoaded', () => |
| 42 | + console.log('AdMobRewarded => adLoaded'), |
| 43 | + ); |
| 44 | + AdMobRewarded.addEventListener('adFailedToLoad', error => |
| 45 | + console.warn(error), |
| 46 | + ); |
| 47 | + AdMobRewarded.addEventListener('adOpened', () => |
| 48 | + console.log('AdMobRewarded => adOpened'), |
| 49 | + ); |
| 50 | + AdMobRewarded.addEventListener('videoStarted', () => |
| 51 | + console.log('AdMobRewarded => videoStarted'), |
| 52 | + ); |
| 53 | + AdMobRewarded.addEventListener('adClosed', () => { |
| 54 | + console.log('AdMobRewarded => adClosed'); |
| 55 | + AdMobRewarded.requestAd().catch(error => console.warn(error)); |
| 56 | + }); |
| 57 | + AdMobRewarded.addEventListener('adLeftApplication', () => |
| 58 | + console.log('AdMobRewarded => adLeftApplication'), |
| 59 | + ); |
| 60 | + |
| 61 | + AdMobRewarded.requestAd().catch(error => console.warn(error)); |
| 62 | + |
| 63 | + AdMobInterstitial.setTestDevices([AdMobInterstitial.simulatorId]); |
| 64 | + AdMobInterstitial.setAdUnitID('ca-app-pub-3940256099942544/1033173712'); |
| 65 | + |
| 66 | + AdMobInterstitial.addEventListener('adLoaded', () => |
| 67 | + console.log('AdMobInterstitial adLoaded'), |
| 68 | + ); |
| 69 | + AdMobInterstitial.addEventListener('adFailedToLoad', error => |
| 70 | + console.warn(error), |
| 71 | + ); |
| 72 | + AdMobInterstitial.addEventListener('adOpened', () => |
| 73 | + console.log('AdMobInterstitial => adOpened'), |
| 74 | + ); |
| 75 | + AdMobInterstitial.addEventListener('adClosed', () => { |
| 76 | + console.log('AdMobInterstitial => adClosed'); |
| 77 | + AdMobInterstitial.requestAd().catch(error => console.warn(error)); |
| 78 | + }); |
| 79 | + AdMobInterstitial.addEventListener('adLeftApplication', () => |
| 80 | + console.log('AdMobInterstitial => adLeftApplication'), |
| 81 | + ); |
| 82 | + |
| 83 | + AdMobInterstitial.requestAd().catch(error => console.warn(error)); |
| 84 | + } |
| 85 | + |
| 86 | + componentWillUnmount() { |
| 87 | + AdMobRewarded.removeAllListeners(); |
| 88 | + AdMobInterstitial.removeAllListeners(); |
| 89 | + } |
| 90 | + |
| 91 | + showRewarded() { |
| 92 | + AdMobRewarded.showAd().catch(error => console.warn(error)); |
| 93 | + } |
| 94 | + |
| 95 | + showInterstitial() { |
| 96 | + AdMobInterstitial.showAd().catch(error => console.warn(error)); |
| 97 | + } |
| 98 | + |
| 99 | + render() { |
| 100 | + return ( |
| 101 | + <View style={styles.container}> |
| 102 | + <ScrollView> |
| 103 | + <BannerExample title="AdMob - Basic"> |
| 104 | + <AdMobBanner |
| 105 | + adSize="banner" |
| 106 | + adUnitID="ca-app-pub-3940256099942544/2934735716" |
| 107 | + ref={el => (this._basicExample = el)} |
| 108 | + /> |
| 109 | + <Button |
| 110 | + title="Reload" |
| 111 | + onPress={() => this._basicExample.loadBanner()} |
| 112 | + /> |
| 113 | + </BannerExample> |
| 114 | + <BannerExample title="Smart Banner"> |
| 115 | + <AdMobBanner |
| 116 | + adSize="smartBannerPortrait" |
| 117 | + adUnitID="ca-app-pub-3940256099942544/2934735716" |
| 118 | + ref={el => (this._smartBannerExample = el)} |
| 119 | + /> |
| 120 | + <Button |
| 121 | + title="Reload" |
| 122 | + onPress={() => this._smartBannerExample.loadBanner()} |
| 123 | + /> |
| 124 | + </BannerExample> |
| 125 | + <BannerExample title="Rewarded"> |
| 126 | + <Button |
| 127 | + title="Show Rewarded Video and preload next" |
| 128 | + onPress={this.showRewarded} |
| 129 | + /> |
| 130 | + </BannerExample> |
| 131 | + <BannerExample title="Interstitial"> |
| 132 | + <Button |
| 133 | + title="Show Interstitial and preload next" |
| 134 | + onPress={this.showInterstitial} |
| 135 | + /> |
| 136 | + </BannerExample> |
| 137 | + <BannerExample title="DFP - Multiple Ad Sizes"> |
| 138 | + <PublisherBanner |
| 139 | + adSize="banner" |
| 140 | + validAdSizes={['banner', 'largeBanner', 'mediumRectangle']} |
| 141 | + adUnitID="/6499/example/APIDemo/AdSizes" |
| 142 | + ref={el => (this._adSizesExample = el)} |
| 143 | + /> |
| 144 | + <Button |
| 145 | + title="Reload" |
| 146 | + onPress={() => this._adSizesExample.loadBanner()} |
| 147 | + /> |
| 148 | + </BannerExample> |
| 149 | + <BannerExample |
| 150 | + title="DFP - App Events" |
| 151 | + style={this.state.appEventsExampleStyle}> |
| 152 | + <PublisherBanner |
| 153 | + style={{ height: 50 }} |
| 154 | + adUnitID="/6499/example/APIDemo/AppEvents" |
| 155 | + onAdFailedToLoad={error => { |
| 156 | + console.warn(error); |
| 157 | + }} |
| 158 | + onAppEvent={event => { |
| 159 | + if (event.name === 'color') { |
| 160 | + this.setState({ |
| 161 | + appEventsExampleStyle: { backgroundColor: event.info }, |
| 162 | + }); |
| 163 | + } |
| 164 | + }} |
| 165 | + ref={el => (this._appEventsExample = el)} |
| 166 | + /> |
| 167 | + <Button |
| 168 | + title="Reload" |
| 169 | + onPress={() => this._appEventsExample.loadBanner()} |
| 170 | + style={styles.button} |
| 171 | + /> |
| 172 | + </BannerExample> |
| 173 | + <BannerExample title="DFP - Fluid Ad Size"> |
| 174 | + <View |
| 175 | + style={[ |
| 176 | + { backgroundColor: '#f3f', paddingVertical: 10 }, |
| 177 | + this.state.fluidAdSizeExampleStyle, |
| 178 | + ]}> |
| 179 | + <PublisherBanner |
| 180 | + adSize="fluid" |
| 181 | + adUnitID="/6499/example/APIDemo/Fluid" |
| 182 | + ref={el => (this._appFluidAdSizeExample = el)} |
| 183 | + style={{ flex: 1 }} |
| 184 | + /> |
| 185 | + </View> |
| 186 | + <Button |
| 187 | + title="Change Banner Width" |
| 188 | + onPress={() => |
| 189 | + this.setState(prevState => ({ |
| 190 | + fluidSizeIndex: prevState.fluidSizeIndex + 1, |
| 191 | + fluidAdSizeExampleStyle: { |
| 192 | + width: |
| 193 | + bannerWidths[ |
| 194 | + prevState.fluidSizeIndex % bannerWidths.length |
| 195 | + ], |
| 196 | + }, |
| 197 | + })) |
| 198 | + } |
| 199 | + style={styles.button} |
| 200 | + /> |
| 201 | + <Button |
| 202 | + title="Reload" |
| 203 | + onPress={() => this._appFluidAdSizeExample.loadBanner()} |
| 204 | + style={styles.button} |
| 205 | + /> |
| 206 | + </BannerExample> |
| 207 | + </ScrollView> |
| 208 | + </View> |
| 209 | + ); |
| 210 | + } |
| 211 | +} |
| 212 | + |
| 213 | +const styles = StyleSheet.create({ |
| 214 | + container: { |
| 215 | + marginTop: Platform.OS === 'ios' ? 30 : 10, |
| 216 | + }, |
| 217 | + example: { |
| 218 | + paddingVertical: 10, |
| 219 | + }, |
| 220 | + title: { |
| 221 | + margin: 10, |
| 222 | + fontSize: 20, |
| 223 | + }, |
| 224 | +}); |
0 commit comments