MobileChromeApps / mobile-chrome-apps

Chrome apps on Android and iOS
BSD 3-Clause "New" or "Revised" License
2.5k stars 347 forks source link

event not fire, and have to wait before call plugin API #452

Open floatinghotpot opened 9 years ago

floatinghotpot commented 9 years ago

I am trying to use cordova plugin in mobile chrome app, but encounter some problems. I have to wait a few seconds before calling plugin API, or else it won't work.

as mentioned in answer to other issue, the doc is loaded and deviceready is fired before background.js is loaded. why I have to wait before call plugin API?

function initAd() {
    if(AdMob) AdMob.createBanner({ adId: admobid.banner, autoShow:true, isTesting:true });
}

function deviceReady() {
    if(! AdMob) {
    alert('admob plugin not ready');
        return;
    }

    // if call the plugin API immediately, it won't work, if delay 3000ms, it works, why?
    // initAd();

    window.setTimeout(initAd, 3000);
}

function onAppLoad() {
    if(typeof window.cordova !== 'undefined') { // for mobile chrome app
        deviceReady();

    } else if ((/(ipad|iphone|ipod|android)/i.test(navigator.userAgent))) { // for normal cordova app
        document.addEventListener('deviceready', deviceReady, false);

    } else { // for test in pc browser
        deviceReady();
    }
}

$(document).ready(onAppLoad);
//document.addEventListener('DOMContentLoaded', onAppLoad);
//onAppLoad();
agrieve commented 9 years ago

Hmm, few things going on here it seems. For mobile chrome app, you don't need to listen for deviceready. Instead, you should listen for load or DOMContentLoaded. I see you've tried that in your commented out code though. Did the event never fire, or was something else going on with it?

floatinghotpot commented 9 years ago

I've fixed the event fire issue by removing <script src='cordova.js'></script> from html, as I read that it's injected automatically by cca. Now the doc ready event is fired properly.

Another issue still exists. I have to call window.setTimeout() before call the plugin API, or else the native code won't be called.

Rayz100 commented 9 years ago

Hi guys, I have just downloaded your code 'floatinghotpot/cordova-plugin-admob'. I am using Intel XDK and have just finished creating my first app. I want to add AdMob on my app and your code requires an external cordova.js Do you have that cordova.js please mate? Thanks in Advance

floatinghotpot commented 9 years ago

@Rayz100, as your question not related with CCA and what I ask here, could you please create another issue or question, on my project issue tracker page? Let's discuss your question there.

Please have a look at the wiki page first. https://github.com/floatinghotpot/cordova-admob-pro/wiki

Rayz100 commented 9 years ago

Hi Ray, Sorry mate I am new on github so I have no idea how to create a new issue or question on your project issue tracker page, please send me a link

floatinghotpot commented 9 years ago

here it is: https://github.com/floatinghotpot/cordova-admob-pro/issues

Rayz100 commented 9 years ago

Tanks a lot, have posted my issue

On Wed, Dec 3, 2014 at 8:28 AM, Raymond Xie notifications@github.com wrote:

here it is: https://github.com/floatinghotpot/cordova-admob-pro/issues

— Reply to this email directly or view it on GitHub https://github.com/MobileChromeApps/mobile-chrome-apps/issues/452#issuecomment-65370574 .

R.Otto Bsc (Hons)