This is a direct fork of sbugert/react-native-admob so much thanks to @sbugert. The original library was unmaintained so hopefully, this fork will remedy most of the issues. The module was renamed to allow for publishing of new packages on npm.
A react-native module for Google AdMob Banners, Interstitials, and Rewarded Videos, and also DFP Banners.
The banner types are implemented as components while the interstitial and rewarded video have an imperative API.
Note: The package name react-native-google-admob
seems to occupied on npm so please use direct repo url for now. The package is unmaintained (last commit 3 years ago) so I have sent an email (also made an issue) asking for ownership. As soon as I get it, I will update the README.
npm:
npm i --save https://github.com/thecodrr/react-native-google-admob
Yarn:
yarn add https://github.com/thecodrr/react-native-google-admob
You won't need to do anything. Autolinking will do all the work for you.
In order to use this library, you have to link it to your project first. There's excellent documentation on how to do this in the React Native Docs.
react-native link
in your project directory. Or add this to your Podfilepod 'RNAdMobManager', :path => '../node_modules/react-native-google-admob'
cd ios
and run pod install
Pods
project, select RNAdMobManager
in TARGET, click Build Pharses, expand Link Binary With Libraries
, drag GoogleMobileAds.framework
from Pods > Pods > Google-Mobile-Ads-SDK to Link Binary With Libraries
you have expanded before.For iOS you will have to add the Google Mobile Ads SDK to your Xcode project.
On Android the AdMob library code is part of Play Services, which is automatically added when this library is linked. But, you need to add your AdMob App ID to your app's AndroidManifest.xml
by adding the <meta-data>
tag shown below.
<!-- Put this inside <application></application> -->
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="YOUR_ADMOB_APP_ID"/>
Replace YOUR_ADMOB_APP_ID
with your AdMob App ID
.
import {
AdMobBanner,
AdMobInterstitial,
PublisherBanner,
AdMobRewarded,
} from 'react-native-google-admob'
// Display a banner
<AdMobBanner
adSize="fullBanner"
adUnitID="your-admob-unit-id"
testDevices={[AdMobBanner.simulatorId]}
onAdFailedToLoad={error => console.error(error)}
/>
// Display a DFP Publisher banner
<PublisherBanner
adSize="fullBanner"
adUnitID="your-admob-unit-id"
testDevices={[PublisherBanner.simulatorId]}
onAdFailedToLoad={error => console.error(error)}
onAppEvent={event => console.log(event.name, event.info)}
/>
// Display an interstitial
AdMobInterstitial.setAdUnitID('your-admob-unit-id');
AdMobInterstitial.setTestDevices([AdMobInterstitial.simulatorId]);
AdMobInterstitial.requestAd().then(() => AdMobInterstitial.showAd());
// Display a rewarded ad
AdMobRewarded.setAdUnitID('your-admob-unit-id');
AdMobRewarded.requestAd().then(() => AdMobRewarded.showAd());
For a full example reference to the example project.
adSize
Corresponding to iOS framework banner size constants
Value | Description | Availability | Size (WxH) |
---|---|---|---|
banner |
Standard Banner | Phones and Tablets | 320x50 |
largeBanner |
Large Banner | Phones and Tablets | 320x100 |
mediumRectangle |
IAB Medium Rectangle | Phones and Tablets | 300x250 |
fullBanner |
IAB Full-Size Banner | Tablets | 468x60 |
leaderboard |
IAB Leaderboard | Tablets | 728x90 |
smartBannerPortrait smartBannerLandscape
|
Smart Banner | Phones and Tablets | Screen width x 32|50|90 |
Note: There is no smartBannerPortrait
and smartBannerLandscape
on Android. Both prop values will map to smartBanner
onAdLoaded
Accepts a function. Called when an ad is received.
onAdFailedToLoad
Accepts a function. Called when an ad request failed.
onAdOpened
Accepts a function. Called when an ad opens an overlay that covers the screen.
onAdClosed
Accepts a function. Called when the user is about to return to the application after clicking on an ad.
onAdLeftApplication
Accepts a function. Called when a user click will open another app (such as the App Store), backgrounding the current app.
onSizeChange
Accepts a function. Called when the size of the banner changes. The function is called with an object containing the width and the height.
_Above names correspond to the Ad lifecycle event callbacks_
Same as AdMobBanner
, with the addition of 2 extra properties:
onAppEvent
Accepts a function. Called when DFP sends an event back to the app.
These events may occur at any time during the ad's lifecycle, even before onAdLoaded
is called. The function is called with an object, containing the name of the event and an info property, containing additional information.
More info here: https://developers.google.com/mobile-ads-sdk/docs/dfp/ios/banner#app_events
validAdSizes
An array of ad sizes which may be eligible to be served.
In comparison to the AdMobBanner
and PublisherBanner
which have a declaritive API, the AdMobInterstitial
has an imperative API.
setAdUnitID(adUnitID)
Sets the AdUnit ID for all future ad requests.
setTestDevices(devices)
Sets the devices which are served test ads.
For simulators/emulators you can use AdMobInterstitial.simulatorId
for the test device ID.
requestAd()
Requests an interstitial and returns a promise, which resolves on load and rejects on error.
showAd()
Shows an interstitial and returns a promise, which resolves when an ad is going to be shown, rejects when the ad is not ready to be shown.
isReady(callback)
Calls callback with a boolean value whether the interstitial is ready to be shown.
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.
This library | iOS | Android |
---|---|---|
adLoaded |
interstitialDidReceiveAd |
onAdLoaded |
adFailedToLoad |
interstitial:didFailToReceiveAdWithError |
onAdFailedToLoad |
adOpened |
interstitialWillPresentScreen |
onAdOpened |
adFailedToOpen |
interstitialDidFailToPresentScreen |
Not supported |
adClosed |
interstitialWillDismissScreen |
onAdClosed |
adLeftApplication |
interstitialWillLeaveApplication |
onAdLeftApplication |
In comparison to the AdMobBanner
and PublisherBanner
which have a declaritive API, the AdMobRewarded
has an imperative API, just like the AdMobInterstitial
.
setAdUnitID(adUnitID)
Sets the AdUnit ID for all future ad requests.
setTestDevices(devices)
Sets the devices which are served test ads.
For simulators/emulators you can use AdMobRewarded.simulatorId
for the test device ID.
requestAd()
Requests a rewarded ad and returns a promise, which resolves on load and rejects on error.
showAd()
Shows a rewarded ad and returns a promise, which resolves when an ad is going to be shown, rejects when the ad is not ready to be shown.
isReady(callback)
Calls callback with a boolean value whether the rewarded ad is ready to be shown.
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.
This library | iOS | Android |
---|---|---|
adLoaded |
rewardBasedVideoAdDidReceiveAd |
onRewardedVideoAdLoaded |
adFailedToLoad |
rewardBasedVideoAd:didFailToLoadWithError |
onRewardedVideoAdFailedToLoad |
rewarded |
rewardBasedVideoAd:didRewardUserWithReward |
onRewarded |
adOpened |
rewardBasedVideoAdDidOpen |
onRewardedVideoAdOpened |
videoStarted |
rewardBasedVideoAdDidStartPlaying |
onRewardedVideoStarted |
videoCompleted |
rewardBasedVideoAdDidCompletePlaying |
rewardedVideoAdVideoCompleted |
adClosed |
rewardBasedVideoAdDidClose |
onRewardedVideoAdClosed |
adLeftApplication |
rewardBasedVideoAdWillLeaveApplication |
onRewardedVideoAdLeftApplication |