admob-plus / admob-plus

Trustable AdMob Plugin for Cordova, Capacitor, Ionic, React Native
https://admob-plus.github.io
MIT License
361 stars 150 forks source link

Error on consent "Uncaught (in promise) Method must be call on main thread" #637

Closed merbin2012 closed 4 months ago

merbin2012 commented 5 months ago

I have use the following code

 if (cordova.platformId === 'ios') {
    const status = await consent.trackingAuthorizationStatus()
    /*
      trackingAuthorizationStatus:
      0 = notDetermined
      1 = restricted
      2 = denied
      3 = authorized
    */
    const statusNew = await consent.requestTrackingAuthorization()
  }

  const consentStatus = await consent.getConsentStatus()
  if (consentStatus === consent.ConsentStatus.Required) {
    await consent.requestInfoUpdate()
  }

  await consent.loadAndShowIfRequired()

  if (await consent.canRequestAds()) {
    // request ads

        _bannerAd = new admob.BannerAd({
            adUnitId: _adUnitId.bannerid
        });
.....

But it shows the following error "Uncaught (in promise) Method must be called on main thread." in this "await consent.loadAndShowIfRequired()" line.

image

EYALIN commented 5 months ago

i have it too. @ratson can you help us here?

merbin2012 commented 4 months ago

Thank you

undergroundcreative commented 4 months ago

At what point do you guys call await consent.canRequestAds() ?

I always get an error using:

try {
   canRequestAds = await consent.canRequestAds();
}
catch(e){
   console.log('canRequestAds error: ' + JSON.stringify(e));
}

Which logs:

canRequestAds error: {}

I call it after:

form = await consent.loadForm();
form.show();

I'm using:

admob-plus-cordova 2.0.0-alpha.14 and cordova-plugin-consent 2.4.0 UMP_VERSION 2.0.0

Everything else seems to work. It loads and shows the form correctly. Is there an event handler that lets you know when the user has chosen their options?

Thanks in advance. Sean

merbin2012 commented 4 months ago

@undergroundcreative You are using lower version of consent plugin, update it to solve the issue https://github.com/admob-plus/admob-plus/releases/tag/cordova-plugin-consent%403.0.0-alpha.6

undergroundcreative commented 4 months ago

Thanks for the reply. I can't even get it to show the form now though!

I updated to: admob-plus-cordova 3.0.0-alpha.6 and UMP_VERSION 2.1.0

I get no output from the consent plugin from this code or after:

try {
        consentStatus = await consent.getConsentStatus();
    }
    catch(e){
        console.log('getConsentStatus error: ' + JSON.stringify(e));
    }
console.log('consentStatus: ' + consentStatus);

With the plugin version I was previously using it would show the form etc.

This is my full code:

try {
    consentStatus = await consent.getConsentStatus();
}
catch(e){
    console.log('getConsentStatus error: ' + JSON.stringify(e));
}
console.log('consentStatus: ' + consentStatus);
console.log('consentStatus here 1');

let requestInfoUpdateResult;
try {
    requestInfoUpdateResult = await consent.requestInfoUpdate()
    console.log('requestInfoUpdateResult: ' + JSON.stringify(requestInfoUpdateResult));
}
catch(e){
    console.log('requestInfoUpdate error: ' + JSON.stringify(e));
}
console.log('consentStatus here 2');
console.log('consentStatus: ' + consentStatus);

let formStatus;

try {
    formStatus = await consent.getFormStatus();
}
catch(e){
    console.log('getFormStatus error: ' + JSON.stringify(e));
}
console.log('formStatus: ' + JSON.stringify(formStatus));

let canRequestAds;

if (formStatus === consent.FormStatus.Available && consentStatus !== consent.ConsentStatus.Obtained){
    console.log('about to load form');
    let form;
    try {
        console.log('about to load form 1');
        form = await consent.loadForm();
        console.log('about to load form 2');
        form.show();
        console.log('about to load form 3');
        try {
            canRequestAds = await consent.canRequestAds();
        }
        catch(e){
            console.log('canRequestAds error: ' + JSON.stringify(e));
        }
        console.log('canRequestAds: ' + canRequestAds);

        if (typeof canRequestAds == 'undefined'){
            canRequestAds = (consentStatus === consent.ConsentStatus.Obtained) ? true : false;
        }

        console.log('canRequestAds 3: ' + canRequestAds);
        return canRequestAds;
    }
    catch(e){
        console.log('loadForm error: ' + JSON.stringify(e));
    }

} else {
    canRequestAds = (consentStatus === consent.ConsentStatus.Obtained) ? true : false;
    return canRequestAds;
}
undergroundcreative commented 4 months ago

Hi guys, could you let me know which versions you're using of:

As I say, the code above worked up to await consent.canRequestAds() using:

admob-plus-cordova 2.0.0-alpha.14 and cordova-plugin-consent 2.4.0 UMP_VERSION 2.0.0 and cordova android ^11.0.0

When I switched to admob-plus-cordova 3.0.0-alpha.6 and UMP_VERSION 2.1.0

I get no further logs after I call await consent.getConsentStatus();

Could you also share any relevant code that would help me and others, as the deadline was yesterday?!

Many thanks,

Sean

undergroundcreative commented 4 months ago

Ah I've got it working!

Added this line to config.xml:

<preference name="GradlePluginKotlinEnabled" value="true" />

..as per:

https://github.com/admob-plus/admob-plus/issues/615#issuecomment-1730163668

I also updated to cordova android ^12.0.0

I'm getting the consent form again and consent.canRequestAds() doesn't result in an error.

Now when I click the Consent button on the form, close the app and reopen, I get the form popping up again asking for consent. This can't be right. My code is above. How do I detect if the user has already given or rejected consent?

Is there an event to catch what options the user selected?

Thanks for all your help! Not far away now, hopefully :)

Sean

1n3JgKl9pQ6cUMrW commented 3 months ago

Now when I click the Consent button on the form, close the app and reopen, I get the form popping up again asking for consent. This can't be right. My code is above. How do I detect if the user has already given or rejected consent?

@undergroundcreative

This is a well known bug / feature and can be solved by using a 2nd plugin (Android only).

See - https://github.com/admob-plus/admob-plus/issues/631#issuecomment-1834293370

And in more detail - https://github.com/admob-plus/admob-plus/issues/601#issuecomment-1702578477

undergroundcreative commented 3 months ago

Thanks @1n3JgKl9pQ6cUMrW , I've been following the issue on #601 - all your input is much appreciated.

I did see that 2nd plugin but as I'm using Ionic Appflow to build my app, I don't know how I'd get it to install. I see you had to download and unzip it in order to install it, but I don't know how that could work with Appflow. Any ideas?

Many thanks again :)

1n3JgKl9pQ6cUMrW commented 3 months ago

You can install every plug-in you want, locally.

Just download the .zip from Git or another source and point to the unzipped folder when installing.

Cordova has a manual for this in the official docs, and the same if for Ionic, I think;

https://stackoverflow.com/questions/45976858/unable-to-add-a-cordova-plugin-from-local-folder

undergroundcreative commented 3 months ago

Appflow is a remote build tool though, so I don't think the instructions in that link would apply?

https://ionic.io/appflow