项目作者: iClonal

项目描述 :
Google Mobile Ads SDK for React Native
高级语言: Java
项目地址: git://github.com/iClonal/react-native-sdk-admob.git
创建时间: 2018-12-21T04:08:21Z
项目社区:https://github.com/iClonal/react-native-sdk-admob

开源协议:

下载


react-native-sdk-admob

Getting started

$ npm install git+https://github.com/iChonal/react-native-sdk-admob.git --save

or

$ yarn add https://github.com/iChonal/react-native-sdk-admob.git

iOS

Installation

  • Automatic (using cocoapods)

$ react-native link react-native-sdk-admob

or

add pod 'react-native-sdk-admob', :path => '../node_modules/react-native-sdk-admob' to your Podfile

then run $ pod install

  • Manual

    1. install AdMob Sdk for iOS, see Google AdMob
    2. In XCode, in the project navigator, right click LibrariesAdd Files to [your project's name]
    3. Go to node_modulesreact-native-sdk-admob and add RNSdkAdmob.xcodeproj
    4. In XCode, in the project navigator, select your project. Add libRNSdkAdmob.a to your project’s Build PhasesLink Binary With Libraries

Configuration

add AdUnitId to Info.plist file

  • banner
  1. <key>GADBannerAdUnitId</key>
  2. <string>"YOUR_ADMOB_BANNER_ADUNIT_ID"</string>
  • Interstitial
  1. <key>GADInterstitialAdUnitId</key>
  2. <string>"YOUR_ADMOB_INTERSTITIAL_ADUNIT_ID"</string>
  • Rewarded Video
  1. <key>GADRewardedAdUnitId</key>
  2. <string>"YOUR_ADMOB_REWARDED_ADUNIT_ID"</string>

Initialization

Before loading ads, apps should initialize the Mobile Ads SDK by calling the configureWithApplicationID: class method in GADMobileAds, and passing it their AdMob App ID. This only needs to be done once, ideally at app launch. You can find the App ID for your app in the AdMob UI.

Here’s an example of how to call the configureWithApplicationID: method in your AppDelegate:

  1. - (BOOL)application:(UIApplication *)application
  2. didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  3. // Initialize Google Mobile Ads SDK
  4. // Sample AdMob app ID: ca-app-pub-3940256099942544~1458002511
  5. [GADMobileAds configureWithApplicationID:@"YOUR_ADMOB_APP_ID"];
  6. return YES;
  7. }

Android

Installation

  • Automatic

$ react-native link react-native-sdk-admob

  • Manual
  1. Append the following lines to android/settings.gradle:

    1. include ':react-native-sdk-admob'
    2. project(':react-native-sdk-admob').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-sdk-admob/android')
  2. Insert the following lines inside the dependencies block in android/app/build.gradle:

    1. implementation project(':react-native-sdk-admob')
  1. Open up android/app/src/main/java/[...]/MainApplication.java

    • Add import io.github.ichonal.sdkadmob.RNSdkAdmobPackage; to the imports at the top of the file
    • Add new RNSdkAdmobPackage() to the list returned by the getPackages() method

Configuration

  • AppId

Add your AdMob App ID to your app’s AndroidManifest.xml file by adding the <meta-data> tag shown below. You can find your App ID in the AdMob UI. For android:value insert your own AdMob App ID in quotes, as shown below.

  1. <manifest>
  2. <application>
  3. <!-- Sample AdMob App ID: ca-app-pub-3940256099942544~3347511713 -->
  4. <meta-data
  5. android:name="com.google.android.gms.ads.APPLICATION_ID"
  6. android:value="[ADMOB_APP_ID]"/>
  7. </application>
  8. </manifest>

Important: This step is required as of Google Mobile Ads SDK version 17.0.0. Failure to add this <meta-data> tag results in a crash with the message: “The Google Mobile Ads SDK was initialized incorrectly.”

  • AdUnitId: Banner
  1. <meta-data
  2. android:name="io.github.ichonal.sdkadmob.GADBANNER_ID"
  3. android:value="[ADMOB_BANNER_ADUNIT_ID]"/>
  • AdUnitId: Interstitial
  1. <meta-data
  2. android:name="io.github.ichonal.sdkadmob.GADINTERSTITIAL_ID"
  3. android:value="[ADMOB_INTERSTITIAL_ADUNIT_ID]"/>
  • AdUnitId: Rewarded Video
  1. <meta-data
  2. android:name="io.github.ichonal.sdkadmob.GADREWARDED_ID"
  3. android:value="[ADMOB_REWARDED_ADUNIT_ID]"/>

Initialization

Before loading ads, have your app initialize the Mobile Ads SDK by calling MobileAds.initialize() with your AdMob App ID. This needs to be done only once, ideally at app launch.

Here’s an example of how to call the initialize() method in an Activity:

  1. package ...
  2. import ...
  3. import com.google.android.gms.ads.MobileAds;
  4. public class MainActivity extends AppCompatActivity {
  5. ...
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9. // Sample AdMob app ID: ca-app-pub-3940256099942544~3347511713
  10. MobileAds.initialize(this, "YOUR_ADMOB_APP_ID");
  11. }
  12. ...
  13. }

Usage

  1. import {
  2. GADBanner,
  3. GADInterstitial,
  4. GADRewarded,
  5. } from 'react-native-sdk-admob';

DOC

GADBanner

GADInterstitial

GADRewarded

ACKNOWLEDGEMENT