App open ads
startAppOpenAds shows an app open ad each time the app returns from the background, following Google's guidance:
- The ad is preloaded with
AdPreloaderand replaced before it expires (app open ads last four hours). - Nothing shows on a cold start.
- Nothing shows over another full-screen ad from this package.
minimumIntervalcaps how often ads show.
import { Admob, startAppOpenAds } from 'nitro-ui-admob'
const stopAppOpenAds = startAppOpenAds({
adUnitId: Admob.testAdUnitId('appOpen'),
minimumInterval: 30 * 60 * 1000, // at most every 30 minutes
onWillShow: (ad) => {
ad.onAdDismissed = () => console.log('App open ad dismissed')
},
onLoadError: (error) => console.warn(error),
})
// Later, for example when the user buys an ad-free upgrade:
stopAppOpenAds()
Options
| Option | Type | Description |
|---|---|---|
adUnitId | string | The AdMob app open ad unit ID. |
requestOptions | AdmobRequestOptions | Targeting for the ad requests. |
minimumInterval | number | Minimum time between two app open ads, in milliseconds. Default 0. |
onWillShow | (ad: AppOpenAd) => void | Called with each ad right before it is shown, to set its callbacks. |
onLoadError | (error: unknown) => void | Called with each failed load. Loads are retried with backoff. |
When an ad shows
On each return from the background (Admob.addOnAppForegroundListener):
- If a full-screen ad is on screen, or less than
minimumIntervalpassed since the last app open ad, nothing happens. - If no ad is ready, nothing happens; the preloader keeps loading.
- Otherwise
onWillShow(ad)runs, then the ad is shown, and the next one starts loading.
Build your own
startAppOpenAds is a small composition of public APIs. For different rules, combine them yourself:
import { AdPreloader, Admob, AppOpenAd } from 'nitro-ui-admob'
const preloader = new AdPreloader(() => AppOpenAd.load(adUnitId))
preloader.start()
const subscription = Admob.addOnAppForegroundListener(() => {
if (Admob.isShowingFullScreenAd || !shouldShowAppOpenAd()) return
preloader.take()?.show()
})
// Cleanup:
subscription.remove()
preloader.stop()