使用者工具

網站工具


android:mopub2

使用Mopub Mediation串接廣告

Mopub後台設定


1.請先至Apps選取您的App點選“Add an Ad Unit”新增廣告。



2.設定廣告名稱,裝置及廣告版型



3.顯示廣告版型的UnitID,並放在App裡



4.至Networks→ Add a Network。



5.點選Custom SDK Network



6.填入您專案的 package name.class name, 可以參考下圖範例步驟4
7.填入您在Adbert申請的APPID/APPKEY, 可以參考上圖範例步驟5


8.開啟授權,選擇 “Segments” 選項並選擇 “Global Segment”,請開啟您新增的Network授權,並確認狀態為 “Running”。

  • 橫幅廣告Class Name:AdbertMopubBanner.java
  • 插屏廣告Class Name:AdbertMopubInterstitial.java
  • 原生廣告Class Name:AdbertMopubCustomEventNative.java


參數設定方法

{"appid":"與艾普特申請的appid","appkey":"與艾普特申請的appkey"}

注意:使用Mopub SDK仍須引用google play services。

CUSTOM EVENT CLASS DATA :
{“appid”:“填入向ADBERT申請的appid”,“appkey”:“填入向ADBERT申請的appkey”}

ADBERT橫幅廣告Adapter

AdbertMopubBanner.java
package com.adbert.mopubtest;
 
 
import android.content.Context;
 
import com.adbert.AdbertADView;
import com.adbert.AdbertListener;
import com.google.android.gms.ads.AdSize;
import com.mopub.common.util.Views;
import com.mopub.mobileads.CustomEventBanner;
 
import java.util.Map;
 
import static com.mopub.mobileads.MoPubErrorCode.NETWORK_NO_FILL;
 
 
public class AdbertMopubBanner extends CustomEventBanner {
    private AdbertADView adbertADView;
 
    @Override
    protected void loadBanner(
            final Context context,
            final CustomEventBannerListener customEventBannerListener,
            final Map<String, Object> localExtras,
            final Map<String, String> serverExtras) {
 
        adbertADView = new AdbertADView(context);
        String appid = serverExtras.get("appid");
        String appkey = serverExtras.get("appkey");
        adbertADView.setMediationAPPID(appid + "|" + appkey);
        adbertADView.setBannerSize(AdSize.SMART_BANNER);
        adbertADView.setListener(new AdbertListener() {
            @Override
            public void onReceive(String s) {
                customEventBannerListener.onBannerLoaded(adbertADView);
            }
 
            @Override
            public void onFailedReceive(String s) {
                customEventBannerListener.onBannerFailed(NETWORK_NO_FILL);
            }
        });
        adbertADView.start();
    }
 
    @Override
    protected void onInvalidate() {
        Views.removeFromParent(adbertADView);
        if (adbertADView != null) {
            adbertADView.destroy();
        }
    }
 
 
}

ADBERT插屏廣告Adapter

AdbertMopubInterstitial.java
package com.adbert.mopubtest;
 
import android.content.Context;
 
import com.adbert.AdbertInterstitialAD;
import com.adbert.AdbertListener;
import com.mopub.mobileads.CustomEventInterstitial;
import com.mopub.mobileads.MoPubErrorCode;
 
import java.util.Map;
 
 
public class AdbertMopubInterstitial extends CustomEventInterstitial {
 
    AdbertInterstitialAD adbertInterstitialAD;
 
    @Override
    protected void loadInterstitial(
            final Context context,
            final CustomEventInterstitialListener customEventInterstitialListener,
            final Map<String, Object> localExtras,
            final Map<String, String> serverExtras) {
        adbertInterstitialAD = new AdbertInterstitialAD(context);
        String appid = serverExtras.get("appid");
        String appkey = serverExtras.get("appkey");
        adbertInterstitialAD.setMediationAPPID(appid + "|" + appkey);
        adbertInterstitialAD.setListener(new AdbertListener() {
            @Override
            public void onReceive(String msg) {
                customEventInterstitialListener.onInterstitialLoaded();
            }
 
            @Override
            public void onFailedReceive(String msg) {
                customEventInterstitialListener.onInterstitialFailed(MoPubErrorCode.NETWORK_NO_FILL);
            }
 
            @Override
            public void onAdClosed() {
                customEventInterstitialListener.onInterstitialDismissed();
            }
        });
        adbertInterstitialAD.loadAd();
 
    }
 
    @Override
    protected void showInterstitial() {
        if (adbertInterstitialAD != null)
            adbertInterstitialAD.show();
    }
 
    @Override
    protected void onInvalidate() {
    }
 
}

ADBERT原生性廣告Adapter

AdbertMopubCustomEventNative.java
package com.adbert.test;
 
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;
 
import com.adbert.AdbertNativeAD;
import com.adbert.AdbertNativeADListener;
import com.mopub.nativeads.CustomEventNative;
import com.mopub.nativeads.ImpressionTracker;
import com.mopub.nativeads.NativeErrorCode;
 
import org.json.JSONException;
import org.json.JSONObject;
 
import java.util.Map;
 
class AdbertMopubCustomEventNative extends CustomEventNative {
 
    @Override
    protected void loadNativeAd(@NonNull final Context context, @NonNull final CustomEventNativeListener customEventNativeListener, Map<String, Object> localExtras, @NonNull Map<String, String> serverExtras) {
 
        String appId = serverExtras.get("appid");
        String appKey = serverExtras.get("appkey");
 
        final AdbertNativeAD adbertNativeAd = new AdbertNativeAD(context, appId, appKey);
        adbertNativeAd.setListener(new AdbertNativeADListener() {
 
            @Override
            public void onReceived(String message) {
                customEventNativeListener.onNativeAdLoaded(new AdbertStaticNativeAd(adbertNativeAd));
            }
 
            @Override
            public void onFailReceived(String message) {
                customEventNativeListener.onNativeAdFailed(NativeErrorCode.NETWORK_NO_FILL);
            }
        });
        adbertNativeAd.loadAD();
    }
 
    static class AdbertStaticNativeAd extends com.mopub.nativeads.StaticNativeAd {
 
        private AdbertNativeAD nativeAD = null;
 
        private ImpressionTracker mImpressionTracker;
 
        public AdbertStaticNativeAd(@NonNull AdbertNativeAD nativeAD, final ImpressionTracker impressionTracker) {
            if (nativeAD.isReady()) {
                this.nativeAD = nativeAD;
                this.mImpressionTracker = impressionTracker;
                JSONObject content = nativeAD.getData();
                try {
                    setTitle(content.getString("headline"));
                    setText(content.getString("desc"));
                    setMainImageUrl(content.getString("image"));
                    setIconImageUrl(content.getString("icon"));
                    addExtra("company", content.getString("companyName"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
 
        private View view;
        @Override
        public void prepare(@NonNull View view) {
            super.prepare(view);
            nativeAD.registerView(view);
            this.view = view;
            mImpressionTracker.addView(view, this);
        }
 
        @Override
        public void clear(final View view) {
            mImpressionTracker.removeView(view);
        }
 
        @Override
        public void destroy() {
            super.destroy();
            nativeAD.unregisterView(view);
            mImpressionTracker.destroy();
        }
 
        @Override
        public void recordImpression(final View view) {
            notifyAdImpressed();
            nativeAD.setImpression();
 
        }
 
        @Override
        public void handleClick(final View view) {
            notifyAdClicked();
            nativeAD.sethandleClick();
        }
    }
 
}
android/mopub2.txt · 上一次變更: 2018/06/22 18:15 (外部編輯)