Android Mopub Native

出自TAMedia
於 2017年9月6日 (三) 03:59 由 imported>Wikiuser 所做的修訂
跳至導覽 跳至搜尋

請建立一個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;
 
         public TWMediaStaticNativeAd(final TWMNativeAd nativeAD, final CustomEventNativeListener customEventNativeListener) {
             mCustomEventNativeListener = customEventNativeListener;
             nativeAd = nativeAD;
         }
 
         @Override
         public void destroy() {
             nativeAd.destroy();
             super.destroy();
         }
         @Override
         public void prepare(View view) {
             super.prepare(view);
 
             view.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View v) {
                     notifyAdClicked();
                     nativeAd.handleClick();
                 }
             });
             notifyAdImpressed();
         }
 
         // 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;
                     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(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) {}
     }
 
 }



回MoPub教學