Android: Getting user's location using Admob's Consent SDK

家住魔仙堡 提交于 2019-12-06 10:26:39

问题


I have an issue with AdMob's new 'Consent SDK'; the integration guide says to put the following into onCreate...

public class MainActivity extends Activity {
    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        ConsentInformation consentInformation = ConsentInformation.getInstance(context);
        String[] publisherIds = {"pub-xxxxxxxxxxxxxxxx"};
        consentInformation.requestConsentInfoUpdate(publisherIds, new ConsentInfoUpdateListener() {
            @Override
            public void onConsentInfoUpdated(ConsentStatus consentStatus) {
                 // User's consent status successfully updated.
            }

            @Override
            public void onFailedToUpdateConsentInfo(String errorDescription) {
                 // User's consent status failed to update.
            }
        });
        ...
    }
    ...
}

And then make a call to:

ConsentInformation.getInstance(context).isRequestLocationInEeaOrUnknown()

The problem I'm having is that when the app is first installed/launched, isRequestLocationInEeaOrUnknown() always returns 'false' (I am in the EEA by the way).

If I then exit the app and re-launch it, it returns 'true' - this is correct. If I then go into my device settings and perform a 'clear data' on my app and re-launch it, once again it returns 'false'.

Obviously this is worrying as I am showing my own custom consent dialog to EEA/Swizerland users than I am the ROW. And this needs to happen on first launch.

Interestingly, I tried putting the call to isRequestLocationInEeaOrUnknown() in my AsyncTask' doInBackground method (I kick this ASync off in onCreate) and then it does return 'true' on first-launch as do calls to it made in the ASync's 'onPostExecute' method. It's just that calls made to it in onCreate do not (before or after the Async starts).

I know it's early days, but has anyone stumbled upon similar issues with this?


回答1:


You must call isRequestLocationInEeaOrUnknown() after onConsentInfoUpdated callback is called.

This value is retrieved asynchronously by requestConsentInfoUpdate(), so it is not correct at first launch, but it is then cached so on second launch you have correct value.




回答2:


I face the same, some countries are correct, some aren't. I tried "https://adservice.google.com/getconfig/pubvendors" too




回答3:


Since i faced the same issue, and the docks are a bit confusing I will try to explain how it works.

add this for testing only remove them in production and make sure you add them before you request consent

ConsentInformation.getInstance(this@MainActivity).addTestDevice("DEVICE_ID") // add your device id for testing
ConsentInformation.getInstance(this@MainActivity).debugGeography = DebugGeography.DEBUG_GEOGRAPHY_EEA // test if user in EEA
ConsentInformation.getInstance(this@MainActivity).consentStatus = ConsentStatus.UNKNOWN // this sets the state to unknown, useful to reset the consent state in between tests careful to remove this if you want to see the flow for a returning user 

Request consent status like this:

val consentInformation = ConsentInformation.getInstance(this@MainActivity)
        val publisherIds = arrayOf(ADMOB_PUBLISHER_ID)
        consentInformation.requestConsentInfoUpdate(publisherIds, object: ConsentInfoUpdateListener {
            override fun onFailedToUpdateConsentInfo(reason: String?) {
                // consent request failed so probably you sould display non personalized ads
                log("MAIN ACTIVITY", "FAILED TO UPDATE CONSENT SHOW NOT PERSONALIZED")
                initializeAds(NON_PERSONALIZED)
                // YOU COULD RETRY HERE OR IT WILL RETRY ON NEXT SESSION 
            }

            override fun onConsentInfoUpdated(consentStatus: ConsentStatus?) {
                when (consentStatus) {
                    ConsentStatus.PERSONALIZED -> {
                        log("MAIN ACTIVITY", "USER OPTED FOR PERSONALIZED")
                        // USER ALREADY GAVE HIS CONSENT FOR YOUR PUBLISHER ID SO YOU CAN DISPLAY PERSONALIZED ADS
                        initializeAds(PERSONALIZED)
                    }
                    ConsentStatus.NON_PERSONALIZED -> {
                        log("MAIN ACTIVITY", "USER OPTED FOR NON_PERSONALIZED")
                        // USER OPTED FOR NON_PERSONALIZED ADS SO INCLUDE THAT IN YOUR ADD REQUEST                            
                        initializeAds(NON_PERSONALIZED)
                    }
                    ConsentStatus.UNKNOWN -> {
                        log("MAIN ACTIVITY", "USER CONSENT STATUS IS UNKNOWN ")
                        // USER WAS NEVER PROMPTED TO GIVE HIS CONSENT (DEFAULT STATE FOR ALL USERS) 
                        if (consentInformation.isRequestLocationInEeaOrUnknown) {
                            log("MAIN ACTIVITY", "USER IS IN EEA REQUEST CONSENT ")
                            // USER IS IN THE EEA AREA SO WE NEED TO REQUEST HIS CONSENT (WE SHOW THE PROMPT HERE) YOU SHOULD UPDATE CONSENT WITH HIS OPTION SO THIS FUNCTION WILL NEVER GET CALLED AGAIN
                            requestConsentFromUser()  
                        } else {
                            log("MAIN ACTIVITY", "USER NOT IN EEA INITIALIZE ADS ")
                            // USER IS NOT IN EEA SO WE ARE NOT REQUIRED TO REQUEST CONSENT (YOU COULD STILL REQUEST IT IF YOU LIKE)
                            initializeAds(PERSONALIZED)
                        }
                    }
                }
            }
        })


来源:https://stackoverflow.com/questions/50487171/android-getting-users-location-using-admobs-consent-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!