Search code examples
nativescriptnativescript-plugin

Nativescript firebase admob not firing some events after the first time on ios


In my code I have these functions:

const preloadRewardAd = function() {
  console.log('preload reward ad');
  firebase.admob.preloadRewardedVideoAd({
    iosAdPlacementId: 'xxx',
    androidAdPlacementId: 'yyy',
    testing: config.adMobTesting,
    iosTestDeviceIds: config.iosTestDeviceIds
  }).then(
      function() {
        console.log('reward ad ready');
      }
  );
};

const showRewardAd = function(card: Card) {
  console.log('show reward ad');
  return firebase.admob.showRewardedVideoAd({
    onRewarded: (reward)  => {
      console.log('onRewarded called with amount ' + reward.amount);
      console.log('onRewarded called with type ' + reward.type);
      card.fetch().then(() => {
        card.notifyPropertyChange('id', card.id);
      });
    },
    onOpened: () => console.log('onOpened'),
    onClosed: () => {
      preloadRewardAd();
      console.log('onClosed');
    },
    onStarted: () => console.log('onStarted'),
    onCompleted: () => console.log('onCompleted'),
    onLeftApplication: () => console.log('onLeftApplication')
  }).catch(err => {
    console.log(err);
  });
};

export function newCard(args: EventData) {
  console.log('tap new card');
  const pager = <Pager>page.getViewById('cards-carousel');

  const card = <Card>pager.get('items').getItem(pager.selectedIndex);

  showRewardAd(card);
}

I then call preLoadRewardAd() in page.loaded.

It works as expected on Android: when the user closes the ad it pre-loads the next one.

On ios it works normally on the first ad display. But on the second one onRewarded is never called, and even though onClosed gets called, it never pre-loads another ad, and the whole thing falls apart. onCompleted is also not called.

Here is the sequence of output from the console:

  1. page loaded
  2. preload reward ad
  3. reward ad ready
  4. tap new card
  5. show reward ad
  6. onOpened
  7. onStarted
  8. onRewarded called with amount 1
  9. onRewarded called with type Card
  10. preload reward ad
  11. onClosed
  12. reward ad ready
  13. tap new card
  14. show reward ad

at this point I wait until the ad completes. Wait several more seconds to be sure. I close the ad. 10 & 11 may seem out of order but it's just the placement of my console.log calls.

  • onOpened is never fired.
  • onStarted is never fired.
  • onRewarded is never fired.
  • onCompleted is never fired.
  • onClosed is never fired.
  • no error is output from the catch block.

Is there something I'm missing?


Solution

  • I found a workaround that is "good enough". In the onClosed event I wrapped the call to preloadRewardAd in a setTimeout after 250ms. This seems to give ios enough time to catch up.