I'm in the process of testing EU GDPR user consent as per new IAB updates. I have created two functions to display the advertisement after the code is executed,
but I have no idea what is the appropriate function after the code is executed.
There are three options as follows:
UNKNOWN
NOT_REQUIRED
OBTAINED
This is my code it works normally I just need what can be done after that
package net.ads.ump;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.ads.AdRequest;
import com.google.ads.mediation.admob.AdMobAdapter;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.RequestConfiguration;
import com.google.android.ump.ConsentDebugSettings;
import com.google.android.ump.ConsentForm;
import com.google.android.ump.ConsentInformation;
import com.google.android.ump.ConsentRequestParameters;
import com.google.android.ump.UserMessagingPlatform;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ConsentInformation consentInformation;
private ConsentForm consentForm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<String> testDeviceIds = Arrays.asList(Config.deviceIds);
RequestConfiguration configuration =
new RequestConfiguration.Builder().setTestDeviceIds(testDeviceIds).build();
MobileAds.setRequestConfiguration(configuration);
ConsentDebugSettings debugSettings = new ConsentDebugSettings.Builder(this)
.setDebugGeography(ConsentDebugSettings
.DebugGeography.DEBUG_GEOGRAPHY_EEA)
.addTestDeviceHashedId(Config.deviceIds).build();
ConsentRequestParameters params = new ConsentRequestParameters
.Builder().setConsentDebugSettings(debugSettings).build();
consentInformation = UserMessagingPlatform.getConsentInformation(this);
consentInformation.requestConsentInfoUpdate(
this, params, () -> {
if (consentInformation.isConsentFormAvailable()) {
loadForm();
}
},
formError -> {
});
}
public void loadForm() {
try {
UserMessagingPlatform.loadConsentForm(
this, setConsentForm -> {
MainActivity.this.consentForm = setConsentForm;
switch (consentInformation.getConsentStatus()) {
case ConsentInformation.ConsentStatus.REQUIRED:
if (consentForm != null) {
consentForm.show(MainActivity.this,
formError -> {
loadForm();
});
}
break;
case ConsentInformation.ConsentStatus.UNKNOWN:
// What is the appropriate function?
// loadAd_GDPR_IAB(); || loadAd();
break;
case ConsentInformation.ConsentStatus.NOT_REQUIRED:
// What is the appropriate function?
// loadAd_GDPR_IAB(); || loadAd();
break;
case ConsentInformation.ConsentStatus.OBTAINED:
// What is the appropriate function?
// loadAd_GDPR_IAB(); || loadAd();
break;
}
},
formError -> {
});
} catch (Exception e) {
e.printStackTrace();
}
}
private void loadAd() {
MobileAds.initialize(this, initializationStatus -> {
});
AdView adView = findViewById(R.id.bannerId);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}
private void loadAd_GDPR_IAB() {
MobileAds.initialize(this, initializationStatus -> {
});
AdView adView = findViewById(R.id.bannerId);
Bundle extras = new Bundle();
extras.putString("npa", "1");
AdRequest adRequest = new AdRequest.Builder().addNetworkExtrasBundle(AdMobAdapter.class, extras).build();
adView.loadAd(adRequest);
}
}
These are the two functions I created :
private void loadAd() {
MobileAds.initialize(this, initializationStatus -> {
});
AdView adView = findViewById(R.id.bannerId);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}
private void loadAd_GDPR_IAB() {
MobileAds.initialize(this, initializationStatus -> {
});
AdView adView = findViewById(R.id.bannerId);
Bundle extras = new Bundle();
extras.putString("npa", "1");
AdRequest adRequest = new AdRequest.Builder().addNetworkExtrasBundle(AdMobAdapter.class, extras).build();
adView.loadAd(adRequest);
}
Also, is the code executed in the main activity of the application only, or should it be executed every class in which an advertisement is displayed?
This is the first time I have implemented an Admob code for EU Users Law Please help Thanks