FreeStar Ads Mediation for Cordova Android - freestarcapital/SDK_documentation_Android GitHub Wiki

Freestar Ads Mediation provides support for Cordova

Getting Started

Maximize your revenue and start displaying Freestar Mediation Ads in your existing Cordova mobile app running on Android today.

Requirements

• You must have a working Cordova app running on an Android device.

• Latest version of Android Studio. And using SDK Manager:

Android SDK's Installed: Android 33 Android 32 Android 31 (12) Android 30 (11)

Android Build Tools Installed: 33 32 31 30

• Works with Cordova-Android 10.x and higher

• Note: No longer supports Cordova-Android 9.x

Install our npm package

In your Cordova project root folder:

First, remove the old cordova-plugin-freestar:

cordova plugin remove cordova-plugin-freestar

Note: You may also need to delete your existing package-lock.json.

Add the new cordova-plugin-freestar:

cordova plugin add @freestar/[email protected]

Once installed, please make sure cordova-plugin-freestar is at least version 1.3.7 by looking within your package.json file.

config.xml

Add the following to config.xml in your project root folder:


<preference name="AndroidXEnabled" value="true" />


AndroidManifest.xml

Edit [Cordova project root]/platforms/android/app/src/main/AndroidManifest.xml

Add the following properties the application tag property as such:

    <application
        ...
        android:networkSecurityConfig="@xml/network_security_config"
        android:usesCleartextTraffic="true">

Add the following tags under the application tag:


        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-3940256099942544~3347511713" />
        <meta-data
            android:name="applovin.sdk.key"
            android:value="hO52kFtMvEo_AoeRzED0_XXfS1B1VQp9GW50yudJO-eUUTOmRBLl3c-2GyTevLNspll_fN5PLTbAHOakoTuHuP" />
        <meta-data
            android:name="com.freestar.android.ads.API_KEY"
            android:value="XqjhRR" />

Important Note: The above are test values that will need to be changed before releasing to production.

Add (if not already exists) android:exported="true" to the MainActivity tag property as follows:

        <activity
            android:name="MainActivity"
            android:exported="true"
            ...
        >
                       

Network Security Config

Add network_security_config.xml to the [Cordova project root]/platforms/android/app/src/main/res/xml folder. If that folder does not exist, please create.

Banner Ads

   
/*  USAGE: window.plugins.freestarPlugin.showBannerAd(placement, bannerAdSize, bannerAdPosition)
 *
 *  1st parameter is the placement. Please pass null
 *  if you aren't using one or the string placement.
 *
 *  2nd parameter can be ONE of the following:
 *  window.plugins.freestarPlugin.BANNER_AD_SIZE_320x50
 *  window.plugins.freestarPlugin.BANNER_AD_SIZE_300x250
 *  window.plugins.freestarPlugin.BANNER_AD_SIZE_728x90
 * 
 *  3rd parameter can be ONE of the following:
 *  window.plugins.freestarPlugin.BANNER_AD_POSITION_BOTTOM
 *  window.plugins.freestarPlugin.BANNER_AD_POSITION_CENTER
 *  window.plugins.freestarPlugin.BANNER_AD_POSITION_TOP
 */
  
//EXAMPLE: Show a small banner ad at the bottom of the screen with default null placement:
window.plugins.freestarPlugin.showBannerAd(null,
                                           window.plugins.freestarPlugin.BANNER_AD_SIZE_320x50,
                                           window.plugins.freestarPlugin.BANNER_AD_POSITION_BOTTOM);

//To close the banner ad:
window.plugins.freestarPlugin.closeBannerAd(placement, bannerAdSize);  //Closes the banner ad

//Register for the following banner ad callbacks and define their functions:
document.addEventListener('onBannerAdShowing', onBannerAdShowing, false);
document.addEventListener('onBannerAdFailed', onBannerAdFailed, false);
document.addEventListener('onBannerAdClicked', onBannerAdClicked, false);

function onBannerAdShowing(data) {
   console.log('banner showing. placement: ' + data.placement 
                            + ' bannerAdSize: ' + data.banner_ad_size);
}

function onBannerAdFailed(data) {
   console.log('banner show failed. placement: ' + data.placement 
                                 + ' bannerAdSize: ' + data.banner_ad_size 
                                 + ' error: ' + data.error);
}

function onBannerAdClicked(data) {
   console.log('banner clicked. placement: ' + data.placement 
                            + ' bannerAdSize: ' + data.banner_ad_size);
}

Interstitial Ads

//Register for the following interstitial callbacks and declare/define their functions:
document.addEventListener('onInterstitialLoaded', onInterstitialLoaded, false);
document.addEventListener('onInterstitialFailed', onInterstitialFailed, false);
document.addEventListener('onInterstitialShown', onInterstitialShown, false);
document.addEventListener('onInterstitialDismissed', onInterstitialDismissed, false);
document.addEventListener('onInterstitialClicked', onInterstitialClicked, false);

//Load the interstitial ad.  Pass in the string placement, or null if you don't use one.
window.plugins.freestarPlugin.loadInterstitialAd(null);

//Declare the following function and show the interstitial ad within it.
function onInterstitialLoaded(data) {
   console.log("onInterstitialLoaded: "+ data.placement);

   //Don't forget to pass in the placement
   window.plugins.freestarPlugin.showInterstitialAd(data.placement);
}

function onInterstitialFailed(data) {
   console.log("onInterstitialFailed: "+ data.placement + " " + data.error);

   //Note: It is not recommended to call loadInterstitialAd here as it can cause eternal loop.
}

function onInterstitialShown(data) {
   console.log("onInterstitialShown: "+ data.placement);
}

function onInterstitialDismissed(data) {
   console.log("onInterstitialDismissed: "+ data.placement);
   
   //Note: It is not recommended to pre-fetch the next ad in here because we already do that internally.
}

function onInterstitialClicked(data) {
   console.log("onInterstitialClicked: "+ data.placement);
}

Rewarded Ads

//Register for the following rewarded ad callbacks and declare/define their functions:

document.addEventListener('onRewardedVideoLoaded', onRewardedVideoLoaded, false);
document.addEventListener('onRewardedVideoFailed', onRewardedVideoFailed, false);
document.addEventListener('onRewardedVideoShown', onRewardedVideoShown, false);
document.addEventListener('onRewardedVideoShownError', onRewardedVideoShownError, false);
document.addEventListener('onRewardedVideoDismissed', onRewardedVideoDismissed, false);
document.addEventListener('onRewardedVideoCompleted', onRewardedVideoCompleted, false);

//Load the rewarded ad.  Pass in the string placement, or null if you don't use one.
window.plugins.freestarPlugin.loadRewardedAd(null);

//Declare the following function and show the rewarded ad within it.
function onRewardedVideoLoaded(data) {
   console.log("onRewardedVideoLoaded: "+ data.placement);
   //MySecret123 - optional, can be any string
   //MyUserId - optional, can be any string
   //Gold Coins - optional, type of reward, can be any string like 'V-Bucks', 'Gold Coins', etc
   //100 - optional, string value representing the amount of type of reward
   //don't forget to pass the placement to show.
   window.plugins.freestarPlugin.showRewardedAd(data.placement, "MySecret123", "MyUserId", "Gold Coins", "100");
}

//Define the rest of the rewarded ad callbacks:

function onRewardedVideoFailed(data) {
   console.log("onRewardedVideoFailed: "+ data.placement + " " + data.error);

   //Note: It is not recommended to call loadRewardedAd here as it can cause eternal loop.
}

function onRewardedVideoShown(data) {
   console.log("onRewardedVideoShown: "+ data.placement);
}

function onRewardedVideoShownError(data) {
   console.log("onRewardedVideoShownError: "+ data.placement + " " + data.error);
}

function onRewardedVideoDismissed(data) {
   console.log("onRewardedVideoDismissed: "+ data.placement);
   
   //Note: It is not recommended to pre-fetch the next ad in here because we already do that internally.
}

function onRewardedVideoCompleted(data) {
   console.log("onRewardedVideoCompleted: "+ data.placement);
   //This callback means the user has completed watching the video and you may reward them at this time.

   //Note: It is not recommended to pre-fetch the next ad in here because we already do that internally.
}

Toggle Test Mode

You can toggle between test and production mode. Pass false or omit the following line to turn off test mode.

window.plugins.freestarPlugin.setTestModeEnabled( true|false, 'optional hash' );

Sample Freestar Cordova App

Check out our Cordova Sample.

⚠️ **GitHub.com Fallback** ⚠️