Android Mopub Native

出自TAMedia
跳至導覽 跳至搜尋

請建立一個Class並繼承Mopub所提供的CustomEventNative

Class實作內容如下:

 package com.taiwanmobile.pt.adp.nativeads;
 
 import android.content.Context;
 import android.support.annotation.NonNull;
 import android.util.Log;
 import android.view.View;
 
 import com.mopub.nativeads.CustomEventNative;
 import com.mopub.nativeads.NativeErrorCode;
 import com.mopub.nativeads.StaticNativeAd;
 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.JSONObject;
 
 import java.util.Map;
 
 // 需將本class的完整類別名稱 com.taiwanmobile.pt.adp.nativeads.TAMediaMopubNative 填入後台的Custom Event Class欄位
 public class TAMediaMopubNative extends CustomEventNative {
     private static final String TAG = "TAMediaMopubNative";
 
     private static final String TAM_NATIVE_KEY_TITLE = "LONGSUBJECT";
     private static final String TAM_NATIVE_KEY_TEXT = "BODY";
     private static final String TAM_NATIVE_KEY_MAIN_IMAGE_URL = "IMAGE1200X627";
     private static final String TAM_NATIVE_KEY_ICON_URL = "ICONSQUARE";
     private static final String TAM_NATIVE_KEY_DESTINATION_URL = "nurl";
     private static final String TAM_NATIVE_KEY_CALLTOACTION = "CTA";
 
     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(context, adUnitId);
         final TWMediaStaticNativeAd staticNativeAd = new TWMediaStaticNativeAd(
                 nativeAd,
                 customEventNativeListener
         );
 
         nativeAd.setAdListener(staticNativeAd);
         nativeAd.loadAd(new TWMAdRequest());
     }
 
     private static synchronized void mapStaticNativeAd(
             @NonNull final StaticNativeAd mopubSupportedAd,
             @NonNull final TWMNativeAd twmNative) throws Exception {
         if (twmNative.isReady()) {
             JSONObject content = twmNative.getNativeAdContent();
 
             String longSubject = (content.has(TAM_NATIVE_KEY_TITLE))
                     ? content.getString(TAM_NATIVE_KEY_TITLE)
                     : null;
             String body = (content.has(TAM_NATIVE_KEY_TEXT))
                     ? content.getString(TAM_NATIVE_KEY_TEXT)
                     : null;
             String iconUrl = (content.has(TAM_NATIVE_KEY_ICON_URL))
                     ? content.getString(TAM_NATIVE_KEY_ICON_URL)
                     : null;
             String imageUrl = (content.has(TAM_NATIVE_KEY_MAIN_IMAGE_URL))
                     ? content.getString(TAM_NATIVE_KEY_MAIN_IMAGE_URL)
                     : null;
             String targetUrl = (content.has(TAM_NATIVE_KEY_DESTINATION_URL))
                     ? content.getString(TAM_NATIVE_KEY_DESTINATION_URL)
                     : null;
             String callToAction = (content.has(TAM_NATIVE_KEY_CALLTOACTION))
                     ? content.getString(TAM_NATIVE_KEY_CALLTOACTION)
                     : null;
 
             if (longSubject != null)
                 mopubSupportedAd.setTitle(longSubject);
             if (body != null)
                 mopubSupportedAd.setText(body);
             if (imageUrl != null)
                 mopubSupportedAd.setMainImageUrl(imageUrl);
             if (iconUrl != null)
                 mopubSupportedAd.setIconImageUrl(iconUrl);
             if (targetUrl != null)
                 mopubSupportedAd.setClickDestinationUrl(targetUrl);
             if (callToAction != null)
                 mopubSupportedAd.setCallToAction(callToAction);
         }
     }
 
     static class TWMediaStaticNativeAd extends StaticNativeAd implements TWMAdViewListener {
 
         private final CustomEventNativeListener mCustomEventNativeListener;
         private final TWMNativeAd nativeAd;
 
         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) {
             try {
                 mapStaticNativeAd(this, nativeAd);
 
                 if(mCustomEventNativeListener != null)
                     mCustomEventNativeListener.onNativeAdLoaded(this);
             } catch (Exception 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();
             }
         };
 
 
     }
 
 
 }




回MoPub教學