forked from NZME/react-native-ad-manager
-
Notifications
You must be signed in to change notification settings - Fork 2
/
App.js
227 lines (210 loc) · 6.25 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import React, {Component} from 'react';
import {
Button,
Platform,
ScrollView,
StyleSheet,
Text,
View,
RefreshControl,
} from 'react-native';
import {Interstitial, Banner, NativeAdsManager} from 'react-native-ad-manager';
import NativeAdView from './NativeAdView';
const BannerExample = ({style, title, children, ...props}) => (
<View {...props} style={[styles.example, style]}>
<Text style={styles.title}>{title}</Text>
<View>{children}</View>
</View>
);
const bannerWidths = [200, 250, 320];
export default class Example extends Component {
constructor() {
super();
this.state = {
fluidSizeIndex: 0,
adsList: [],
refreshingScrollView: false,
};
}
componentDidMount() {
Interstitial.setTestDevices([Interstitial.simulatorId]);
Interstitial.setAdUnitID('/83069739/jeff');
Interstitial.addEventListener('adLoaded', () =>
console.log('Interstitial adLoaded'),
);
Interstitial.addEventListener('adFailedToLoad', error =>
console.warn(error),
);
Interstitial.addEventListener('adOpened', () =>
console.log('Interstitial => adOpened'),
);
Interstitial.addEventListener('adClosed', () => {
console.log('Interstitial => adClosed');
Interstitial.requestAd().catch(error => console.warn(error));
});
Interstitial.addEventListener('adLeftApplication', () =>
console.log('Interstitial => adLeftApplication'),
);
Interstitial.requestAd().catch(error => console.warn(error));
// const adsList = [{type: 'banner'}];
// this.setState({adsList: adsList});
}
componentWillUnmount() {
Interstitial.removeAllListeners();
}
showInterstitial() {
Interstitial.showAd().catch(error => console.warn(error));
}
onAdLoaded = nativeAd => {
// console.log(nativeAd);
};
showBanner = (adsManager, index) => {
return (
<BannerExample title={`${index}. DFP - Fluid Ad Size`}>
<View
style={[
{backgroundColor: '#f3f', paddingVertical: 10},
{alignItems: 'center', width: '100%'},
]}>
{/*<NativeAdView
targeting={{
customTargeting: { group: 'nzme_user_test' },
categoryExclusions: ['media'],
contentURL: 'nzmetest://',
publisherProvidedID: 'provider_id_nzme',
}}
// style={{ width: '100%'}}
adsManager={adsManager}
// adLayout={'horizontal'}
validAdTypes={['banner']}
adSize="mediumRectangle"
validAdSizes={['mediumRectangle']}
onAdLoaded={this.onAdLoaded}
adUnitID={'/83069739/jeff'}
onAdFailedToLoad={error => {
console.log(error);
}}
/>*/}
<Banner
onAdLoaded={this.onAdLoaded}
adSize="mediumRectangle"
validAdSizes={['mediumRectangle']}
adUnitID={'/83069739/jeff'}
targeting={{
customTargeting: {group: 'nzme_user_test'},
categoryExclusions: ['media'],
contentURL: 'nzmetest://',
publisherProvidedID: 'provider_id_nzme',
}}
/>
</View>
</BannerExample>
);
};
showNative = (adsManager, index) => {
return (
<BannerExample style={{padding: 20}} title={`${index}. DFP - Native ad`}>
<View style={{alignItems: 'center', width: '100%'}}>
<NativeAdView
targeting={{
customTargeting: {group: 'nzme_user_test'},
categoryExclusions: ['media'],
contentURL: 'nzmetest://',
publisherProvidedID: 'provider_id_nzme',
}}
style={{width: '100%'}}
adsManager={adsManager}
// adLayout={'horizontal'}
validAdTypes={['native', 'template']}
customTemplateId="11891103"
onAdLoaded={this.onAdLoaded}
adUnitID={'/83069739/jeff'}
onAdFailedToLoad={error => {
console.log(error);
}}
/>
</View>
</BannerExample>
);
};
addAd = type => {
const {adsList} = this.state;
if (type === 'banner') {
adsList.push({type: 'banner'});
} else {
adsList.push({type: 'native'});
}
this.setState({adsList: adsList});
};
onRefreshScrollView = () => {
const adsList = [{type: 'banner'}];
this.setState({adsList: adsList});
};
render() {
// const adsManager = new NativeAdsManager("/6499/example/native", [AdMobInterstitial.simulatorId]);
const adsManager = new NativeAdsManager('/83069739/jeff', [
Interstitial.simulatorId,
]);
const {adsList, refreshingScrollView} = this.state;
return (
<View style={styles.container}>
<ScrollView
refreshControl={
<RefreshControl
refreshing={refreshingScrollView}
onRefresh={this.onRefreshScrollView}
/>
}>
<BannerExample title="Interstitial">
<Button
title="Show Interstitial and preload next"
onPress={this.showInterstitial}
/>
</BannerExample>
{adsList?.map((curItem, index) => {
if (curItem.type === 'banner') {
return (
<View key={index}>
{this.showBanner(adsManager, index + 1)}
</View>
);
} else {
return (
<View key={index}>
{this.showNative(adsManager, index + 1)}
</View>
);
}
})}
<BannerExample title="Add more adds" style={{paddingBottom: 40}}>
<Button
title="Add Banner"
onPress={() => this.addAd('banner')}
style={styles.button}
/>
<Button
title="Add Native"
onPress={() => this.addAd('native')}
style={styles.button}
/>
</BannerExample>
</ScrollView>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
marginTop: Platform.OS === 'ios' ? 30 : 10,
},
example: {
paddingVertical: 10,
},
title: {
margin: 10,
fontSize: 20,
},
button: {
backgroundColor: '#CC5500',
},
});