「Android Mopub Native」修訂間的差異
跳至導覽
跳至搜尋
imported>Wikiuser |
imported>Wikiuser |
||
(未顯示同一使用者於中間所作的 1 次修訂) | |||
行 1: | 行 1: | ||
請建立一個Class並繼承Mopub所提供的CustomEventNative<br><br> | 請建立一個Class並繼承Mopub所提供的CustomEventNative<br><br> | ||
Class實作內容如下:<br> | Class實作內容如下:<br> | ||
package com.taiwanmobile.pt.adp. | package com.taiwanmobile.pt.adp.nativeads; | ||
import android.content.Context; | import android.content.Context; | ||
import android.support.annotation.NonNull; | |||
import android.util.Log; | import android.util.Log; | ||
import android.view.View; | import android.view.View; | ||
import com.mopub.nativeads.CustomEventNative; | import com.mopub.nativeads.CustomEventNative; | ||
import com.mopub.nativeads.NativeErrorCode; | import com.mopub.nativeads.NativeErrorCode; | ||
import com.mopub.nativeads.StaticNativeAd; | |||
import com.taiwanmobile.pt.adp.view.TWMAd; | import com.taiwanmobile.pt.adp.view.TWMAd; | ||
import com.taiwanmobile.pt.adp.view.TWMAdRequest; | import com.taiwanmobile.pt.adp.view.TWMAdRequest; | ||
行 15: | 行 16: | ||
import com.taiwanmobile.pt.adp.view.TWMNativeAd; | import com.taiwanmobile.pt.adp.view.TWMNativeAd; | ||
import org.json.JSONObject; | import org.json.JSONObject; | ||
import java.util.Map; | import java.util.Map; | ||
<b>// 需將本class的完整類別名稱 com.taiwanmobile.pt.adp. | <b>// 需將本class的完整類別名稱 com.taiwanmobile.pt.adp.nativeads.TAMediaMopubNative 填入後台的Custom Event Class欄位</b> | ||
public class TAMediaMopubNative extends CustomEventNative { | public class TAMediaMopubNative extends CustomEventNative { | ||
private static final String TAG = "TAMediaMopubNative"; | 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; | private TWMNativeAd nativeAd; | ||
行 32: | 行 39: | ||
final Map<String, String> serverExtras) { | final Map<String, String> serverExtras) { | ||
Log.d(TAG, "loadNativeAd(" + serverExtras.get("adUnitId") + ") invoked!!"); | Log.d(TAG, "loadNativeAd(" + serverExtras.get("adUnitId") + ") invoked!!"); | ||
String adUnitId = serverExtras.get("adUnitId"); //後台設定的adUnitId會透過此Key值取得 | String adUnitId = serverExtras.get("adUnitId"); <b>//後台設定的adUnitId會透過此Key值取得</b> | ||
nativeAd = new TWMNativeAd( | nativeAd = new TWMNativeAd(context, adUnitId); | ||
final TWMediaStaticNativeAd staticNativeAd = new TWMediaStaticNativeAd(nativeAd, customEventNativeListener); | final TWMediaStaticNativeAd staticNativeAd = new TWMediaStaticNativeAd( | ||
nativeAd, | |||
customEventNativeListener | |||
); | |||
nativeAd.setAdListener(staticNativeAd); | 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 | static class TWMediaStaticNativeAd extends StaticNativeAd implements TWMAdViewListener { | ||
private final CustomEventNativeListener mCustomEventNativeListener; | private final CustomEventNativeListener mCustomEventNativeListener; | ||
private final TWMNativeAd nativeAd; | private final TWMNativeAd nativeAd; | ||
public TWMediaStaticNativeAd(final TWMNativeAd nativeAD, final CustomEventNativeListener customEventNativeListener) { | |||
public TWMediaStaticNativeAd(final TWMNativeAd nativeAD, final CustomEventNativeListener customEventNativeListener) { | |||
mCustomEventNativeListener = customEventNativeListener; | mCustomEventNativeListener = customEventNativeListener; | ||
nativeAd = nativeAD; | nativeAd = nativeAD; | ||
行 82: | 行 123: | ||
@Override | @Override | ||
public void onReceiveAd(TWMAd twmAd) { | 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 | @Override | ||
行 125: | 行 153: | ||
} | } | ||
}; | }; | ||
} | } | ||
} | } | ||
<br><br> | <br><br> | ||
[[Android Integeration with Mopub Mediation|回MoPub教學]]<br> | [[Android Integeration with Mopub Mediation|回MoPub教學]]<br> |
於 2019年7月11日 (四) 03:47 的最新修訂
請建立一個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(); } }; } }