Admob Reward New APIs For Custom Events

157 views
Skip to first unread message

Stephen Ren

unread,
Sep 27, 2019, 5:20:22 AM9/27/19
to Google Mobile Ads SDK Developers
Hi,

I have encounter with a issue while I implement the GADMediationAdapter protocol to serve third-party ads:

Upon an app's initialization of the Google Mobile Ads SDK, setUpWithConfiguration:completionHandler: wouldn't be invoked 

"loadRewardedAdForAdConfiguration" and "presentFromViewController" methods have been called properly.

So, my question is whether or not custom events wouldn't call setUpWithConfiguration method.

Or what should I do to make it work?

Mobile Ads SDK Forum Advisor Prod

unread,
Sep 27, 2019, 3:17:33 PM9/27/19
to stephe...@gmail.com, google-adm...@googlegroups.com
Hello Stephen, 

The error is because the initialization API for the GMA SDK was not used using the startwithCompletionHandler. Before loading ads, have your app initialize the Mobile Ads SDK by calling startWithCompletionHandler: with a GADInitializationCompletionHandler. This needs to be done only once, ideally at app launch. Please refer to this guide for more information on the SDK initialization. Please give this a try and let us know if you have any issues.

Thanks,
Bharani Cherukuri
Mobile Ads SDK Team

ref:_00D1U1174p._5001UHIA6o:ref

Jiang Liu

unread,
Dec 10, 2019, 5:39:40 AM12/10/19
to Google Mobile Ads SDK Developers
 hi,

I have the same problem,and I have called startWithCompletionHandler

Mobile Ads SDK Forum Advisor Prod

unread,
Dec 10, 2019, 12:48:11 PM12/10/19
to google-adm...@googlegroups.com
Hi Jiang, 

Could you please share the complete error logs, so I can take a closer look and assist you further?

Jiang Liu

unread,
Dec 10, 2019, 9:54:35 PM12/10/19
to Google Mobile Ads SDK Developers
hi,

I use the admob by unity plug - in.Admob ads can be played normally.Then I plan to add a third - party ad, but I do n’t want to write too much native code, so I send the admob callback back to unity.Then initialize the third - party ads from Unity.I control which kind of third - party ads are played through the parameters of CustomEvent.The following is the admob adapter I implemented.

GADMediationCustomAdapter.h

#import "GoogleMobileAds/GoogleMobileAds.h"

@interface GADMediationHandler : NSObject

@end

#if defined(__cplusplus)
extern "C" {
#endif
    
    typedef void (*GADCustomEventInitAdReques)();
    typedef void (*GADCustomEventLoadAdRequest)(const char* name);
    typedef void (*GADCustomEventShowAdRequest)(const char* name);

    
    void GADCustomEvent_Init(GADCustomEventInitAdReques init, GADCustomEventLoadAdRequest load, GADCustomEventShowAdRequest show);
    void GADCustomEvent_InitAdResponse();
    void GADCustomEvent_StateResponse(const char* name, int state, int errCode);
#if defined(__cplusplus)
}
#endif
NS_ASSUME_NONNULL_BEGIN


@interface GADMediationCustomAdapter : NSObject <GADMediationAdapter>


@end


NS_ASSUME_NONNULL_END


GADMediationCustomAdapter.m


#import "GADMediationCustomAdapter.h"

@interface GADMediationHandler()
@property (nonatomic, strong) GADMediationAdapterSetUpCompletionBlock setUpCompletion;
@property (nonatomic, strong) GADMediationRewardedLoadCompletionHandler loadCompletion;
@property (nonatomic, weak, nullable) id<GADMediationRewardedAdEventDelegate> rewardedAdEventDelegate;
@property (nonatomic, strong) id<GADMediationRewardedAd> rewardedAd;

@property bool initialed;
@property bool loaded;

@end
@implementation GADMediationHandler

@end

#if defined(__cplusplus)
extern "C"{
#endif
    
    
    static GADCustomEventInitAdReques s_InitAdRequest;
    static GADCustomEventLoadAdRequest s_LoadAdRequest;
    static GADCustomEventShowAdRequest s_ShowAdRequest;

    static NSMutableDictionary *s_MediationADMap = nil;
    
// ad system init
    void GADCustomEvent_Init(GADCustomEventInitAdReques init, GADCustomEventLoadAdRequest load, GADCustomEventShowAdRequest show){
        if (s_MediationADMap == nil) {
            s_MediationADMap = [[NSMutableDictionary alloc] initWithCapacity:2];
        }
        s_InitAdRequest = init;
        s_LoadAdRequest = load;
        s_ShowAdRequest = show;
        
        NSLog(@"GADMediation init.");
    }
// third ad init response
    void GADCustomEvent_InitAdResponse() {
        for (NSString* key in s_MediationADMap) {
            GADMediationHandler *handler = s_MediationADMap[key];
            handler.initialed = true;
            handler.setUpCompletion(nil);
        }
        
        NSLog(@"GADMediation InitAd: %lu", (unsigned long)s_MediationADMap.count);
    }
    // third ad response
    // open=1,clcick=2,close=3,given=4,abandon=5,loaded=6
    void GADCustomEvent_StateResponse(const char* name, int state, int errCode){
        NSString *adName = [NSString stringWithUTF8String:name];
        GADMediationHandler *handler = s_MediationADMap[adName];
        if (handler != nil) {
            switch (state) {
                case 1:
                    [handler.rewardedAdEventDelegate willPresentFullScreenView];
                    [handler.rewardedAdEventDelegate reportImpression];
                    [handler.rewardedAdEventDelegate didStartVideo];
                    
                    NSLog(@"GADMediation open: %@", adName);
                    break;
                case 2:
                    [handler.rewardedAdEventDelegate reportClick];
                    
                    NSLog(@"GADMediation click: %@", adName);
                    break;
                case 3:
                    [handler.rewardedAdEventDelegate willDismissFullScreenView];
                    
                    NSLog(@"GADMediation close: %@", adName);
                    break;
                case 4:
                {
                    [handler.rewardedAdEventDelegate didEndVideo];
                    GADAdReward *reward = [[GADAdReward alloc] initWithRewardType:@"reward" rewardAmount:[NSDecimalNumber decimalNumberWithString:@"1"]];
                    [handler.rewardedAdEventDelegate didRewardUserWithReward:reward];
                    
                    NSLog(@"GADMediation given: %@", adName);
                    break;
                }
                case 6:
                    // success to load
                    if (errCode == 0) {
                        handler.loaded = true;
                        handler.rewardedAdEventDelegate = handler.loadCompletion(handler.rewardedAd, nil);
                        
                        NSLog(@"GADMediation load success: %@", adName);
                    } else {
                        // failed
                        handler.loaded = false;
                        handler.loadCompletion(nil, [[NSError alloc] initWithDomain:NSMachErrorDomain code:1 userInfo:nil]);
                        [s_MediationADMap removeObjectForKey:adName];
                        
                        NSLog(@"GADMediation load failed: %@", adName);
                    }
                    break;
            }
        }
    }
    
    
#if defined(__cplusplus)
}
#endif



@interface GADMediationCustomAdapter() <GADMediationRewardedAd>


@end

@implementation GADMediationCustomAdapter

+ (GADVersionNumber)adSDKVersion {
    NSString *versionString = @"1.0.0";
    NSArray *versionComponents = [versionString componentsSeparatedByString:@"."];
    GADVersionNumber version = {0};
    if (versionComponents.count == 3) {
        version.majorVersion = [versionComponents[0] integerValue];
        version.minorVersion = [versionComponents[1] integerValue];
        version.patchVersion = [versionComponents[2] integerValue];
    }
    return version;
}

+ (GADVersionNumber)version {
    NSString *versionString = @"1.0.0.0";
    NSArray *versionComponents = [versionString componentsSeparatedByString:@"."];
    GADVersionNumber version = {0};
    if (versionComponents.count == 4) {
        version.majorVersion = [versionComponents[0] integerValue];
        version.minorVersion = [versionComponents[1] integerValue];
        
        // Adapter versions have 2 patch versions. Multiply the first patch by 100.
        version.patchVersion = [versionComponents[2] integerValue] * 100
        + [versionComponents[3] integerValue];
    }
    return version;
}

+ (nullable Class<GADAdNetworkExtras>)networkExtrasClass {
    return Nil;
}

// init third ad
+ (void)setUpWithConfiguration:(nonnull GADMediationServerConfiguration *)configuration
             completionHandler:(nonnull GADMediationAdapterSetUpCompletionBlock)completionHandler{
    
    for (GADMediationCredentials *credential in configuration.credentials){
        if (credential.format == GADAdFormatRewarded){
            NSString *adName = credential.settings[@"parameter"];
            GADMediationHandler *handler = [[GADMediationHandler alloc] init];
            handler.setUpCompletion = completionHandler;
            handler.initialed = false;
            handler.loaded = false;
            
            [s_MediationADMap setObject:handler forKey:adName];
            
            NSLog(@"GADMediation setup: %@", adName);
        }
    }
    
    s_InitAdRequest();
    NSLog(@"GADMediation initAdRequest");
}
// load third ad
- (void)loadRewardedAdForAdConfiguration:(nonnull GADMediationRewardedAdConfiguration *)adConfiguration completionHandler:(nonnull GADMediationRewardedLoadCompletionHandler) completionHandler{
    NSString *adName = adConfiguration.credentials.settings[@"parameter"];
    GADMediationHandler *handler = s_MediationADMap[adName];
    
    if (handler != nil) {
        handler.loadCompletion = completionHandler;
        handler.rewardedAd = self;
        s_LoadAdRequest([adName UTF8String]);
        
        NSLog(@"GADMediation load: %@", adName);
    } else {
        completionHandler(nil, [[NSError alloc] initWithDomain:NSArgumentDomain code:1 userInfo:nil]);
        
        NSLog(@"GADMediation load err: NoExistName, name: %@", adName);
    }

}
// show third ad
- (void)presentFromViewController:(nonnull UIViewController *)viewController {
    // found frist available ad
    for (NSString *key in s_MediationADMap) {
        GADMediationHandler *handler = s_MediationADMap[key];
        if (handler.loaded) {
            s_ShowAdRequest([key UTF8String]);
            
            NSLog(@"GADMediation show: %@", key);
            return;
        }
    }
    
    NSLog(@"GADMediation show err: AllNotReady");
}

@end




Mobile Ads SDK Forum Advisor Prod

unread,
Dec 11, 2019, 2:21:58 PM12/11/19
to google-adm...@googlegroups.com
Hello Jiang, 

Thank you for the code snippet. Could you please share the error logs that you received which will help us assist you with the issue?

Regards,
Message has been deleted
Message has been deleted

Jiang Liu

unread,
Dec 11, 2019, 9:12:40 PM12/11/19
to Google Mobile Ads SDK Developers


在 2019年12月12日星期四 UTC+8上午3:21:58,mobileadssdkforumadvisor写道:
hi

Attachments are all logs
 
log.zip

Mobile Ads SDK Forum Advisor Prod

unread,
Dec 12, 2019, 1:11:46 PM12/12/19
to google-adm...@googlegroups.com
Hi Jiang, 

Thank you for the logs. Upon checking with the team, it is confirmed that the Rewarded API for Custom Events is not yet supported for Unity. It could be the reason that the initialization is failing. Let me know if you have any further questions. 

Jiang Liu

unread,
Dec 12, 2019, 9:54:57 PM12/12/19
to Google Mobile Ads SDK Developers
hi Cherukuri


Unity does not support custom events refer to the unit initialization method is different? I see GADUInterface.m is initialized using startWithCompletionHandler

void GADUInitializeWithCallback(GADUTypeMobileAdsClientRef *mobileAdsClientRef,
                                GADUInitializationCompleteCallback callback) {
  [[GADMobileAds sharedInstance]
      startWithCompletionHandler:^(GADInitializationStatus *_Nonnull status) {
        GADUObjectCache *cache = [GADUObjectCache sharedInstance];
        [cache.references setObject:status forKey:[status gadu_referenceKey]];
        callback(mobileAdsClientRef, (__bridge GADUTypeInitializationStatusRef)status);
      }];
}


Mobile Ads SDK Forum Advisor Prod

unread,
Dec 13, 2019, 3:53:42 PM12/13/19
to google-adm...@googlegroups.com
Hi Jiang,

I'm checking this internally with the team. I will get back to you with an update on this. 

Thanks,

Bharani Cherukuri
Mobile Ads SDK Team

ref:_00D1U1174p._5001UOETqh:ref

Mobile Ads SDK Forum Advisor Prod

unread,
Dec 16, 2019, 6:44:53 PM12/16/19
to google-adm...@googlegroups.com
Hi Jiang,
Do you have a sample app that demonstrates this behavior so we can take a look? Thanks.

Jon

ref:_00D1U1174p._5001UOETqh:ref

Jiang Liu

unread,
Dec 17, 2019, 10:42:13 PM12/17/19
to Google Mobile Ads SDK Developers
hi Cherukuri

Can you use testflight? Our products are in mainland China(only ios)
Reply all
Reply to author
Forward
0 new messages