Mohammed Salaam
Mohammed Salaam

Reputation: 31

Get admob Id remotely

I have an application I made using android studio. As of the moment, the admob ids are defined in the strings.xml file. However, I want to make the app get the admob ids from either my server or from firebase as I want to be able to change them remotely should any problems occur. How would I go about doing this?

Upvotes: 1

Views: 3681

Answers (3)

Frank van Puffelen
Frank van Puffelen

Reputation: 599101

There is a new API that allows you to manage Firebase projects and get configuration data from them. This would be the most logical place to look up such configuration data programmatically.

I checked the projects.get and projects.getAdminConfig, but haven't been able to find the ID you're looking for yet.

If that ID is indeed missing, it might be worth it to file a feature request.

Upvotes: 0

Ramesh
Ramesh

Reputation: 280

Either you can load Ads From Firebase Using Remote Config

or you Can Use the Firebase database. Refer this Sample Project from my Github Load Ads From Firebse

Using Database

Upvotes: 2

Joshua de Guzman
Joshua de Guzman

Reputation: 2143

You can do it programmatically.

Layout

From your layout file, add AdView:

<com.google.android.gms.ads.AdView
   xmlns:ads="http://schemas.android.com/apk/res-auto"
   android:id="@+id/adView"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_centerHorizontal="true"
   android:layout_alignParentBottom="true"
   ads:adSize="BANNER"
   ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>

Retrieving Ad Unit Id

  • You need to do network calls either on your splash screen or main activity to retrieve your Ad Unit Id/s
  • You can use Retrofit for doing such http request calls from your web server
  • If you choose Firebase, you can use their Firebase Realtime Database

Setting Ad Unit Id

In Java

AdView adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111"); // Set ad unit id

In Kotlin

val adView = AdView(this)
adView.adSize = AdSize.BANNER
adView.adUnitId = "ca-app-pub-3940256099942544/6300978111" // Set ad unit id

Additional

Depending on your persistent storage implementation, here's a one liner way of saving the Ad Unit Id using SharedPreferences

PreferenceManager.getDefaultSharedPreferences(context).edit().putString("MY_AD_UNIT_ID", "YOUR_AD_UNIT_VALUE").apply();

Here's how you will retrieve it

PreferenceManager.getDefaultSharedPreferences(context).getString("MY_AD_UNIT_ID", "DEFAULT_STRING_IF_NOTHING_WAS_FOUND"); 

Where context is your Context.

Read more:

Upvotes: 1

Related Questions