A react-native module for Google AdMob GADBanner and GADInterstitial (react-native v0.19.0 or newer required).
The banner is implemented as a component while the interstitial has an imperative API.
rnpm
(recommended)npm i react-native-admob -S
rnpm link
npm i react-native-admob -S
npm i react-native-admob -S
android/settings.gradle
include ':RNAdMob', ':app'
project(':RNAdMob').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-admob/android')
android/app/build.gradle
dependencies {
...
compile project(':RNAdMob')
}
MainActivity.java
On top, where imports are:
import com.sbugert.rnadmob.RNAdMobPackage;
Under protected List<ReactPackage> getPackages() {
:
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new RNAdMobPackage()
);
import { AdMobBanner, AdMobInterstitial } from 'react-native-admob'
// Display a banner
<AdMobBanner
bannerSize="fullBanner"
adUnitID="your-admob-unit-id"
testDeviveID="EMULATOR"
didFailToReceiveAdWithError={this.bannerError} />
// Display an interstitial
AdMobInterstitial.setAdUnitID('your-admob-unit-id');
AdMobInterstitial.setTestDeviceID('EMULATOR');
AdMobInterstitial.requestAd(AdMobInterstitial.showAd);
For a full example reference to the example project.
Corresponding to iOS framework banner size constants
Prop value | Description | Size |
---|---|---|
banner |
Standard Banner for Phones and Tablets | 320x50 |
largeBanner |
Large Banner for Phones and Tablets | 320x100 |
mediumRectangle |
IAB Medium Rectangle for Phones and Tablets | 300x250 |
fullBanner |
IAB Full-Size Banner for Tablet | 468x60 |
leaderboard |
IAB Leaderboard for Tablets | 728x90 |
smartBannerPortrait |
Smart Banner for Phones and Tablets (default) | Screen width x 32 |
smartBannerLandscape |
Smart Banner for Phones and Tablets | Screen width x 32 |
Note: There is no smartBannerPortrait
and smartBannerLandscape
on Android. Both prop values will map to smartBanner
Corresponding to Ad lifecycle event callbacks
Prop |
---|
adViewDidReceiveAd() |
didFailToReceiveAdWithError(errorDescription) |
adViewWillPresentScreen() |
adViewWillDismissScreen() |
adViewDidDismissScreen() |
adViewWillLeaveApplication() |
Name | Description |
---|---|
setAdUnitID(adUnitID) |
sets the AdUnit ID for all future ad requests. |
setTestDeviceID(deviceID) |
sets the test device ID |
requestAd(callback) |
requests an interstitial and calls callback when interstitialDidLoad orinterstitialDidFailToLoad event fires |
showAd(callback) |
shows an interstitial if it is ready and calls callback when interstitialDidOpen event fires |
isReady(callback) |
calls callback with boolean whether interstitial is ready to be shown |
For simulators/emulators you can use 'EMULATOR'
for the test device ID.
Note: tryShowNewInterstitial()
is deprecated as of v1.1.0 and can be replaced by calling requestAd
with showAd
as callback.
Unfortunately, events are not consistent across iOS and Android. To have one unified API, new event names are introduced for pairs that are roughly equivalent.
iOS | this library | Android |
---|---|---|
interstitialDidReceiveAd |
interstitialDidLoad |
onAdLoaded |
interstitial:didFailToReceiveAdWithError |
interstitialDidFailToLoad |
onAdFailedToLoad |
interstitialWillPresentScreen |
interstitialDidOpen |
onAdOpened |
interstitialDidFailToPresentScreen |
||
interstitialWillDismissScreen |
||
interstitialDidDismissScreen |
interstitialDidClose |
onAdClosed |
interstitialWillLeaveApplication |
interstitialWillLeaveApplication |
onAdLeftApplication |
Note that interstitialWillLeaveApplication
and onAdLeftApplication
are not exactly the same but share one event in this library.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。