Interstitial Ads
插页广告是一种全屏广告,会覆盖主应用的所有界面。这些广告可以在用户进行不同操作或切换游戏关卡时自动出现。用户可以点击跳过这些广告,或者直接返回到应用中。Google 为Android 系统和iOS 系统设计的插页广告模板详细说明了其表现形式。
在用户不应获得应用内奖励的情况下,应使用此类广告。请在初始化和同意流程之后调用此函数。提前准备好广告内容,先注册监听器,只有当广告内容准备就绪时才会显示它。
import { AdLoadInfo, AdMob, AdMobRevenueData, AdOptions, InterstitialAdPluginEvents } from '@capacitor-community/admob';
await AdMob.addListener(InterstitialAdPluginEvents.Loaded, (info: AdLoadInfo) => {
console.log('Interstitial loaded', info.adUnitId);
});
await AdMob.addListener(InterstitialAdPluginEvents.FailedToLoad, console.error);
await AdMob.addListener(InterstitialAdPluginEvents.AdImpression, (data: AdMobRevenueData) => {
console.log(data);
});
const options: AdOptions = {
adId: 'YOUR_AD_UNIT_ID',
// isTesting: true,
// npa: true,
// immersiveMode: true,
};
const { adUnitId } = await AdMob.prepareInterstitial(options);
await AdMob.showInterstitial({ adId: adUnitId });
method prepareInterstitial(...)
加载插屏广告并返回已加载的广告单元 ID。如果 SDK 加载失败,Promise 会被拒绝,并返回原生错误消息和字符串形式的 code。错误码因平台而异,FailedToLoad 事件会以数值形式提供同一错误码。
prepareInterstitial(options: AdOptions) => Promise<AdLoadInfo>
method showInterstitial(...)
展示已加载的插屏广告。
showInterstitial(options?: AdShowOptions | undefined) => Promise<void>
interface AdOptions
请求广告的通用选项。
| 属性 | 类型 | 说明 | 默认值 | 起始版本 |
|---|---|---|---|---|
adId |
string |
要加载的广告单元 ID。 | 1.1.2 | |
isTesting |
boolean |
是否请求测试广告。 | false | 1.1.2 |
margin |
number |
以屏幕逻辑单位表示的横幅边距(Android 上为 dp,iOS 上为点)。对于 BOTTOM_CENTER,该值为下边距;对于 TOP_CENTER,则为上边距。 |
0 | 1.1.2 |
npa |
boolean |
是否请求非个性化广告。 | false | 1.2.0 |
immersiveMode |
boolean |
在 Android 上是否以沉浸模式展示全屏广告。 | 7.0.3 |
当没有传递adId参数给showInterstitial()函数时,将会显示最近准备好的广告。
准备多个广告
await AdMob.prepareInterstitial({ adId: 'ca-app-pub-xxx/interstitial-1' });
await AdMob.prepareInterstitial({ adId: 'ca-app-pub-xxx/interstitial-2' });
await AdMob.showInterstitial({ adId: 'ca-app-pub-xxx/interstitial-1' });
关于isTesting的相关信息,请参见测试部分。
加载、显示、关闭和失败事件的完整列表见广告事件。
已准备好的插页式广告会在一小时后过期。广告过期后调用 showInterstitial() 会收到 AD_EXPIRED 错误。请先准备新广告再重试。
Android 沉浸模式
在prepareInterstitial()方法中设置immersiveMode: true,这样在广告显示时就可以隐藏 Android 系统的各种条带。不过,仅仅隐藏应用界面上的条带并不能实现 Google Mobile Ads SDK 所要求的独立广告展示效果。
await AdMob.prepareInterstitial({ adId: 'YOUR_AD_UNIT_ID', immersiveMode: true });
await AdMob.showInterstitial();
这一点同样适用于prepareRewardVideoAd()方法。在沉浸式模式下,系统栏由用户自行控制;广告的宽高比以及任何相关的边框设置则由 SDK 和创意文件来决定。用户仍然可以通过滑动屏幕来显示那些短暂出现的系统栏。