Android Mopub Native SDK8

出自TAMedia
於 2021年8月31日 (二) 07:18 由 imported>Wikiuser 所做的修訂 (新頁面: == 建立一個Class並繼承MoPub所提供的CustomEventNative == <br><br> Class實作內容如下:<br> <source> package com.taiwanmobile.pt.adp.nativeads import android.content...)
(差異) ←上個修訂 | 最新修訂 (差異) | 下個修訂→ (差異)
跳至導覽 跳至搜尋

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



Class實作內容如下:

package com.taiwanmobile.pt.adp.nativeads

import android.content.Context
import android.view.View
import com.mopub.common.Preconditions
import com.mopub.common.logging.MoPubLog
import com.mopub.nativeads.BaseNativeAd
import com.mopub.nativeads.CustomEventNative
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 com.mopub.common.logging.MoPubLog.AdapterLogEvent.CLICKED
import com.mopub.common.logging.MoPubLog.AdapterLogEvent.LOAD_SUCCESS
import com.mopub.common.logging.MoPubLog.AdapterLogEvent.LOAD_FAILED
import com.mopub.common.logging.MoPubLog.AdapterLogEvent.SHOW_SUCCESS
import com.mopub.common.logging.MoPubLog.AdapterLogEvent.WILL_DISAPPEAR
import com.taiwanmobile.pt.adp.nativead.TWMMediaContent
import com.taiwanmobile.pt.adp.nativead.TWMNativeAdOptions

// 需將本class的完整類別名稱 com.taiwanmobile.pt.adp.nativeads.TAMediaMopubNative 填入後台的Custom Event Class欄位
class TAMediaMopubNative : CustomEventNative() {

    /**
     * TAMedia ad unit ID
     */
    private var adUnitId: String? = null

    /**
     * Set MediaView unmuted in initial state
     */
    private var videoStartUnmuted: Boolean = false

    /**
     * Display Image in MediaView
     */
    private var mediaPreferImage: Boolean = false


    /**
     * String to store the simple class name for this adapter.
     */
    private val adapterName: String = TAMediaMopubNative::class.java.simpleName

    override fun loadNativeAd(context: Context,
                              customEventNativeListener: CustomEventNativeListener,
                              localExtras: Map<String, Any>,
                              serverExtras: Map<String, String>) {

        Preconditions.checkNotNull(context)
        Preconditions.checkNotNull(customEventNativeListener)
        Preconditions.checkNotNull(localExtras)
        Preconditions.checkNotNull(context)

        //後台設定的adUnitId會透過此Key值取得
        try {
            adUnitId = serverExtras.getValue(KEY_AD_UNIT_ID)
            if (serverExtras.containsKey(KEY_MEDIA_PREFER_IMAGE)) {
                mediaPreferImage = serverExtras.getValue(KEY_MEDIA_PREFER_IMAGE).toBoolean()
            }
            if (serverExtras.containsKey(KEY_VIDEO_START_UNMUTED)) {
                videoStartUnmuted = serverExtras.getValue(KEY_VIDEO_START_UNMUTED).toBoolean()
            }
            adUnitId?.let {
                val nativeAd = TAMediaNativeAd(customEventNativeListener)
                nativeAd.loadAd(context, it, mediaPreferImage, videoStartUnmuted)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * The class is used to load and map TAMedia native
     * ads to MoPub native ads.
     */
    inner class TAMediaNativeAd(listener: CustomEventNativeListener) :
        BaseNativeAd(), TWMAdViewListener {

        // Native ad assets.
        var body: String? = null
        var shortSubject: String? = null
        var longSubject: String? = null
        var callToAction:String? = null
        var iconRect: TWMNativeAd.Image? = null
        var iconSquare: TWMNativeAd.Image? = null
        var image960x640: TWMNativeAd.Image? = null
        var image1200x627: TWMNativeAd.Image? = null
        var mediaContent: TWMMediaContent? = null

        /**
         * A TAMedia native ad.
         */
        var nativeAd: TWMNativeAd? = null

        /**
         * A custom event native listener used to forward TAMedia SDK events to MoPub.
         */
        private var customEventNativeListener: CustomEventNativeListener? = listener

        /**
         * This method will load native ads from TAMedia for the given ad unit ID.
         *
         * @param context  required to request a TAMedia native ad.
         * @param adUnitId TAMedia's AdMob Ad Unit ID.
         * @param mediaPreferImage Display Image in MediaView
         * @param videoStartUnmuted Set MediaView unmuted in initial state
         */
        fun loadAd(context: Context,
                   adUnitId: String,
                   mediaPreferImage: Boolean,
                   videoStartUnmuted: Boolean
        ) {
            val nativeAdOptions = mutableListOf<TWMNativeAdOptions>().apply {
                if (mediaPreferImage) { add(TWMNativeAdOptions.MEDIA_PREFER_IMAGE) }
                if (videoStartUnmuted) { add(TWMNativeAdOptions.VIDEO_START_UNMUTED) }
            }
            nativeAd = TWMNativeAd(context, adUnitId)
            nativeAd?.setAdListener(this)
            nativeAd?.loadAd(TWMAdRequest().apply {
                if (nativeAdOptions.size > 0) {
                    setNativeAdOptions(nativeAdOptions.toTypedArray())
                }
            })
        }

        // BaseNativeAd
        override fun prepare(p0: View) {
        }
        override fun clear(p0: View) {
        }
        override fun destroy() {
            MoPubLog.log(getAdNetworkId(), WILL_DISAPPEAR, adapterName)
            nativeAd?.destroy()
        }

        // TWMAdViewListener
        override fun onDismissScreen(ad: TWMAd) {
        }
        override fun onFailedToReceiveAd(ad: TWMAd, errorCode: TWMAdRequest.ErrorCode) {
            MoPubLog.log(getAdNetworkId(), LOAD_FAILED, adapterName)
        }
        override fun onLeaveApplication(ad: TWMAd) {
            // notify MoPub click event
            this.notifyAdClicked()
            MoPubLog.log(getAdNetworkId(), CLICKED, adapterName)
        }
        override fun onPresentScreen(ad: TWMAd) {
            this.notifyAdImpressed()
            MoPubLog.log(getAdNetworkId(), SHOW_SUCCESS, adapterName)
        }
        override fun onReceiveAd(ad: TWMAd) {
            this.body = nativeAd?.nativeAdContent?.body
            this.shortSubject = nativeAd?.nativeAdContent?.shortSubject
            this.longSubject = nativeAd?.nativeAdContent?.longSubject
            this.callToAction = nativeAd?.nativeAdContent?.callToAction
            this.iconRect = nativeAd?.nativeAdContent?.iconRect
            this.iconSquare = nativeAd?.nativeAdContent?.iconSquare
            this.image960x640 = nativeAd?.nativeAdContent?.image960_640
            this.image1200x627 = nativeAd?.nativeAdContent?.image1200_627
            this.mediaContent = nativeAd?.nativeAdContent?.mediaContent
            customEventNativeListener?.onNativeAdLoaded(this)
            MoPubLog.log(getAdNetworkId(), LOAD_SUCCESS, adapterName)
        }

    }

    private fun getAdNetworkId(): String? {
        return adUnitId
    }

    companion object {
        private const val KEY_AD_UNIT_ID = "adUnitId"
        private const val KEY_MEDIA_PREFER_IMAGE = "MEDIA_PREFER_IMAGE"
        private const val KEY_VIDEO_START_UNMUTED = "VIDEO_START_UNMUTED"
    }
}




回MoPub教學