Android Ошибка AdMob в методе onConsentFormError - PullRequest
0 голосов
/ 03 мая 2020

Я пытаюсь внедрить Consent SDK в моем приложении android. Я создал вспомогательный класс в соответствии с этим обсуждением . Каждый раз, когда я запускаю приложение, оно выдает ошибку, хотя в методе public void onConsentFormError(String errorDescription)

Ошибка: Error: no information available. Successful call to com.google.ads.consent.ConsentInformation.requestConsentInfoUpdate() required before using this form. Кто-нибудь знает, что я делаю неправильно?

public class GdprHelper {

    private Context appContext;
    private SharedPreferences sharedPreferences;

    private static final String[] PUBLISHER_ID = {"pub-5473288728312930"};
    private static final String PRIVACY_URL = "https://sites.google.com/";
    private static final String PAID_VERSION_URL = "market://details?id=com.google.android.youtube";
    private ConsentForm consentForm;

    public GdprHelper(Context appContext, SharedPreferences sharedPreferences) {
        this.appContext = appContext;
        this.sharedPreferences = sharedPreferences;
    }

    public void initialize() {
        final ConsentInformation consentInformation = ConsentInformation.getInstance(appContext);

        // todo delete ===========
        consentInformation.setDebugGeography(DebugGeography.DEBUG_GEOGRAPHY_EEA);
        // todo delete ===========


        consentInformation.requestConsentInfoUpdate(PUBLISHER_ID, new ConsentInfoUpdateListener() {
            @Override
            public void onConsentInfoUpdated(ConsentStatus consentStatus) {
                // User's consent status successfully updated.
                if (consentStatus == ConsentStatus.UNKNOWN
                        && ConsentInformation.getInstance(appContext).isRequestLocationInEeaOrUnknown()) {
                    displayConsentForm();
                }
            }

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

    public void resetConsent() {
        ConsentInformation consentInformation = ConsentInformation.getInstance(appContext);
        consentInformation.reset();
    }

    private void displayConsentForm() {
        consentForm = new ConsentForm.Builder(appContext, getPrivacyUrl())
                .withListener(new ConsentFormListener() {
                    @Override
                    public void onConsentFormLoaded() {
                        consentForm.show();
                    }

                    @Override
                    public void onConsentFormOpened() {
                        // Consent form was displayed.
                    }

                    @Override
                    public void onConsentFormClosed(
                            ConsentStatus consentStatus, Boolean userPrefersAdFree) {

                        // Consent form was closed. This callback method contains all the data about user's selection, that you can use.
                        SharedPreferences.Editor editor = sharedPreferences.edit();
                        if (userPrefersAdFree) {
                            showPaidVersion();
                        } else if (consentStatus == ConsentStatus.NON_PERSONALIZED) {
                            editor.putInt(SP_EEA_NPA, 1);
                            editor.putBoolean(SP_EEA_UNKNOWN, false);
                        } else if (consentStatus == ConsentStatus.UNKNOWN) {
                            editor.putInt(SP_EEA_NPA, 1);
                            editor.putBoolean(SP_EEA_UNKNOWN, true);
                        } else {
                            editor.putInt(SP_EEA_NPA, 0);
                            editor.putBoolean(SP_EEA_UNKNOWN, false);
                        }
                        editor.commit();
                    }

                    @Override
                    public void onConsentFormError(String errorDescription) {
                        // Consent form error. Would be nice to have some proper logging
                        if (BuildConfig.BUILD_TYPE.equals("debug")) {
                            Toast.makeText(appContext, errorDescription, Toast.LENGTH_LONG).show();
                            save(appContext, errorDescription);
                            System.out.println("error consent:");
                            System.out.println(errorDescription);
                        }
                    }
                })
                .withPersonalizedAdsOption()
                .withNonPersonalizedAdsOption()
                .withAdFreeOption()
                .build();
        consentForm.load();
    }

    private URL getPrivacyUrl() {
        URL privacyUrl = null;
        try {
            privacyUrl = new URL(PRIVACY_URL);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return privacyUrl;
    }

    private void showPaidVersion() {
        Intent i = new Intent(
                Intent.ACTION_VIEW, Uri.parse(PAID_VERSION_URL));
        appContext.startActivity(i);
    }
}
...