How to Secure Admob Adunit Ids Using Firebase | Android Studio Firebase Tutorials

How to Secure Admob Adunit Ids Using Firebase  | Android Studio Firebase Tutorials:


Friends  I will tell you how to control admob adunit id From Firebase and Secure Your admob Adunit Id's you can simply follow these steps to secure your admob adunit  ids using firebase.
for this method you can change admob adunit id's from firebase server. 

Content:
  1.  Create Android Studio Empty Prjoect
  2.  Create Button For Show Ads
  3.  Create Firebase Project
  4.  Dependencies 
  5.  Firebase Java Code For Admob Ads
  6.  Interstitial Ads Java Code
  7.  Run your App and Check it

1. Create Android Studio Project For Firebase Admob Ads

First You Can Create Android Empty Project Follow Steps Below
1.
2.
3.
4. Now Can Create a Simple Android Studio Empty Project Easily. Now Can Create A button for Showing  Admob Ads From Firebase Server.

How to Create Button in Android Studio For Showing Admob Ads using Firebase:

First We goto Xml File of Our Project and Simply Copy Below Code and Replace it into your activity_main.xml file..

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:text="Show Ad"
        android:textSize="30dp"
        android:layout_centerInParent="true"
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


</RelativeLayout>

Now Goto MainActivity.Java and Past this Code Under AppCompact Actvity .
    Button btn;
Now Goto MainActivity.Java and Past this code Under Oncreate mehod
btn = findViewById(R.id.btn1);
 btn.setOnClickListener(new View.OnClickListener() {
 @Override public void onClick(View v) { 
 AdRequest adRequest = new AdRequest.Builder().build();
 mInterstitialAd.loadAd(adRequest); 
 if (mInterstitialAd.isLoaded()) { 
 mInterstitialAd.show(); 
 } 
else 
{ Log.d("TAG", "The interstitial wasn't loaded yet.");
 }
 } 
 });

  1.  Create Firebase Project for Admob Ads
  • Open the Firebase Website, Goto Console and Ad a Project. 
  • gave project Name , tik on Accept and then click create project
  • Now Click Android Icon
  • now Register App in Firebase Project for Control Admob Adunit id's
  • after Register the app Goto Download and Config Simply Download googleservice.json file and and past it into your project apps folder. Watch Video for Any Problem, and Copy dependcies and past it into the project into grademodule.  You can find dependcies in depedencies heading of this article and watch video for complete setup.

4.dependencies for Firebase Admob Ads Project

implementation 'com.google.firebase:firebase-core:16.0.8'
        implementation 'com.google.firebase:firebase-database:16.1.0'
    implementation 'com.google.firebase:firebase-ads:17.2.0'
    implementation 'com.firebase:firebase-client-android:2.5.1'
}
apply plugin: 'com.google.gms.google-services'
classpath 'com.google.gms:google-services:4.0.1'

5.Java Code For Firebase Implementations:


Firebase.setAndroidContext(this);
        Firebase myFirebase = new Firebase("https://controlads-422f3.firebaseio.com/admob");
        myFirebase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                MyAdUnitId = dataSnapshot.getValue(String.class);

                

            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });

6.Java Code For Admob Interstitial Ads:

mInterstitialAd = new InterstitialAd(this);
        MobileAds.initialize(this,
                "ca-app-pub-3940256099942544~3347511713");

mInterstitialAd.setAdUnitId(MyAdUnitId);
                mInterstitialAd.loadAd(new AdRequest.Builder().build());

AdRequest adRequest = new AdRequest.Builder().build();
                mInterstitialAd.loadAd(adRequest);

                if (mInterstitialAd.isLoaded()) {
                    mInterstitialAd.show();
                } else {
                    Log.d("TAG", "The interstitial wasn't loaded yet.");
                }

now run the app and try to check if you have any problem please comment me and i will solve your problem..




Previous
Next Post »