Skip to content

Commit b160473

Browse files
authored
Merge pull request #479 from sbugert/ci
add GitHub workflow that sets up a React Native project with the library
2 parents 2d1432a + 9adacb3 commit b160473

File tree

5 files changed

+311
-229
lines changed

5 files changed

+311
-229
lines changed

.github/workflows/integrate.yml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
name: Integrate React Native AdMob
2+
on:
3+
schedule:
4+
- cron: 0 12 * * 1
5+
pull_request:
6+
branches:
7+
- master
8+
jobs:
9+
integrate:
10+
name: Create React Native App and integrate React Native Admob
11+
runs-on: macOS-10.14
12+
steps:
13+
- uses: actions/checkout@v1
14+
- uses: actions/setup-node@v1
15+
with:
16+
node-version: '10.x'
17+
- name: Install xmlstarlet
18+
run: brew install xmlstarlet
19+
- name: Install xcpretty
20+
run: gem install xcpretty
21+
- name: Configure Xcode
22+
run: sudo xcode-select --switch /Applications/Xcode_11.app
23+
- name: Install React Native CLI
24+
run: sudo npm install -g react-native-cli
25+
- name: Initialize React Native application
26+
run: react-native init ReactNativeAdMobExample
27+
- name: Add react-native-admob
28+
working-directory: ./ReactNativeAdMobExample
29+
run: yarn add react-native-admob@next
30+
- name: Configure Android Project
31+
working-directory: ./ReactNativeAdMobExample
32+
run: |
33+
xml ed -L \
34+
-s /manifest/application -t elem -n meta-data \
35+
-i /manifest/application/meta-data -t attr -n android:name -v "com.google.android.gms.ads.APPLICATION_ID" \
36+
-i /manifest/application/meta-data -t attr -n android:value -v "ca-app-pub-3940256099942544~1458002511" \
37+
./android/app/src/main/AndroidManifest.xml
38+
- name: Configure iOS Project
39+
working-directory: ./ReactNativeAdMobExample/ios
40+
run: |
41+
cp ../../Example/ios/ExampleTests/ExampleTests.m ./ReactNativeAdMobExampleTests/ReactNativeAdMobExampleTests.m
42+
sed -i -e '/target '"'"'ReactNativeAdMobExample'"'"' do/a\
43+
pod '"'"'Google-Mobile-Ads-SDK'"'"'
44+
' Podfile
45+
/usr/libexec/PlistBuddy -c 'Add :GADApplicationIdentifier string ca-app-pub-3940256099942544~1458002511' \
46+
ReactNativeAdMobExample/Info.plist
47+
pod install
48+
- name: Update App.js
49+
working-directory: ./ReactNativeAdMobExample
50+
run: cp ../Example/App.js ./App.js
51+
- name: Start packager
52+
working-directory: ./ReactNativeAdMobExample
53+
run: react-native start &
54+
- name: Run iOS
55+
working-directory: ./ReactNativeAdMobExample
56+
run: react-native run-ios --simulator "iPhone 11 Pro"
57+
- name: Take screenshot after delay
58+
run: sleep 30 && xcrun simctl io booted screenshot screenshot.png
59+
- uses: actions/upload-artifact@master
60+
with:
61+
name: screenshot
62+
path: ./screenshot.png
63+
- name: Run iOS tests
64+
working-directory: ./ReactNativeAdMobExample/ios
65+
run: |
66+
set -o pipefail && xcodebuild \
67+
-workspace ReactNativeAdMobExample.xcworkspace \
68+
-scheme ReactNativeAdMobExample \
69+
-sdk iphonesimulator \
70+
-destination 'platform=iOS Simulator,name=iPhone 11 Pro' \
71+
test | xcpretty

Example/App.js

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
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

Comments
 (0)