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

久未见 提交于 2019-12-04 12:54:51

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.

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

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