Skip to content

Commit

Permalink
feat: add method to reload banner ad
Browse files Browse the repository at this point in the history
  • Loading branch information
dylancom committed May 20, 2024
1 parent 9eb3b51 commit db716ea
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 5 deletions.
6 changes: 6 additions & 0 deletions RNGoogleMobileAdsExample/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ class BannerTest implements Test {

constructor(bannerAdSize) {
this.bannerAdSize = bannerAdSize;
this.bannerRef = React.createRef();
}

getPath(): string {
Expand All @@ -203,9 +204,13 @@ class BannerTest implements Test {
}

render(onMount: (component: any) => void): React.ReactNode {
const reload = () => {
this.bannerRef.current?.reload();
};
return (
<View ref={onMount}>
<BannerAd
ref={this.bannerRef}
unitId={
this.bannerAdSize.includes('ADAPTIVE_BANNER')
? TestIds.ADAPTIVE_BANNER
Expand All @@ -220,6 +225,7 @@ class BannerTest implements Test {
);
}}
/>
<Button title="reload" onPress={reload} />
</View>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public class ReactNativeGoogleMobileAdsBannerAdViewManager
private final String EVENT_SIZE_CHANGE = "onSizeChange";
private final String EVENT_APP_EVENT = "onAppEvent";
private final int COMMAND_ID_RECORD_MANUAL_IMPRESSION = 1;
private final int COMMAND_ID_RELOAD = 2;

@Nonnull
@Override
Expand All @@ -85,7 +86,9 @@ public Map<String, Object> getExportedCustomDirectEventTypeConstants() {
@Nullable
@Override
public Map<String, Integer> getCommandsMap() {
return MapBuilder.of("recordManualImpression", COMMAND_ID_RECORD_MANUAL_IMPRESSION);
return MapBuilder.of(
"recordManualImpression", COMMAND_ID_RECORD_MANUAL_IMPRESSION,
"reload", COMMAND_ID_RELOAD);
}

@Override
Expand All @@ -99,6 +102,10 @@ public void receiveCommand(
if (adView instanceof AdManagerAdView) {
((AdManagerAdView) adView).recordManualImpression();
}
} else if (commandIdInt == COMMAND_ID_RELOAD) {
BaseAdView adView = getAdView(reactViewGroup);
AdRequest request = reactViewGroup.getRequest();
adView.loadAd(request);
}
}

Expand Down
1 change: 1 addition & 0 deletions ios/RNGoogleMobileAds/RNGoogleMobileAdsBannerComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

- (void)requestAd;
- (void)recordManualImpression;
- (void)reload;

@end

Expand Down
9 changes: 9 additions & 0 deletions ios/RNGoogleMobileAds/RNGoogleMobileAdsBannerComponent.m
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@ - (void)requestAd {
}];
}

- (void)reload {
[_banner loadRequest:[RNGoogleMobileAdsCommon buildAdRequest:_request]];
[self sendEvent:@"onSizeChange"
payload:@{
@"width" : @(_banner.bounds.size.width),
@"height" : @(_banner.bounds.size.height),
}];
}

- (void)sendEvent:(NSString *)type payload:(NSDictionary *_Nullable)payload {
if (!self.onNativeEvent) {
return;
Expand Down
16 changes: 16 additions & 0 deletions ios/RNGoogleMobileAds/RNGoogleMobileAdsBannerView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ - (void)requestAd {
- (void)handleCommand:(const NSString *)commandName args:(const NSArray *)args {
if ([commandName isEqual:@"recordManualImpression"]) {
[self recordManualImpression];
} else if ([commandName isEqual:@"reload"]) {
[self reload];
}
}

Expand All @@ -170,6 +172,20 @@ - (void)recordManualImpression {
}
}

- (void)reload {
if ([_banner class] == [GAMBannerView class]) {
[_banner loadRequest:[RNGoogleMobileAdsCommon buildAdRequest:_request]];
if (_eventEmitter != nullptr) {
std::dynamic_pointer_cast<const facebook::react::RNGoogleMobileAdsBannerViewEventEmitter>(
_eventEmitter)
->onNativeEvent(facebook::react::RNGoogleMobileAdsBannerViewEventEmitter::OnNativeEvent{
.type = "onSizeChange",
.width = _banner.bounds.size.width,
.height = _banner.bounds.size.height});
}
}
}

#pragma mark - Events

- (void)bannerViewDidReceiveAd:(GADBannerView *)bannerView {
Expand Down
14 changes: 14 additions & 0 deletions ios/RNGoogleMobileAds/RNGoogleMobileAdsBannerViewManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ @implementation RNGoogleMobileAdsBannerViewManager
#endif
}

RCT_EXPORT_METHOD(reload : (nonnull NSNumber *)reactTag) {
#if !TARGET_OS_MACCATALYST
[self.bridge.uiManager
addUIBlock:^(RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
RNGoogleMobileAdsBannerComponent *banner = viewRegistry[reactTag];
if (!banner || ![banner isKindOfClass:[RNGoogleMobileAdsBannerComponent class]]) {
RCTLogError(@"Cannot find NativeView with tag #%@", reactTag);
return;
}
[banner reload];
}];
#endif
}

#ifdef RCT_NEW_ARCH_ENABLE

#else
Expand Down
17 changes: 14 additions & 3 deletions src/ads/BannerAd.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,21 @@
*
*/

import React from 'react';
import React, { createRef } from 'react';
import { BannerAdProps } from '../types/BannerAdProps';
import { BaseAd } from './BaseAd';
import GoogleMobileAdsBannerView, { Commands } from './GoogleMobileAdsBannerViewNativeComponent';

export function BannerAd({ size, ...props }: BannerAdProps) {
return <BaseAd sizes={[size]} {...props} />;
export class BannerAd extends React.Component<BannerAdProps> {
private ref = createRef<React.ElementRef<typeof GoogleMobileAdsBannerView>>();

reload() {
if (this.ref.current) {
Commands.reload(this.ref.current);
}
}

render() {
return <BaseAd ref={this.ref} sizes={[this.props.size]} {...this.props} />;
}
}
6 changes: 6 additions & 0 deletions src/ads/GAMBannerAd.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ export class GAMBannerAd extends React.Component<GAMBannerAdProps> {
}
}

reload() {
if (this.ref.current) {
Commands.reload(this.ref.current);
}
}

render() {
return <BaseAd ref={this.ref} {...this.props} />;
}
Expand Down
3 changes: 2 additions & 1 deletion src/ads/GoogleMobileAdsBannerViewNativeComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ export type ComponentType = HostComponent<NativeProps>;

interface NativeCommands {
recordManualImpression: (viewRef: React.ElementRef<ComponentType>) => void;
reload: (viewRef: React.ElementRef<ComponentType>) => void;
}

export const Commands: NativeCommands = codegenNativeCommands<NativeCommands>({
supportedCommands: ['recordManualImpression'],
supportedCommands: ['recordManualImpression', 'reload'],
});

export default codegenNativeComponent<NativeProps>(
Expand Down

0 comments on commit db716ea

Please sign in to comment.