Skip to content

Commit

Permalink
Merge pull request #40 from ammarahm-ed/fb-audience-fix
Browse files Browse the repository at this point in the history
Next Release: 0.3.0
  • Loading branch information
ammarahm-ed authored Jul 6, 2020
2 parents ecf2402 + b140dc5 commit c78aaea
Show file tree
Hide file tree
Showing 23 changed files with 646 additions and 89 deletions.
70 changes: 65 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,66 @@ return (
<h1>📃 Reference</h1>
</div>

## AdManager
AdManager allows you to configure your ads globally when the app starts

### setRequestConfiguration(config)
Configure your Ad Requests during App Startup. You need to pass a single object as an argument with atleast one of the following properties

| Name | Type | Required |
| --------- | -------- | -------- |
| testDeviceIds | `Array<string>` | no |
| maxAdContentRating | AdManager.MAX_AD_CONTENT_RATING | no |
| tagForChildDirectedTreatment | AdManager.TAG_FOR_CHILD_DIRECTED_TREATMENT | no |
| tagForUnderAgeConsent | AdManager.TAG_FOR_UNDER_AGE_CONSENT | no |

```js

const config = {
testDeviceIds:["YOUR_TEST_DEVICE_ID"],
maxAdContetRating:AdManager.MAX_AD_CONTENT_RATING.MA,
tagForChildDirectedTreatment:AdManager.TAG_FOR_CHILD_DIRECTED_TREATMENT.FALSE,
tagForUnderAgeConsent:AdManager.TAG_FOR_UNDER_AGE_CONSENT.FALSE
}

AdManager.setRequestConfiguration(config);

```

### isTestDevice()
Check if the current device is registered as a test device to show test ads.

```js
AdManager.isTestDevice().then(result => console.log(result))
```
return: `boolean`

### AdManager.MAX_AD_CONTENT_RATING

| Name | Description |
| --------- | -------- |
| G | "General audiences." Content suitable for all audiences, including families and children. |
| MA | "Mature audiences." Content suitable only for mature audiences; includes topics such as alcohol, gambling, sexual content, and weapons. |
| PG | "Parental guidance." Content suitable for most audiences with parental guidance, including topics like non-realistic, cartoonish violence. |
| T | "Teen." Content suitable for teen and older audiences, including topics such as general health, social networks, scary imagery, and fight sports. |
| UNSPECIFIED | Set default value to ""|

### AdManager.TAG_FOR_CHILD_DIRECTED_TREATMENT

| Name | Description |
| --------- | -------- |
| TRUE | Enabled |
| FALSE | Disabled |

### AdManager.TAG_FOR_UNDER_AGE_CONSENT

| Name | Description |
| --------- | -------- |
| TRUE | Enabled |
| FALSE | Disabled |

#

## NativeAdView

NativeAdView will wrap all your views related to the ad and provides a context through which all the Views get their respective information and load it automatically. It has the following properties to it.
Expand Down Expand Up @@ -238,7 +298,7 @@ Set Ad Unit ID for Native Advanced Ads that you created on your AdMob account.
| -------- | -------- | -------- |
| `string` | Yes | All |

#
#

#### `testDevices`

Expand All @@ -248,7 +308,7 @@ Set testDevices during testing ads or during development.
| --------------- | -------- | -------- |
| `Array<string>` | no | All |

#
#

#### `enableTestMode`

Expand All @@ -258,7 +318,7 @@ Setting this to true will load a placeholder ad (Not from Admob server) incase y
| --------- | -------- | -------- |
| `boolean` | no | All |

#
#

#### `delayAdloading`

Expand All @@ -269,7 +329,7 @@ Delay ad loading and rendering by the specified time in milliseconds. This is a
| `number` | no | 0 ms | All |


#
#

#### `refreshInterval`

Expand All @@ -279,7 +339,7 @@ Time in ms after which a new ad should be requested from the server.
| -------- | -------- | ------------------- | -------- |
| `number` | no | 60000 ms (1 minute) | All |

#
#

### Events

Expand Down
3 changes: 2 additions & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ android {

dependencies {
implementation 'com.facebook.react:react-native:+'
implementation 'com.google.android.gms:play-services-ads:+'
implementation 'com.google.android.gms:play-services-ads:19.2.0'
implementation 'com.android.support:support-annotations:28.0.0'
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ public class RNAdMobNativePackage implements ReactPackage {

@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
return Arrays.<NativeModule>asList();
return Arrays.<NativeModule>asList(
new RNAdmobNativeAdsManager(reactContext)
);
}

public List<Class<? extends JavaScriptModule>> createJSModules() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class RNAdMobNativeViewManager extends ViewGroupManager<RNNativeAdWrapper
public static final String PROP_PRICE_VIEW = "price";
public static final String PROP_ICON_VIEW = "icon";
public static final String PROP_STAR_RATING_VIEW = "starrating";
public static final String PROP_AD_CHOICES_PLACEMENT = "adChoicesPlacement";
public static final String PROP_NON_PERSONALIZED_ADS = "requestNonPersonalizedAdsOnly";

private RNNativeAdWrapper nativeAdView;

Expand Down Expand Up @@ -81,6 +83,8 @@ protected RNNativeAdWrapper createViewInstance(ThemedReactContext reactContext)
return nativeAdView;
}



@Override
public void addView(RNNativeAdWrapper parent, View child, int index) {
//super.addView(parent, child, index);
Expand All @@ -95,6 +99,20 @@ public void setRefreshInterval(final RNNativeAdWrapper view, final int interval)
nativeAdView.setAdRefreshInterval(interval);
}

@ReactProp(name = PROP_NON_PERSONALIZED_ADS, defaultBoolean = false)
public void setPropNonPersonalizedAds(final RNNativeAdWrapper view, final boolean npa) {

nativeAdView.setRequestNonPersonalizedAdsOnly(npa);
}


@ReactProp(name = PROP_AD_CHOICES_PLACEMENT)
public void setPropAdChoicesPlacement(final RNNativeAdWrapper view, final int location) {

nativeAdView.setAdChoicesPlacement(location);
}


@ReactProp(name = PROP_DELAY_AD_LOAD)
public void setPropDelayAdLoad(final RNNativeAdWrapper view, final int delay) {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.ammarahmed.rnadmob.nativeads;

import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableNativeArray;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.RequestConfiguration;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class RNAdmobNativeAdsManager extends ReactContextBaseJavaModule {

private ReactContext reactContext;

public RNAdmobNativeAdsManager(ReactApplicationContext rc) {
super(rc);
reactContext = rc;

}

@Override
public String getName() {
return "RNAdmobNativeAdsManager";
}




@ReactMethod
public void setRequestConfiguration(ReadableMap config) {


RequestConfiguration.Builder configuration = new RequestConfiguration.Builder();

if (config.hasKey("maxAdContentRating")) {
if (config.getString("maxAdContentRating") != null) {
configuration.setMaxAdContentRating(config.getString("maxAdContentRating"));
}
}

if (config.hasKey("tagForChildDirectedTreatment")) {
configuration.setTagForChildDirectedTreatment(config.getInt("tagForChildDirectedTreatment"));
}
if (config.hasKey("tagForUnderAgeOfConsent")) {
configuration.setTagForUnderAgeOfConsent(config.getInt("TagForUnderAgeOfConsent"));
}
if (config.hasKey("testDeviceIds")) {
ReadableNativeArray nativeArray = (ReadableNativeArray) config.getArray("testDeviceIds");
ArrayList<Object> list = nativeArray.toArrayList();
List<String> testDeviceIds = Arrays.asList(list.toArray(new String[list.size()]));
configuration.setTestDeviceIds(testDeviceIds);
}

MobileAds.setRequestConfiguration(configuration.build());
MobileAds.initialize(reactContext);

}



@ReactMethod
public void isTestDevice(Promise promise) {

AdRequest builder = new AdRequest.Builder().build();
if (builder != null) {
promise.resolve(builder.isTestDevice(reactContext));
}
}
}
Loading

0 comments on commit c78aaea

Please sign in to comment.