AppLovin Max聚合接入-激励广告

本文详细描述了在Android应用中集成AppLovin广告平台的过程,包括包的引入、AdRegistration配置、激励广告的初始化、加载及错误处理机制,如广告加载失败和显示失败时的重试策略。
摘要由CSDN通过智能技术生成

文档

平台选择

错误码

涉及到的平台开发者账号、激励广告id申请、广告瀑布流等这里不做说明。

代码

引入包

implementation 'com.applovin:applovin-sdk:12.4.2'
//以下内容是根据勾选对应广告平台时需要导入的包,具体参考文档平台选择
implementation 'com.applovin.mediation:amazon-tam-adapter:9.9.3.2'
implementation 'com.amazon.android:aps-sdk:9.9.3'
implementation 'com.applovin.mediation:chartboost-adapter:9.6.1.0'
implementation 'com.google.android.gms:play-services-base:16.1.0'
implementation 'com.applovin.mediation:vungle-adapter:7.3.1.2'
implementation 'androidx.recyclerview:recyclerview-selection:1.1.0'
implementation 'com.applovin.mediation:mintegral-adapter:16.6.71.0'
implementation 'com.applovin.mediation:smaato-adapter:22.6.1.0'
implementation 'com.applovin.mediation:unityads-adapter:4.10.0.0'

gradle中

android {
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
}
buildscript {
    repositories {
        maven { url 'https://artifacts.applovin.com/android' }
    }
}

gradle.properties中

android.useAndroidX=true
android.enableJetifier=true

AndroidManifest.xml

<meta-data android:name="applovin.sdk.key"
           android:value="YOUR_SDK_KEY_HERE"/>

初始化

AdRegistration.getInstance( "AMAZON_APP_ID", this );
AdRegistration.setAdNetworkInfo( new DTBAdNetworkInfo( DTBAdNetwork.MAX ) );
AdRegistration.setMRAIDSupportedVersions( new String[] { "1.0", "2.0", "3.0" } );
AdRegistration.setMRAIDPolicy( MRAIDPolicy.CUSTOM );

激励广告初始化、加载(里面已经包含了广告加载成功失败和变现的处理)

private void loadAd()
    {
        if ( isFirstLoad )
        {
            isFirstLoad = false;

            if ( rewardedAd == null )
            {
                  //打开后可以测试广告是否接入成功  
//                AppLovinSdk.getInstance(activity).showMediationDebugger();
                rewardedAd = MaxRewardedAd.getInstance( Reward_Ad_Id, activity );

                rewardedAd.setListener( new MaxRewardedAdListener() {
                    @Override
                    public void onAdLoaded(@NonNull final MaxAd ad)
                    {
                        Log.d(TAG, "onAdLoaded ready to be shown");
                        System.out.println("onAdLoaded ready to be shown");
                        // Rewarded ad is ready to be shown. rewardedAd.isReady() will now return 'true'
//                _reconnectMilliseconds = RECONNECT_TIMER_START_MILLISECONDS;
                        String mediaName = ad.getDspName();
                        Map<String, String> infoMap = new HashMap<>();
                        infoMap.put("mediaName", mediaName);
                        putLoadInfoToScript(true, infoMap);

                        // Reset retry attempt
                        retryAttempt = 0;
                    }

                    @Override
                    public void onAdLoadFailed(@NonNull final String adUnitId, @NonNull final MaxError maxError)
                    {
                        Log.e(TAG, "onAdLoadFailed maxError=000"+maxError);
                        System.out.println("onAdLoadFailed maxError=000 "+maxError);

                        // Rewarded ad failed to load. We recommend retrying with exponentially higher delays up to a maximum delay (in this case 64 seconds).

                        retryAttempt++;
                        long delayMillis = TimeUnit.SECONDS.toMillis( (long) Math.pow( 2, Math.min( 6, retryAttempt ) ) );

                        new Handler().postDelayed(new Runnable()
                        {
                            @Override
                            public void run()
                            {
                                rewardedAd.loadAd();
                            }
                        }, delayMillis );
                    }

                    @Override
                    public void onAdDisplayFailed(@NonNull final MaxAd ad, @NonNull final MaxError maxError)
                    {
                        Log.e(TAG, "onAdDisplayFailedr "+maxError);
                        System.out.println("onAdDisplayFailedr "+maxError);
                        int errorCode = maxError.getCode();
                        Map<String, String> infoMap = new HashMap<>();
                        infoMap.put("status", "4");
                        infoMap.put("message", maxError.getMessage());
                        infoMap.put("domain", maxError.getMediatedNetworkErrorMessage());
                        infoMap.put("errorCode", Integer.toString(errorCode));
                        System.out.println("onAdDisplayFailedr infoMap="+infoMap);
                        putShowStatusToScript(infoMap);

                        // Rewarded ad failed to display. We recommend loading the next ad.
                        rewardedAd.loadAd();
                    }

                    @Override
                    public void onAdDisplayed(@NonNull final MaxAd ad) {}

                    @Override
                    public void onAdClicked(@NonNull final MaxAd ad) {}

                    @Override
                    public void onAdHidden(@NonNull final MaxAd ad)
                    {
                        Log.e(TAG, "onAdHidden ");
                        // Rewarded ad is hidden. Pre-load the the next ad
                        rewardedAd.loadAd();
                    }

                    @Override
                    public void onUserRewarded(@NonNull final MaxAd ad, @NonNull final MaxReward reward)
                    {
                        Log.e(TAG, "onUserRewarded ");
                        System.out.println("onUserRewarded ");
                        // Rewarded ad was displayed and user should receive the reward.
                        int rewardAmount = reward.getAmount();
                        String rewardType = reward.getLabel();
                        Map<String, String> infoMap = new HashMap<>();
                        infoMap.put("status", "3");
                        infoMap.put("rewardType", rewardType);
                        infoMap.put("rewardAmount", Integer.toString(rewardAmount));
                        infoMap.put("mediaName", ad.getNetworkName());
                        Log.e(TAG, "onUserRewarded infoMap="+infoMap);
                        //调用游戏也出去处理奖励
                        putShowStatusToScript(infoMap);
                    }

                } );

                //region MAX Ad Revenue Listener
                rewardedAd.setRevenueListener(new MaxAdRevenueListener() {
                    @Override
                    public void onAdRevenuePaid(@NonNull MaxAd maxAd) {

                        AdjustAdRevenue adjustAdRevenue = new AdjustAdRevenue( AdjustConfig.AD_REVENUE_APPLOVIN_MAX );
                        adjustAdRevenue.setRevenue( maxAd.getRevenue(), "USD" );
                        adjustAdRevenue.setAdRevenueNetwork( maxAd.getNetworkName() );
                        adjustAdRevenue.setAdRevenueUnit( maxAd.getAdUnitId() );
                        adjustAdRevenue.setAdRevenuePlacement( maxAd.getPlacement() );
                        System.out.println("setRevenueListener adjustAdRevenue="+adjustAdRevenue);

                        Adjust.trackAdRevenue( adjustAdRevenue );
                    }
                });
            }

            DTBAdRequest adLoader = new DTBAdRequest();

            // Switch video player width and height values(320, 480) depending on device orientation

            DisplayMetrics dm = new DisplayMetrics();
            activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
            int width = dm.widthPixels;
            int height = dm.heightPixels;
            System.out.println("width = "+width+" height="+height);
            //设置显示区域大小
            adLoader.setSizes( new DTBAdSize.DTBVideo( width, height, "<AMAZON_VIDEO_REWARDED_SLOT_ID>" ) );
            adLoader.loadAd( new DTBAdCallback()
            {
                @Override
                public void onSuccess(@NonNull final DTBAdResponse dtbAdResponse)
                {
                    // 'rewardedAd' is your instance of MaxRewardedAd
                    rewardedAd.setLocalExtraParameter( "amazon_ad_response", dtbAdResponse );
                    rewardedAd.loadAd();
                }

                @Override
                public void onFailure(@NonNull final AdError adError)
                {
                    // 'rewardedAd' is your instance of MaxRewardedAd
                    rewardedAd.setLocalExtraParameter( "amazon_ad_error", adError );
                    rewardedAd.loadAd();
                }
            } );
        }
        else
        {
            rewardedAd.loadAd();
        }
    }

点击展示广告

public static void showAd()
{
    Log.d(TAG, "showAd");
    System.out.println("showAd");
    if(rewardedAd == null){
        if(_showAdCallback != null){
            Map<String, String> infoMap = new HashMap<>();
            infoMap.put("status", "0");
            infoMap.put("adIsOK", "0");
            Log.d(TAG, "showAd return");
            System.out.println("showAd return infoMap="+infoMap);
            putShowStatusToScript(infoMap);
        }
        return;
    }

    if ( rewardedAd.isReady() )
    {
        Log.d(TAG, "showAd isReady show");
        System.out.println("showAd isReady show");
        rewardedAd.showAd();
    } else {
        Log.d(TAG, "showAd not isReady!!!");
        System.out.println("showAd not isReady!!!");
        Map<String, String> infoMap = new HashMap<>();
        infoMap.put("status", "0");
        infoMap.put("adIsOK", "0");
        Log.d(TAG, "showAd return");
        System.out.println("showAd return infoMap="+infoMap);
        putShowStatusToScript(infoMap);
    }
}

函数putShowStatusToScript(infoMap)是处理广告函数。具体内容根据业务去处理

  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个使用 AppLovin MAX 播放广告的示例代码: 1. 首先,在你的项目中添加 AppLovin SDK 和 AppLovin MAX SDK 的依赖。 2. 初始化 AppLovin SDK,在 AppDelegate.m 文件中添加以下代码: ```objective-c #import <AppLovinSDK/AppLovinSDK.h> - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 初始化 AppLovin SDK [ALSdk initializeSdk]; return YES; } ``` 3. 在需要播放广告的地方创建一个 ALIncentivizedInterstitial 对象,并设置其代理: ```objective-c #import <AppLovinSDK/AppLovinSDK.h> @interface MyViewController () <ALAdLoadDelegate, ALAdDisplayDelegate, ALAdVideoPlaybackDelegate, ALIncentivizedInterstitialDelegate> @property (nonatomic, strong) ALIncentivizedInterstitialAd *incentivizedInterstitial; @end @implementation MyViewController - (void)viewDidLoad { [super viewDidLoad]; // 创建 ALIncentivizedInterstitial 对象 self.incentivizedInterstitial = [[ALIncentivizedInterstitialAd alloc] initWithSdk:[ALSdk shared]]; self.incentivizedInterstitial.adDisplayDelegate = self; self.incentivizedInterstitial.adVideoPlaybackDelegate = self; self.incentivizedInterstitial.adLoadDelegate = self; self.incentivizedInterstitial.delegate = self; // 加载广告 [self.incentivizedInterstitial preloadAndNotify]; } // 实现 ALAdLoadDelegate 协议 - (void)adService:(nonnull ALAdService *)adService didLoadAd:(nonnull ALAd *)ad { NSLog(@"广告加载成功"); } - (void)adService:(nonnull ALAdService *)adService didFailToLoadAdWithError:(int)code { NSLog(@"广告加载失败,错误代码:%d", code); } // 实现 ALAdDisplayDelegate 协议 - (void)ad:(nonnull ALAd *)ad wasDisplayedIn:(nonnull UIView *)view { NSLog(@"广告显示成功"); } - (void)ad:(nonnull ALAd *)ad wasHiddenIn:(nonnull UIView *)view { NSLog(@"广告已关闭"); } - (void)ad:(nonnull ALAd *)ad wasClickedIn:(nonnull UIView *)view { NSLog(@"广告被点击了"); } // 实现 ALAdVideoPlaybackDelegate 协议 - (void)videoPlaybackBeganInAd:(nonnull ALAd *)ad { NSLog(@"广告视频开始播放"); } - (void)videoPlaybackEndedInAd:(nonnull ALAd *)ad atPlaybackPercent:(nonnull NSNumber *)percentPlayed fullyWatched:(BOOL)wasFullyWatched { NSLog(@"广告视频播放结束,播放进度:%d%%,是否完全观看:%d", [percentPlayed intValue], wasFullyWatched); } // 实现 ALIncentivizedInterstitialDelegate 协议 - (void)incentivizedInterstitial:(nonnull ALIncentivizedInterstitialAd *)incentivizedInterstitial didFailToLoadWithError:(NSInteger)errorCode { NSLog(@"激励视频加载失败,错误代码:%ld", (long)errorCode); } - (void)incentivizedInterstitialDidLoad:(nonnull ALIncentivizedInterstitialAd *)incentivizedInterstitial { NSLog(@"激励视频加载成功"); } - (void)incentivizedInterstitial:(nonnull ALIncentivizedInterstitialAd *)incentivizedInterstitial didFailToDisplayWithError:(NSInteger)errorCode { NSLog(@"激励视频播放失败,错误代码:%ld", (long)errorCode); } - (void)incentivizedInterstitialDidDisplay:(nonnull ALIncentivizedInterstitialAd *)incentivizedInterstitial { NSLog(@"激励视频开始播放"); } - (void)incentivizedInterstitial:(nonnull ALIncentivizedInterstitialAd *)incentivizedInterstitial didCompleteAdWithWasFullyWatched:(BOOL)wasFullyWatched { NSLog(@"激励视频播放结束,是否完全观看:%d", wasFullyWatched); } - (void)incentivizedInterstitial:(nonnull ALIncentivizedInterstitialAd *)incentivizedInterstitial didClickWithUrl:(nullable NSURL *)url { NSLog(@"激励视频被点击了,URL:%@", url); } @end ``` 4. 在需要播放广告的地方调用以下代码: ```objective-c if (self.incentivizedInterstitial.isReadyForDisplay) { [self.incentivizedInterstitial showAd]; } else { NSLog(@"广告未准备好"); } ``` 以上就是一个简单的使用 AppLovin MAX 播放广告的示例代码,您可以根据自己的需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值