Ali Al Fayed
Ali Al Fayed

Reputation: 524

How To Create Test Facebook Ads || How to Get Android Advertisement ID (AAID) programmatically?

How to create Test Device to Facebook Ads >> To Create Test Device you need Enter a device ID (IDFA, AAID):

sometimes we need to get the android advertisement id AAID for android device, when we place google ads or Facebook ads to our Android app or any other functions we need to place the unique AAID for testing purpose.

Upvotes: 2

Views: 1634

Answers (3)

Sakhawat Hossain
Sakhawat Hossain

Reputation: 463

The following code snippet might help lot of you in current cases.

Add this method in your code:

fun getIdThread() {
    var adInfo: AdvertisingIdClient.Info? = null
    try {
        adInfo = AdvertisingIdClient.getAdvertisingIdInfo(this)
    } catch (e: IOException) {
        // Unrecoverable error connecting to Google Play services (e.g.,
        // the old version of the service doesn't support getting AdvertisingId).
    } catch (e: GooglePlayServicesAvailabilityException) {
        // Encountered a recoverable error connecting to Google Play services.
    } catch (e: GooglePlayServicesNotAvailableException) {
        // Google Play services is not available entirely.
    }
    val id: String = adInfo?.id.orEmpty()
    val isLAT: Boolean = adInfo?.isLimitAdTrackingEnabled?.orFalse() == true
}

And call from separate thread as like below.

Thread{
   getIdThread()
}.start()

This simple snippet will give you the AD ID. Even if the user resets the advertising ID.

Note: [Don't forget to declare ad ID permission in your manifest]

Upvotes: 0

Hamid Raza Goraya
Hamid Raza Goraya

Reputation: 159

For Kotlin code

Thread{
        AdvertisingIdClient.getAdvertisingIdInfo(activity).id?.let {
            Log.i("myId",it)
        }
    }.start()

Upvotes: 2

Ali Al Fayed
Ali Al Fayed

Reputation: 524

The first Step, get AdvertisingIdClient

public class AdvertisingIdClient extends Object Helper library for retrieval of advertising ID and related information such as the limit ad tracking setting.

It is intended that the advertising ID completely replace existing usage of other identifiers for ads purposes (such as use of ANDROID_ID in Settings.Secure) when Google Play Services is available. Cases, where Google Play Services is unavailable, are indicated by a GooglePlayServicesNotAvailableException being thrown by getAdvertisingIdInfo().

Code >>>

@SuppressLint("StaticFieldLeak")
AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {
    @Override
    protected String doInBackground(Void... params) {
        String token = null;
        AdvertisingIdClient.Info adInfo = null;
        try {
            adInfo = AdvertisingIdClient.getAdvertisingIdInfo(getApplicationContext());
        } catch (IOException | GooglePlayServicesRepairableException | GooglePlayServicesNotAv
            // ...
            MessageLog.setLogCat("DEVICE_ID",""+e);
        }
        String android_id = adInfo.getId();
        MessageLog.setLogCat("DEVICE_ID",android_id);
        return android_id;
    }
    @Override
    protected void onPostExecute(String token) {
        MessageLog.setLogCat("DEVICE_ID","DEVICE_ID Access token retrieved:" + token);
    }
};
task.execute();

Open Your Logcate of android studio you will get Result like it

2020-07-23 19:32:37.225 19041-19093/com.packagename.example I/DEVICE_ID :: 1aaf7707-22b5-4627-xxx-xxxxxxxx
2020-07-23 19:32:37.738 19041-19041/com.packagename.example I/DEVICE_ID :: DEVICE_ID Access token retrieved:1aaf7707-22b5-4627-xxx-xxxxxxxx

1aaf7707-22b5-4627-xxx-xxxxxxxx is your device ID Second Step: Open your Facebook Monetization Manager enter image description here write your device id in a device ID (IDFA, AAID): EditText and Write Name Device like the image.

Upvotes: 0

Related Questions