「Android Mopub Native」修訂間的差異
跳至導覽
跳至搜尋
imported>Wikiuser |
imported>Wikiuser |
||
行 36: | 行 36: | ||
final TWMediaStaticNativeAd staticNativeAd = new TWMediaStaticNativeAd(nativeAd, customEventNativeListener); | final TWMediaStaticNativeAd staticNativeAd = new TWMediaStaticNativeAd(nativeAd, customEventNativeListener); | ||
nativeAd.setAdListener(staticNativeAd); | nativeAd.setAdListener(staticNativeAd); | ||
nativeAd.loadAd(new TWMAdRequest()); | nativeAd.loadAd(new TWMAdRequest()); | ||
} | } |
於 2018年8月20日 (一) 08:51 的修訂
請建立一個Class並繼承Mopub所提供的CustomEventNative
Class實作內容如下:
package com.taiwanmobile.pt.adp.mediation; import android.app.Activity; import android.content.Context; import android.util.Log; import android.view.View; import com.mopub.nativeads.CustomEventNative; import com.mopub.nativeads.NativeErrorCode; import com.taiwanmobile.pt.adp.view.TWMAd; import com.taiwanmobile.pt.adp.view.TWMAdRequest; import com.taiwanmobile.pt.adp.view.TWMAdViewListener; import com.taiwanmobile.pt.adp.view.TWMNativeAd; import org.json.JSONException; import org.json.JSONObject; import java.util.Map; // 需將本class的完整類別名稱 com.taiwanmobile.pt.adp.mediation.TAMediaMopubNative 填入後台的Custom Event Class欄位 public class TAMediaMopubNative extends CustomEventNative { private static final String TAG = "TAMediaMopubNative"; private TWMNativeAd nativeAd; @Override protected void loadNativeAd(final Context context, final CustomEventNativeListener customEventNativeListener, final Map<String, Object> localExtras, final Map<String, String> serverExtras) { Log.d(TAG, "loadNativeAd(" + serverExtras.get("adUnitId") + ") invoked!!"); String adUnitId = serverExtras.get("adUnitId"); //後台設定的adUnitId會透過此Key值取得 nativeAd = new TWMNativeAd((Activity) context, adUnitId); final TWMediaStaticNativeAd staticNativeAd = new TWMediaStaticNativeAd(nativeAd, customEventNativeListener); nativeAd.setAdListener(staticNativeAd); nativeAd.loadAd(new TWMAdRequest()); } static class TWMediaStaticNativeAd extends com.mopub.nativeads.StaticNativeAd implements TWMAdViewListener { private final CustomEventNativeListener mCustomEventNativeListener; private final TWMNativeAd nativeAd; // Native ad assets. private String longSubject; private String body; private String iconUrl; private String imageUrl; private String targetUrl; private String callToAction; public TWMediaStaticNativeAd(final TWMNativeAd nativeAD, final CustomEventNativeListener customEventNativeListener) { mCustomEventNativeListener = customEventNativeListener; nativeAd = nativeAD; } @Override public void prepare(final View view) { super.prepare(view); // Set click event to all view. view.setOnClickListener(clickListener); } @Override public void clear(View view) { view.setOnClickListener(null); super.clear(view); } @Override public void destroy() { nativeAd.destroy(); super.destroy(); } // TWMAdViewListener @Override public void onReceiveAd(TWMAd twmAd) { if (nativeAd.isReady()) { JSONObject content = nativeAd.getNativeAdContent(); try { longSubject = (content.has("LONGSUBJECT")) ? content.getString("LONGSUBJECT") : null; body = (content.has("BODY")) ? content.getString("BODY") : null; iconUrl = (content.has("ICONSQUARE")) ? content.getString("ICONSQUARE") : null; imageUrl = (content.has("IMAGE1200X627")) ? content.getString("IMAGE1200X627") : null; targetUrl = (content.has("nurl")) ? content.getString("nurl") : null; callToAction = (content.has("CTA")) ? content.getString("CTA") : null; if(longSubject != null) setTitle(longSubject); if(body != null) setText(body); if(imageUrl != null) setMainImageUrl(imageUrl); if(iconUrl != null) setIconImageUrl(iconUrl); if(targetUrl != null) setClickDestinationUrl(targetUrl); if(callToAction != null) setCallToAction(callToAction); if(mCustomEventNativeListener != null) mCustomEventNativeListener.onNativeAdLoaded(this); } catch (JSONException e) { Log.e(TAG, "Native Content Parse Error: " + e.getMessage()); if(mCustomEventNativeListener != null) mCustomEventNativeListener.onNativeAdFailed(NativeErrorCode.NETWORK_INVALID_STATE); } } } @Override public void onFailedToReceiveAd(TWMAd twmAd, TWMAdRequest.ErrorCode errorCode) { mCustomEventNativeListener.onNativeAdFailed(NativeErrorCode.NETWORK_NO_FILL); } @Override public void onPresentScreen(TWMAd twmAd) {} @Override public void onDismissScreen(TWMAd twmAd) {} @Override public void onLeaveApplication(TWMAd twmAd) {} private View.OnClickListener clickListener = new View.OnClickListener() { @Override public void onClick(View v) { notifyAdClicked(); nativeAd.handleClick(); } }; } }