Implement Admob GDPR message - Java
Complete the following steps to create a GDPR message.
- Library Implement (Admob & GDPR )
- Create GDPR Java class
- Call this method in your activity class
1. Library Implement (Admob & GDPR )
Code:
//GDPR Message
implementation 'com.google.android.ump:user-messaging-platform:2.1.0'
2. Create GDPR Java class
Code :
public class GDPR {
Activity activity;
String GDPR_DEBUG_TAG = "";
private final AtomicBoolean isMobileAdsInitializeCalled = new AtomicBoolean(false);
public GDPR(Activity activity) {
this.activity = activity;
}
public void setGDPR() {
ConsentRequestParameters params = new ConsentRequestParameters
.Builder()
.setTagForUnderAgeOfConsent(false)
.build();
ConsentInformation consentInformation = UserMessagingPlatform.getConsentInformation(activity);
consentInformation.requestConsentInfoUpdate(activity, params, (ConsentInformation.OnConsentInfoUpdateSuccessListener) () -> {
UserMessagingPlatform.loadAndShowConsentFormIfRequired(
activity,
(ConsentForm.OnConsentFormDismissedListener) loadAndShowError -> {
if (loadAndShowError != null) {
Log.w(GDPR_DEBUG_TAG, String.format("%s: %s", loadAndShowError.getErrorCode(), loadAndShowError.getMessage()));
}
// Consent has been gathered.
if (consentInformation.canRequestAds()) {
initializeMobileAdsSdk();
}
}
);
}, (ConsentInformation.OnConsentInfoUpdateFailureListener) requestConsentError -> {
Log.w(GDPR_DEBUG_TAG, String.format("%s: %s", requestConsentError.getErrorCode(), requestConsentError.getMessage()));
});
if (consentInformation.canRequestAds()) {
initializeMobileAdsSdk();
}
} // setGDPR End here ======================
private void initializeMobileAdsSdk() {
if (isMobileAdsInitializeCalled.getAndSet(true)) {
return;
}
MobileAds.initialize(activity);
} // initializeMobileAdsSdk end here =========
} // GDPR End Here ===============================
3. Call this method in your activity class
// Create GDPR Class Object and call setGDPR method
GDPR gdpr = new GDPR(MainActivity.this);
gdpr.setGDPR();
You Are Successfully Implement GDPR Message 😍
Masha allah
bai manifaste kon kico ki add korte hobe
thank you