Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade Fyber SDK to 8.21.0 and added rewarded video result for android #4

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions RNFyberOfferWall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { NativeModules, NativeEventEmitter } from 'react-native';

const RNFyberOfferWall = NativeModules.RNFyberOfferWall;
const RNFyberOfferWallEventEmitter = new NativeEventEmitter(RNFyberOfferWall);

const eventHandlers = {
offerWallAvailable: new Map(),
offerWallUnavailable: new Map(),
offerWallFailedToLoad: new Map(),
offerWallDidStart: new Map(),
offerWallClosed: new Map()
};

const addEventListener = (type, handler) => {
switch (type) {
case 'offerWallAvailable':
eventHandlers[type].set(handler, RNFyberOfferWallEventEmitter.addListener(type, handler));
break;
case 'offerWallUnavailable':
eventHandlers[type].set(handler, RNFyberOfferWallEventEmitter.addListener(type, handler));
break;
case 'offerWallFailedToLoad':
eventHandlers[type].set(handler, RNFyberOfferWallEventEmitter.addListener(type, type, (error) => { handler(error); }));
break;
case 'offerWallDidStart':
eventHandlers[type].set(handler, RNFyberOfferWallEventEmitter.addListener(type, handler));
break;
case 'offerWallClosed':
eventHandlers[type].set(handler, RNFyberOfferWallEventEmitter.addListener(type, handler));
break;
default:
console.log(`Event with type ${type} does not exist.`);
}
}

const removeEventListener = (type, handler) => {
if (!eventHandlers[type].has(handler)) {
return;
}
eventHandlers[type].get(handler).remove();
eventHandlers[type].delete(handler);
}

const removeAllListeners = () => {
RNFyberOfferWallEventEmitter.removeAllListeners('offerWallAvailable');
RNFyberOfferWallEventEmitter.removeAllListeners('offerWallUnavailable');
RNFyberOfferWallEventEmitter.removeAllListeners('offerWallFailedToLoad');
RNFyberOfferWallEventEmitter.removeAllListeners('offerWallDidStart');
RNFyberOfferWallEventEmitter.removeAllListeners('offerWallClosed');
};

module.exports = {
...RNFyberOfferWall,
requestOfferWall: (cb = () => {}) => RNFyberOfferWall.requestOfferWall(cb),
showOfferWall: () => RNFyberOfferWall.showOfferWall(),
addEventListener,
removeEventListener,
removeAllListeners
};
5 changes: 3 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.library'

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
buildToolsVersion '27.0.3'

defaultConfig {
minSdkVersion 16
Expand All @@ -20,5 +20,6 @@ repositories {

dependencies {
implementation 'com.facebook.react:react-native:+'
implementation 'com.fyber:fyber-sdk:8.20.0'
implementation 'com.google.android.gms:play-services-ads:9.2.0'
implementation 'com.fyber:fyber-sdk:8.21.0@aar'
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,21 @@
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.support.annotation.Nullable;

import com.facebook.react.bridge.ActivityEventListener;
import com.facebook.react.bridge.BaseActivityEventListener;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.fyber.Fyber;
import com.fyber.ads.AdFormat;
import com.fyber.requesters.OfferWallRequester;
import com.fyber.requesters.RequestCallback;
import com.fyber.requesters.RequestError;
import com.facebook.react.bridge.Callback;

/**
* Created by benyee on 16/01/2016.
Expand All @@ -25,10 +31,27 @@ public class RNFyberOfferWallModule extends ReactContextBaseJavaModule {
private RequestCallback requestCallback;
private ReactApplicationContext mContext;
private Intent mOfferWallIntent;
private Callback requestAdCallback;

private final ActivityEventListener mActivityEventListener = new BaseActivityEventListener() {

@Override
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
// handle the closing of the video
if (resultCode == Activity.RESULT_OK && requestCode == OFFER_WALL_REQUEST) {

// OfferWall Closed
Log.d(TAG, "The offerwall is closed");
sendEvent("offerWallClosed", null);
}
}
};

public RNFyberOfferWallModule(ReactApplicationContext reactContext) {
super(reactContext);
mContext = reactContext;
// Add the listener for `onActivityResult`
reactContext.addActivityEventListener(mActivityEventListener);
}

@Override
Expand All @@ -43,35 +66,57 @@ public void initializeOfferWall(final String appId, final String securityToken,
public void run() {
Log.d(TAG, "Settings appId:" + appId);
Fyber.Settings settings = Fyber.with(appId, getCurrentActivity()).withUserId(userId).withSecurityToken(securityToken).start();
requestOfferWall(new Callback() {
public void invoke(Object... args) {}
});
}
});
}

@ReactMethod
public void requestOfferWall(final Callback callback) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Log.d(TAG, ">> Requesting OfferWall");
requestCallback = new RequestCallback() {
@Override
public void onRequestError(RequestError requestError) {
Log.d(TAG, "Something went wrong with the request: " + requestError.getDescription());
sendEvent("offerWallFailedToLoad", null);
}

@Override
public void onAdAvailable(Intent intent) {
Log.d(TAG, "Offers are available");
mOfferWallIntent = intent;
sendEvent("offerWallAvailable", null);
}

@Override
public void onAdNotAvailable(AdFormat adFormat) {
Log.d(TAG, "No ad available");
sendEvent("offerWallUnavailable", null);
callback.invoke("OfferWall is not ready.");
}
};
OfferWallRequester.create(requestCallback).request(mContext);
}
});
}

private void sendEvent(String eventName, @Nullable WritableMap params) {
getReactApplicationContext().getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName, params);
}

@ReactMethod
public void showOfferWall() {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
mContext.startActivityForResult(mOfferWallIntent, OFFER_WALL_REQUEST, null);
Log.d(TAG, "showOfferWall started");
sendEvent("offerWallDidStart", null);
mContext.startActivityForResult(mOfferWallIntent, OFFER_WALL_REQUEST, null);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public List<NativeModule> createNativeModules(ReactApplicationContext reactConte
List<NativeModule> modules = new ArrayList<>();
modules.add(new RNFyberOfferWallModule(reactContext));
modules.add(new RNFyberRewardedVideoModule(reactContext));
modules.add(new RNFyberUserModule(reactContext));
return modules;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.support.annotation.Nullable;
import android.util.Log;

import com.facebook.react.bridge.ActivityEventListener;
import com.facebook.react.bridge.BaseActivityEventListener;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;

import com.fyber.Fyber;
import com.fyber.ads.AdFormat;
import com.fyber.ads.videos.RewardedVideoActivity;
import com.fyber.requesters.RequestCallback;
import com.fyber.requesters.RequestError;
import com.fyber.requesters.RewardedVideoRequester;
Expand All @@ -33,10 +34,41 @@ public class RNFyberRewardedVideoModule extends ReactContextBaseJavaModule {
private Intent mRewardedVideoIntent;
private Callback requestAdCallback;

private final ActivityEventListener mActivityEventListener = new BaseActivityEventListener() {

@Override
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
// handle the closing of the video
if (resultCode == Activity.RESULT_OK && requestCode == REWARDED_VIDEO_REQUEST_CODE) {

// check the engagement status
String engagementResult = data.getStringExtra(RewardedVideoActivity.ENGAGEMENT_STATUS);
switch (engagementResult) {
case RewardedVideoActivity.REQUEST_STATUS_PARAMETER_FINISHED_VALUE:
// The user watched the entire video and will be rewarded
Log.d(TAG, "The video ad was dismissed because the user completed it");
sendEvent("rewardedVideoClosedByUser", null);
break;
case RewardedVideoActivity.REQUEST_STATUS_PARAMETER_ABORTED_VALUE:
// The user stopped the video early and will not be rewarded
Log.d(TAG, "The video ad was dismissed because the user explicitly closed it");
sendEvent("rewardedVideoClosedByError", null);
break;
case RewardedVideoActivity.REQUEST_STATUS_PARAMETER_ERROR:
// An error occurred while showing the video and the user will not be rewarded
Log.d(TAG, "The video ad was dismissed error during playing");
sendEvent("rewardedVideoClosedByError", null);
break;
}
}
}
};

public RNFyberRewardedVideoModule(ReactApplicationContext reactContext) {
super(reactContext);
mContext = reactContext;
// Add the listener for `onActivityResult`
reactContext.addActivityEventListener(mActivityEventListener);
}

@Override
Expand Down
61 changes: 61 additions & 0 deletions android/src/main/java/co/squaretwo/rnfyber/RNFyberUserModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package co.squaretwo.rnfyber;

import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.fyber.Fyber;
import com.fyber.user.User;

/**
* Created by Heiko Weber on 17/05/2018.
*/
public class RNFyberUserModule extends ReactContextBaseJavaModule {
private static final String TAG = "RNFyberUser";

private ReactApplicationContext mContext;

public RNFyberUserModule(ReactApplicationContext reactContext) {
super(reactContext);
mContext = reactContext;
}

@Override
public String getName() {
return TAG;
}

@ReactMethod
public void set(final ReadableMap uprops) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
try {
if (uprops.hasKey("age")) {
User.setAge(uprops.getInt("age"));
}
if (uprops.hasKey("custom")) {
ReadableMap custom = uprops.getMap("custom");
String[] pubs = { "pub0","pub1","pub2","pub3", "pub4", "pub5", "pub6", "pub7", "pub8", "pub9" };
for (String s: pubs) {
if (custom.hasKey(s)) {
User.addCustomValue(s, custom.getString(s));
}
}
}
} catch (Exception e) {
}
}
});
}

@ReactMethod
public void startFyberSDK(final String appId, final String securityToken, final String userId) {
Log.d(TAG, "startFyberSDK for appId:" + appId);
Fyber.with(appId, getCurrentActivity()).withUserId(userId).withSecurityToken(securityToken).start();
}
}
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// react-native-fyber
//
//
import React, {Component} from 'react';
import {NativeModules, Text, View, requireNativeComponent} from 'react-native'

Expand All @@ -20,7 +20,8 @@ class FyberBanner extends Component {
}
}

const FyberOfferWall = NativeModules['RNFyberOfferWall'];
const FyberUser = NativeModules['RNFyberUser'];
import FyberRewardedVideo from './RNFyberRewardedVideo';
import FyberOfferWall from './RNFyberOfferWall';

module.exports = {FyberBanner, FyberOfferWall, FyberRewardedVideo};
module.exports = {FyberBanner, FyberOfferWall, FyberRewardedVideo, FyberUser};
Loading