Android Tutorial Admob Mediation With Facebook

Discussion in 'Tutorials & Examples' started by tufanv, Mar 22, 2017.

  1. tufanv

    tufanv Expert Licensed User

    Hello,

    This is the first time I am preparing a tutorial for this great community so please excuse me for any mistakes or weak explanations. Methods of some of the implementation may not be %100 correct but I can confirm it is working. Erel Already described the mediation with inmobi and with the help of that tutorial I tried to implement facebook ads to admob. It took some days for me to implement it because of manifest problems, correct sdk and adapter files etc. My aim with this tutorial is to help people mediate with facebook without any hassle.

    First of all you have to implement Firebase admob ( https://www.b4x.com/android/forum/threads/67710/#content)

    If you have firebase admob working without problems we can integrate facebook.

    1. We have to download the facebook android sdk located at https://developers.facebook.com/docs/android

    After the download
    Open the archive and navigate to AudienceNetwork/bin
    Copy the AudienceNetwork.aar to b4a libraries folder.

    2. We have to download the Facebook adapter for admob located at https://bintray.com/google/mobile-ads-adapters-android/com.google.ads.mediation.facebook#read

    Click on the files tab , click on 4.20.0.0 ( which is the latest version at the time of this tutorial , you can download the newer if there is one ) and download the aar file ( in this tutorial's case it is : facebook-4.20.0.0.aar )

    You can rename this to adapterfacebook.aar and copy this too to b4a libs folder.

    Our adapter and sdk is now ready.

    3.
    We have to create a new app at facebook developer page located at https://developers.facebook.com/

    At the top right , click on my apps / Add New App to add a new app. If you don't have an app earlier, navigation may be different but you can find easily " Create New App" button.

    New window will ask you app information shortly. Fill them and create the app.
    After the app is created open the app dashboard and navigate to Settings/Basic
    Click on Add Platform and select android.

    For Google Play Package Name : fill your package name. ( set at project / Build Configurations at b4a )
    For Class Name : I Always user my packagename.mainactivity , I am not sure if it is the correct entry but I don't think it is too much important as I don't have any problems with showing ads.
    For Key Hashes: we need our private sign key's sha1 value. In B4a navigate to : Tools / Private Sign Key . In the textfield find the SHA1 value of your sign key it is like : 6F:7B:0E: .....
    Copy That Value. I use Erel's code snippet to convert it to what facebook wants with byteconverter.

    You can add the below code to your app's application_create sub to find that value. After you get it delete it.

    Code:
    Dim bc As ByteConverter
        
    Dim raw() As Byte = bc.HexToBytes("xxxxx".Replace(":"""))
        
    Dim su As StringUtils
        LogColor(su.EncodeBase64(raw),
    Colors.cyan)
    Change xxxxx to sha1 value of your sign key.

    This will give you a hash ending with = copy that to Key hash field in facebook settings and click save changes.

    Our app is ready to be used for the audience network.


    4. Navigate to Audience Network link from your app's dashboard on facebook developer's console and Click on Placements. This is the place we will add banner and interstitial ad units.

    Click on Create Ad Placement.
    Leave the status Enabled on the opened window. Type a name for your placement. For Example : banner ( for banner ads ).
    For Steps to Trigger Ad : Write when is your ad going to be triggered for the app review team of facebook.
    Select Banner for Display Format.

    Our Banner Ad Unit is ready. Do the same to add interstitial ad unit ( if you like ) and create that too.

    Our Ad units are now ready


    5. Go to Admob. Click on Monetize. Select the app from the left. Click on the ad unit's mediation link which says 1 ad Source ( if you did not add any other mediation ) where you want to add facebook ads. You will see the admob network is already added. Now We click on New Ad Network. Select Facebook Audience Network and enter the placement id you get from the facebook developers console . Click continue. Set the ecpm of the facebook to a high value like $20 for now. ( To give priority to facebook ads , Later you can adjust it as you like.)

    Do the same procedure for the interstitial mediation on admob with the interstitial ad's placement id you got from facebook.

    Admob Mediation is now ready.

    6. Now Let's modify our app's code. First we need to add dependencies: Under Project Attributes add:

    Code:
    #AdditionalJar: AudienceNetwork.aar
    #AdditionalJar: adapterfacebook.aar

    Add these to your manifest: ( DO NOT forget to change the xxxxx with your app id from the facebook console , This is the general id of the app you have created not any ad placement id. Probably 16 digit id .) Also For the android label I used my app's name "Canli Doviz" you can change it to your own app name.

    Code:
    'facebook
    CreateResource(values, strings.xml, <resources>
         <
    string name="facebook_app_id">xxxxx</string>
        </resources>)
    AddApplicationText(<
    activity android:name="com.facebook.FacebookActivity"
              android:configChanges=
                     
    "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
              android:theme=
    "@android:style/Theme.Translucent.NoTitleBar" />
               <meta-data android:name=
    "com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>)

    AddApplicationText(
    application
    android:
    label="Canli Döviz"
    />
        <meta-data android:name=
    "com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/> )
       
    AddApplicationText(
    <
    activity android:name="com.facebook.ads.InterstitialAdActivity"
               android:configChanges=
    "keyboardHidden|orientation" />
    )
    'facebook end


    Everything is ready.

    After about 1 hours of adding the ad units ads will start to deliver. Most important thing here is facebook shows adds to people who ONLY uses native facebook app in his/her device and who are logged in.

    So you have to login to facebook app with your admin account you used for creating the apps and stay logged in.

    Because you have set the the ecpm value high for the facebook ads, they will appear before the admob. If you only see admob ads after 5-6 tries probably the ad units are not yet ready, or facebook do not have ads for you but probably you will see ads. You can understand the facebook ads from the label "AdChoices" brand printed on corner of every ad delivered. An example from my app for interstitial and banner are :
    [​IMG]

    [​IMG]



    I have clicked on the one banner and one insterstitial ad shown for the first time in my app because I read somewhere that we need to click one time at least to send the request but I am not sure about it. Maybe only requesting an ad from the server is enough. I don't think They will ban your account for clicking 1 time for an ad.




    7. After you confirm ads are delivering, you can upload your apk to google store ( you wont have any problems because older version of your apps will only show admob ads bacause no adapter if found for facebook ) . This is necessary I think as facebook people will download and try your app for review.

    8. Last Step is to go to facebook developers page again and switch to your app's console. Click on Audience Network / Placements .

    If your have seen ads delivered from the facebook you should see this:
    [​IMG]

    This means that ad unit got the request and you are ready to send you app to review.

    Click on Audience Network / Apps . Click Send For Review Next to your app name.


    All Done !
    If you have any problems or question I will be glad to help.

    Tufan.

     
    scsjc, Multiverse app, asales and 4 others like this.
  2. Erel

    Erel Administrator Staff Member Licensed User

    Thank you for sharing this valuable information!
     
    tufanv likes this.
  3. andymc

    andymc Well-Known Member Licensed User

    When I try to add my facebook mad to admob mediation, it is asking for an App access token, not a placement ID, how do I get one of these?
     
  4. jai

    jai Member Licensed User

    Nice tutorial!
    Encountered one problem though; after following all steps when I try to compile I get an error-

    An error occurred. Version required to extract this entry not supported (788)

    Error occurs at line-
    Code:
    #AdditionalJar: AudienceNetwork.aar
    I am using B4A ver 9.30.
    Any suggestions?
     
  5. jtare

    jtare Active Member Licensed User

    Same problem here. Were you able to solve it?

    Edit: This error occurred while compiling the app.
     
    Last edited: Aug 22, 2019
  6. jtare

    jtare Active Member Licensed User

    I fixed this issue by doing the following:
    -Rename the file from AudienceNetwork.aar to AudienceNetwork.zip
    -Extract the zip files to a folder
    -Rezip back the files inside that folder.
    -Rename the file from AudienceNetwork.zip to AudienceNetwork.aar

    I don't know the consequences of doing this or if it make sense at all. I got the idea from this post.
    7-zip was used to unzip and zip.
     
  7. tufanv

    tufanv Expert Licensed User

    you need to jetify them, it fixes the problem
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice