Skip to content

Commit

Permalink
[firebase_admob] Parameters for rewarded video SSV Callbacks (firebas…
Browse files Browse the repository at this point in the history
  • Loading branch information
dr4xor authored Feb 21, 2020
1 parent 0592027 commit 7ccdd3b
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/firebase_admob/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.9.1+1

* Enable custom parameters for rewarded video server-side verification callbacks.

## 0.9.1

* Support v2 embedding. This will remain compatible with the original embedding and won't require
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,20 @@ private void callShowRewardedVideoAd(Result result) {
}
}

private void callSetRewardedVideoAdUserId(MethodCall call, Result result) {
String userId = call.argument("userId");

rewardedWrapper.setUserId(userId);
result.success(Boolean.TRUE);
}

private void callSetRewardedVideoAdCustomData(MethodCall call, Result result) {
String customData = call.argument("customData");

rewardedWrapper.setCustomData(customData);
result.success(Boolean.TRUE);
}

private void callDisposeAd(Integer id, Result result) {
MobileAd ad = MobileAd.getAdForId(id);
if (ad == null) {
Expand Down Expand Up @@ -294,6 +308,12 @@ public void onMethodCall(MethodCall call, Result result) {
case "showRewardedVideoAd":
callShowRewardedVideoAd(result);
break;
case "setRewardedVideoAdUserId":
callSetRewardedVideoAdUserId(call, result);
break;
case "setRewardedVideoAdCustomData":
callSetRewardedVideoAdCustomData(call, result);
break;
case "disposeAd":
callDisposeAd(id, result);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ Status getStatus() {
return status;
}

public void setUserId(String userId) {
rewardedInstance.setUserId(userId);
}

public void setCustomData(String customData) {
rewardedInstance.setCustomData(customData);
}

public void load(String adUnitId, Map<String, Object> targetingInfo) {
status = Status.LOADING;
AdRequestBuilderFactory factory = new AdRequestBuilderFactory(targetingInfo);
Expand Down
24 changes: 24 additions & 0 deletions packages/firebase_admob/ios/Classes/FLTFirebaseAdMobPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,20 @@ - (void)callIsAdLoaded:(NSNumber *)mobileAdId
}
}

- (void)callSetRewardedVideoAdUserId:(FlutterMethodCall *)call result:(FlutterResult)result {
NSString *userId = (NSString *)call.arguments[@"userId"];

[self.rewardedWrapper setUserIdentifier:userId];
result([NSNumber numberWithBool:YES]);
}

- (void)callSetRewardedVideoAdCustomData:(FlutterMethodCall *)call result:(FlutterResult)result {
NSString *customData = (NSString *)call.arguments[@"customData"];

[self.rewardedWrapper setCustomRewardString:customData];
result([NSNumber numberWithBool:YES]);
}

- (void)callShowRewardedVideoAd:(FlutterMethodCall *)call result:(FlutterResult)result {
if (self.rewardedWrapper.status != FLTRewardedVideoAdStatusLoaded) {
result([FlutterError errorWithCode:@"ad_not_loaded"
Expand Down Expand Up @@ -257,6 +271,16 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
return;
}

if ([call.method isEqualToString:@"setRewardedVideoAdUserId"]) {
[self callSetRewardedVideoAdUserId:call result:result];
return;
}

if ([call.method isEqualToString:@"setRewardedVideoAdCustomData"]) {
[self callSetRewardedVideoAdCustomData:call result:result];
return;
}

if ([call.method isEqualToString:@"showRewardedVideoAd"]) {
[self callShowRewardedVideoAd:call result:result];
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ typedef enum : NSUInteger {
- (FLTRewardedVideoAdStatus)status;
- (void)loadWithAdUnitId:(NSString *)adUnitId targetingInfo:(NSDictionary *)targetingInfo;
- (void)show;
- (void)setUserIdentifier:(NSString *)userIdentifier;
- (void)setCustomRewardString:(NSString *)customRewardString;
@end
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ - (void)show {
presentFromRootViewController:[FLTRewardedVideoAdWrapper rootViewController]];
}

- (void)setUserIdentifier:(NSString *)userIdentifier {
[[GADRewardBasedVideoAd sharedInstance] setUserIdentifier:userIdentifier];
}

- (void)setCustomRewardString:(NSString *)customRewardString {
[[GADRewardBasedVideoAd sharedInstance] setCustomRewardString:customRewardString];
}

- (NSString *)description {
NSString *statusString =
(NSString *)rewardedStatusToString[[NSNumber numberWithInt:_rewardedStatus]];
Expand Down
25 changes: 25 additions & 0 deletions packages/firebase_admob/lib/firebase_admob.dart
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,31 @@ class RewardedVideoAd {
/// Callback invoked for events in the rewarded video ad lifecycle.
RewardedVideoAdListener listener;

String _userId;
String _customData;

/// The user id used in server-to-server reward callbacks
String get userId => _userId;

/// The custom data included in server-to-server reward callbacks
String get customData => _customData;

/// Sets the user id to be used in server-to-server reward callbacks.
set userId(String userId) {
_invokeBooleanMethod("setRewardedVideoAdUserId", <String, dynamic>{
'userId': userId,
});
_userId = userId;
}

/// Sets custom data to be included in server-to-server reward callbacks.
set customData(String customData) {
_invokeBooleanMethod("setRewardedVideoAdCustomData", <String, dynamic>{
'customData': customData,
});
_customData = customData;
}

/// Shows a rewarded video ad if one has been loaded.
Future<bool> show() {
return _invokeBooleanMethod("showRewardedVideoAd");
Expand Down
2 changes: 1 addition & 1 deletion packages/firebase_admob/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: firebase_admob
description: Flutter plugin for Firebase AdMob, supporting
banner, interstitial (full-screen), and rewarded video ads
homepage: https://github.com/FirebaseExtended/flutterfire/tree/master/packages/firebase_admob
version: 0.9.1
version: 0.9.1+1

flutter:
plugin:
Expand Down
12 changes: 12 additions & 0 deletions packages/firebase_admob/test/firebase_admob_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ void main() {
case 'loadRewardedVideoAd':
case 'showAd':
case 'showRewardedVideoAd':
case 'setRewardedVideoAdUserId':
case 'setRewardedVideoAdCustomData':
case 'disposeAd':
return Future<bool>.value(true);
default:
Expand Down Expand Up @@ -128,13 +130,23 @@ void main() {
targetingInfo: const MobileAdTargetingInfo()),
true);

RewardedVideoAd.instance.userId = "user-id";
RewardedVideoAd.instance.customData = "custom-data";

expect(await RewardedVideoAd.instance.show(), true);

expect(log, <Matcher>[
isMethodCall('loadRewardedVideoAd', arguments: <String, dynamic>{
'adUnitId': RewardedVideoAd.testAdUnitId,
'targetingInfo': <String, String>{'requestAgent': 'flutter-alpha'},
}),
isMethodCall('setRewardedVideoAdUserId', arguments: <String, dynamic>{
'userId': "user-id",
}),
isMethodCall('setRewardedVideoAdCustomData',
arguments: <String, dynamic>{
'customData': "custom-data",
}),
isMethodCall('showRewardedVideoAd', arguments: null),
]);
});
Expand Down

0 comments on commit 7ccdd3b

Please sign in to comment.