Skip to content

原生广告(模版\自渲染\draw)
Native ads (template\self-rendering\draw)

注意: 原生广告具体实现可参考Demo中NativeAdUnifiedActivity、NativeAdUnifiedListActivity、NativeAdUnifiedRecycleActivity等

Note: For the specific implementation of native advertising, please refer to NativeAdUnifiedActivity, NativeAdUnifiedListActivity, NativeAdUnifiedRecycleActivity, etc. in Demo

自渲染广告: 聚合SDK返回物料,建议由开发者根据自己的广告设计自行UI适配和展示。

Self-rendering ads: ToBid SDK returns materials. It is recommended that developers adapt and display the UI according to their own ad design.

1. 主要API
Main API

1.1 WMNativeAd

  • com.windmill.sdk.natives.WMNativeAd
方法名
Method
方法介绍
Method Introduction
WMNativeAd(Context context, WMNativeAdRequest request)构造方法。参数说明:activity(展示广告的 activity)、request(广告请求对象)。
Constructor. Parameter description: activity (activity to display ads), request (ad request object).
loadAd(NativeAdLoadListener nativeAdLoadListener)请求广告。参数说明:nativeAdLoadListener(请求广告相关状态会通过 nativeAdLoadListener 通知开发者)。
Request ads. Parameter description: nativeAdLoadListener (the status of requesting ads will be notified to the developer through nativeAdLoadListener).
List getNativeADDataList()广告拉取成功后,获取广告集合。
After the advertisement is successfully pulled, the advertisement collection is obtained.
checkValidAdCaches()查询当前广告位的所有缓存信息的AdInfo对象、在广告加载成功后调用。
Query the AdInfo object of all cached information of the current ad slot and call it after the ad is loaded successfully.
destroy()销毁广告。
Destroy ads.

1.2 WMNativeAdRequest

  • com.windmill.sdk.natives.WMNativeAdRequest
方法名
Method
方法介绍
Method Introduction
WMNativeAdRequest(String placementId, String userId, int adCount, Map options)构造方法。参数说明:placementId(广告位Id)、userId(用户Id可选)、adCount(请求广告数量)、options(扩展参数,可传任意)。
Constructor. Parameter description: placementId (ad slot ID), userId (user ID is optional), adCount (number of requested ads), options (extended parameters, any parameters can be passed).

1.3 NativeAdLoadListener

  • com.windmill.sdk.natives.WMNativeAd$NativeAdLoadListener
方法名
Method
方法介绍
Method Introduction
onFeedAdLoad(String placementId)广告成功加载。参数说明:placementId(广告位Id)。
The ad was loaded successfully. Parameter description: placementId (ad slot Id).
onError(WindMillError error, String placementId)广告加载失败。参数说明:error(报错信息,具体可看其内部code和message)、placementId(报错的广告位Id)。
Ad loading failed. Parameter description: error (error information, see its internal code and message for details), placementId (the ID of the ad position where the error was reported).

1.4 WMNativeAdData

  • com.windmill.sdk.natives.WMNativeAdData
方法名
Method
方法介绍
Method Introduction
getAppInfo()获取下载类广告六要素信息,媒体可根据需要自行展示。
Obtain the six elements of download ads, and media can display them as needed.
getInteractionType()获取广告交互类型。取值范围:WMConstants.INTERACTION_TYPE_DOWNLOAD、WMConstants.INTERACTION_TYPE_BROWSER、WMConstants.INTERACTION_TYPE_UNKNOWN
Gets the ad interaction type. Value range: WMConstants.INTERACTION_TYPE_DOWNLOAD, WMConstants.INTERACTION_TYPE_BROWSER, WMConstants.INTERACTION_TYPE_UNKNOWN
getAdPatternType()获取广告样式。取值范围:WMNativeAdDataType.NATIVE_UNKNOWN、WMNativeAdDataType.NATIVE_SMALL_IMAGE_AD、WMNativeAdDataType.NATIVE_BIG_IMAGE_AD、WMNativeAdDataType.NATIVE_GROUP_IMAGE_AD、WMNativeAdDataType.NATIVE_VIDEO_AD
Gets the ad style. Value range: WMNativeAdDataType.NATIVE_UNKNOWN, WMNativeAdDataType.NATIVE_SMALL_IMAGE_AD, WMNativeAdDataType.NATIVE_BIG_IMAGE_AD, WMNativeAdDataType.NATIVE_GROUP_IMAGE_AD, WMNativeAdDataType.NATIVE_VIDEO_AD
getExpressAdView()获取模版渲染的广告View
Get the ad View rendered by the template
getCTAText()获取创意按钮文案
Obtain Creative Button Text
getTitle()获取广告的Tittle
Get the title of the ad
getDesc()获取广告的描述
Get the description of the ad
getAdLogo()获取广告的Logo
Get the ad logo
getIconUrl()获取广告的Icon
Get the ad icon
getImageUrlList()获取广告的图片地址,已废弃,建议用getImageList
Get the image address of the advertisement. It is deprecated. It is recommended to use getImageList
getImageList()获取广告的图片信息集合:宽、高、url等
Get the image information set of the advertisement: width, height, url, etc.
getVideoCoverImage()获取视频广告图片信息:宽、高、url等
Get video ad image information: width, height, url, etc.
getCustomizeVideo()获取自定义视频信息:url等
Get custom video information: URL, etc.
startVideo()播放视频广告
Play video ads
pauseVideo()暂停视频广告
Pause video ad
resumeVideo()恢复视频广告
Resume video ads
stopVideo()停止视频广告
Stop Video Ads
render()广告渲染方法
Ad rendering method
isExpressAd()是否为模版渲染广告
Whether to render ads for templates
isNativeDrawAd()是否为原生Draw广告
Is it a Draw ad?
destroy()销毁广告单元对象
Destroy ad
connectAdToView(Activity activity, WMNativeAdContainer adContainer, WMNativeAdRender adRender)自渲染具体实现方法。参数说明:activity(展示广告的 activity)、adContainer(ToBid提供的根容器)、adRender(广告渲染实现类对象)。
Specific implementation method of self-rendering. Parameter description: activity (activity that displays ads), adContainer (root container provided by ToBid), adRender (ad rendering implementation class object).
bindImageViews(Context context, List imageViews, int defaultImageRes)图片渲染绑定方法。参数说明:context(展示广告的 上下文)、imageViews(需要渲染的ImageView集合)、defaultImageRes(图片渲染失败默认展示的资源)。
Image rendering binding method. Parameter description: context (context for displaying ads), imageViews (the ImageView collection that needs to be rendered), defaultImageRes (the default resource to be displayed if image rendering fails).
bindViewForInteraction(Context context, View view, List clickableViews, List creativeViewList, View disLikeView)绑定广告交互的方法。参数说明:context(展示广告的 上下文)、clickableViews(可点击的View的列表)、creativeViewList(用于下载或者拨打电话的创意View集合)、disLikeView(dislike按钮View)。
Method for binding ad interactions. Parameter description: context (context for displaying ads), clickableViews (list of clickable Views), creativeViewList (collection of creative Views for downloading or making calls), disLikeView (dislike button View).
setInteractionListener(NativeAdInteractionListener adInteractionListener)设置广告交互监听。参数说明:adInteractionListener(广告交互监听回调对象)。
Set ad interaction listener. Parameter description: adInteractionListener (ad interaction listener callback object).
void setMediaViewOption(WMVideoOption videoOption)设置视频播放参数,在bindMediaView之前调用。
Set video playback parameters and call before bindMediaView.
void bindMediaView(Context context, ViewGroup mediaLayout)绑定视频Video方法。参数说明:context(展示广告的 上下文)、mediaLayout(装video的容器)。
Bind video Video method. Parameter description: context (context for displaying ads), mediaLayout (container for video).
setMediaListener(NativeADMediaListener nativeADMediaListener)设置自渲染Video监听。参数说明:nativeADMediaListener(视频播放监听回调对象)。
Set the self-rendering Video listener. Parameter description: nativeADMediaListener (video playback listener callback object).
setDislikeInteractionCallback(Activity activity, DislikeInteractionCallback dislikeInteractionCallback)设置Dislike监听。参数说明:activity(dislike弹窗的acticity)、dislikeInteractionCallback(dislike监听回调对象)。
Set Dislike listener. Parameter description: activity (activity of the dislike pop-up window), dislikeInteractionCallback (dislike listener callback object).
setDownloadListener(AppDownloadListener appDownloadListener)设置广告下载监听。参数说明:appDownloadListener(广告下载监听回调对象)。
Set the ad download listener. Parameter description: appDownloadListener (ad download listener callback object).

1.5 NativeAdInteractionListener

  • com.windmill.sdk.natives.WMNativeAdData$NativeAdInteractionListener
方法名
Method
方法介绍
Method Introduction
onADExposed(AdInfo adInfo)广告曝光。参数说明:adInfo(广告信息,具体可看其内部成员变量)。
Advertisement exposure. Parameter description: adInfo (advertisement information, see its internal member variables for details)
onADError(AdInfo adInfo, WindMillError error)广告展示失败。参数说明:adInfo(广告信息,具体可看其内部成员变量)、error(报错信息,具体可看其内部code和message)。
Ad display failed. Parameter description: adInfo (ad information, see its internal member variables for details), error (error information, see its internal code and message for details).
onADRenderSuccess(AdInfo adInfo, View view, float width, float height)广告渲染成功。参数说明:adInfo(广告信息,具体可看其内部成员变量)、view(模版渲染的广告view)、width(view宽度)、height(view高度)。
Ad rendering is successful. Parameter description: adInfo (ad information, see its internal member variables for details), view (ad view rendered by template), width (view width), height (view height).
onADClicked(AdInfo adInfo)广告点击。参数说明:adInfo(广告信息,具体可看其内部成员变量)。
Advertisement click. Parameter description: adInfo (advertisement information, see its internal member variables for details).

1.6 NativeADMediaListener

  • com.windmill.sdk.natives.WMNativeAdData$NativeADMediaListener
方法名
Method
方法介绍
Method Introduction
onVideoLoad()视频加载成功。
Video loaded successfully.
onVideoError(WindMillError error)视频播放失败。参数说明:error(报错信息,具体可看其内部code和message)。
Video playback failed. Parameter description: error (error information, see its internal code and message for details).
onVideoStart()视频开始播放。
The video starts playing.
onVideoPause()视频暂停播放。
The video is paused.
onVideoResume()视频恢复播放。
The video resumes playing.
onVideoCompleted()视频完成播放。
The video has finished playing.

1.7 AppDownloadListener

  • com.windmill.sdk.natives.WMNativeAdData$AppDownloadListener
方法名
Method
方法介绍
Method Introduction
onIdle()下载静止状态。
Download static state
onDownloadActive(long totalBytes, long currBytes, String fileName, String appName)下载活动状态。参数说明:totalBytes(下载总字节)、currBytes(当前下载字节)、fileName(下载文件名)、appName(下载App名字)。
Download activity status. Parameter description: totalBytes (total bytes downloaded), currBytes (current download bytes), fileName (download file name), appName (download App name).
onDownloadPaused(long totalBytes, long currBytes, String fileName, String appName)下载暂停状态。参数说明:totalBytes(下载总字节)、currBytes(当前下载字节)、fileName(下载文件名)、appName(下载App名字)。
Download pause status. Parameter description: totalBytes (total bytes downloaded), currBytes (current download bytes), fileName (download file name), appName (download App name).
onDownloadFailed(long totalBytes, long currBytes, String fileName, String appName)下载失败。参数说明:totalBytes(下载总字节)、currBytes(当前下载字节)、fileName(下载文件名)、appName(下载App名字)。
Download failed. Parameter description: totalBytes (total bytes downloaded), currBytes (current download bytes), fileName (download file name), appName (download App name).
onDownloadFinished(long totalBytes, String fileName, String appName)下载结束。参数说明:totalBytes(下载总字节)、fileName(下载文件名)、appName(下载App名字)。
Download is complete. Parameter description: totalBytes (total bytes downloaded), fileName (download file name), appName (download App name).
onInstalled(String fileName, String appName)安装完成。参数说明:fileName(安装文件名)、appName(安装App名字)。
Installation is complete. Parameter description: fileName (installation file name), appName (installation App name).

1.8 DislikeInteractionCallback

  • com.windmill.sdk.natives.WMNativeAdData$DislikeInteractionCallback
方法名
Method
方法介绍
Method Introduction
onShow()Dislike弹窗显示。
A Dislike pop-up window appears.
onSelected(int position, String value, boolean enforce)Dislike弹窗Item点击。参数说明:position(点击的位置)、value(点击的位置的文字选项)、enforce(是否关闭广告)。
Dislike pop-up window item click. Parameter description: position (clicked position), value (text option of the clicked position), enforce (whether to close the ad).
onCancel()Dislike弹窗取消。
Dislike pop-up window is cancelled.

1.9 安卓下载应用六要素信息
Six elements of information for Android app downloads

由于国内监管部门要求,在安卓下载类广告中,需要给用户披露下载的应用的六要素信息。开发者可以通过getAppInfo() 方法获取。六要素信息包括
Due to the requirements of domestic regulatory authorities, Android download ads need to disclose six elements of information about the downloaded app to users. Developers can getAppInfo() obtain this information through methods. The six elements include:

信息
information
字段
Fields
支持的广告网络
Supported ad networks
应用名称
Application Name
getAppName()Sigmob, Tencent , Baidu , Kuaishou, CSJ, Taptap
开发者名称
Developer Name
getDeveloperName()Sigmob, Tencent , Baidu , Kuaishou, CSJ, Taptap
应用版本
Application version
getAppVersion()Sigmob, Tencent , Baidu , Kuaishou, CSJ, Taptap
隐私协议
Privacy Policy
getPrivacyUrl()Sigmob, Tencent , Baidu , Kuaishou, CSJ
权限列表
Permission List
getPermissionInfoUrl()Sigmob, Tencent , Baidu , Kuaishou, CSJ
应用功能介绍
Application Function Introduction
getFunctionDescUrl()Sigmob, Tencent , Baidu , CSJ, Taptap

2. 接入代码示例
Code example

2.1 原生广告加载
Native Ad Loading

java
private WMNativeAd nativeAd;

private List<WMNativeAdData> nativeAdDataList;

//请求广告时可以传入期望的宽高
Map<String, Object> options = new HashMap<>();
options.put(WMConstants.AD_WIDTH, adWidth);//针对于模版广告有效、单位dp
options.put(WMConstants.AD_HEIGHT, WMConstants.AUTO_SIZE);//默认自适应高度、单位dp

/**
 * PLACEMENT_ID 必填
 * USER_ID 可选
 * AD_COUNT 必填 1-3
 * OPTIONS 自定义参数 可选
 */
WMNativeAdRequest request = new WMNativeAdRequest(PLACEMENT_ID, USER_ID, AD_COUNT, options);

nativeAd = new WMNativeAd(this, request);

nativeAd.loadAd(new WMNativeAd.NativeAdLoadListener() {
    @Override
    public void onError(WindMillError error, String placementId) {
        Log.d("lance", "onError:" + error.toString() + ":" + placementId);
    }

    @Override
    public void onFeedAdLoad(String placementId) {
        List<WMNativeAdData> unifiedADData = nativeAd.getNativeADDataList();
        if (unifiedADData != null && unifiedADData.size() > 0) {
            nativeAdDataList = unifiedADData;
        }
    }
});

2.2 原生广告展示
Native Ad Display

java

private ViewGroup adContainer;

/**
 * 请在收到onFeedAdLoad回调后再展示广告
 * nativeAd.getNativeADDataList()获取广告
 */
List<WMNativeAdData> nativeAdDataList = nativeAd.getNativeADDataList();
if (nativeAdDataList != null && nativeAdDataList.size() > 0) {
    WMNativeAdData nativeAdData = nativeAdDataList.get(0);
    //绑定广告的监听
    bindListener(nativeAdData);
    if (nativeAdData.isExpressAd()) {//模版广告
        nativeAdData.render();
        View expressAdView = nativeAdData.getExpressAdView();
        //媒体最终将要展示广告的容器(这里可以直接先addView,也可以在收到onRenderSuccess回调后addView)
        if (adContainer != null) {
            adContainer.removeAllViews();
            adContainer.addView(expressAdView);
        }
    } else {//自渲染广告
        //创建一个装整个自渲染广告的容器
        WMNativeAdContainer windContainer = new WMNativeAdContainer(this);
        //媒体自渲染的View
        NativeAdDemoRender adRender = new NativeAdDemoRender();
        //将容器和view链接起来
        nativeAdData.connectAdToView(this, windContainer, adRender);
        //媒体最终将要展示广告的容器
        if (adContainer != null) {
            adContainer.removeAllViews();
            adContainer.addView(windContainer);
        }
    }
}

2.3 原生广告销毁
Native Ad Destruction

java
//原生广告单元的销毁
if (nativeAdDataList != null && nativeAdDataList.size() > 0) {
    for (WMNativeAdData adData : nativeAdDataList) {
        if (adData != null) {
            adData.destroy();
        }
    }
}
//原生请求广告对象的销毁
if (nativeAd != null) {
    nativeAd.destroy();
}

2.4 原生广告预加载注意事项
Precautions for native ad preloading

  • 需要在平台开启预加载功能。

The preloading function needs to be enabled on the platform.

  • 开启预加载功能后,sdk内部会在原生广告曝光后再次去请求广告。成功后不会通知媒体,会在媒体下次loadAd时立马返回预加载成功的广告。(需要是同一个WMNativeAd对象

After the preloading function is enabled, the SDK will request ads again after the native ads are exposed. The media will not be notified after the success, and the preloaded ads will be returned immediately when the media loads Ad next time. ( It needs to be the same WMNativeAd object )

  • 开启预加载功能后,要注意全局使用同一个WMNativeAd对象。(new注意判null

After enabling the preloading function, be sure to use the same WMNativeAd object globally. ( Note that new is null .)

2.5 原生广告Gromore自渲染注意事项
Notes on Gromore self-rendering of native ads

  • Gromore原生自渲染需要绑定资源ID。
    Gromore native self-rendering requires binding resource ID.

1、视频类素材:mediaViewId(视频)必须绑定资源id,否则视频不展示,会影响广告计费。

Video materials: mediaViewId (video) must be bound to the resource id, otherwise the video will not be displayed, which will affect the advertising billing.

2、图片类素材:可以不绑定资源id,不影响广告计费。

Image materials: You can not bind the resource id, which will not affect the advertising billing.

3、callToActionId(广告创意按钮):如果开发者有需要展示创意按钮,必须绑定。

callToActionId (ad creative button): If the developer needs to display the creative button, it must be bound.

4、logoId(logo):目前SDK不支持logo资源的绑定,需要开发者针对GM自己渲染logo。

logoId (logo): Currently, the SDK does not support the binding of logo resources, and developers need to render the logo for GM themselves.

java
public class NativeAdDemoRender implements WMNativeAdRender<WMNativeAdData> {


    @Override
    public void renderAdView(View view, final WMNativeAdData adData) {

        //gromore需要绑定资源ID
        //在bindViewForInteraction之前注册
        if (adData.getNetworkId() == 22) {//gromore
            WMViewBinder viewBinder = new WMViewBinder.Builder(view.getId())
                    .titleId(text_title.getId())
                    .descriptionTextId(text_desc.getId())
                    .callToActionId(mCTAButton.getId())
                    .iconImageId(img_logo.getId())
                    .mainImageId(mImagePoster.getId())
                    .mediaViewIdId(mMediaViewLayout.getId())
                    .groupImage1Id(img_1.getId())
                    .groupImage2Id(img_2.getId())
                    .groupImage3Id(img_3.getId())
                    .shakeViewContainerId(shakeLayout.getId())
                    .build();
            adData.registerViewBidder(viewBinder);
        }

        //重要! 这个涉及到广告计费,必须正确调用。convertView必须使用ViewGroup。
        //作为creativeViewList传入,点击不进入详情页,直接下载或进入落地页,视频和图文广告均生效
        adData.bindViewForInteraction(context, view, clickableViews, creativeViewList, img_dislike);

    }
}