generated from capacitor-community/.github
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Conflicts: # ios/Plugin.xcodeproj/project.pbxproj # ios/Plugin/Plugin.m
- Loading branch information
Showing
19 changed files
with
740 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...a/com/getcapacitor/community/admob/rewardedinterstitial/AdRewardInterstitialExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.getcapacitor.community.admob.rewardedinterstitial; | ||
|
||
import android.app.Activity; | ||
import android.content.Context; | ||
import androidx.core.util.Supplier; | ||
import com.getcapacitor.JSObject; | ||
import com.getcapacitor.PluginCall; | ||
import com.getcapacitor.PluginMethod; | ||
import com.getcapacitor.community.admob.helpers.AdViewIdHelper; | ||
import com.getcapacitor.community.admob.helpers.RequestHelper; | ||
import com.getcapacitor.community.admob.models.AdMobPluginError; | ||
import com.getcapacitor.community.admob.models.AdOptions; | ||
import com.getcapacitor.community.admob.models.Executor; | ||
import com.google.android.gms.ads.AdRequest; | ||
import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAd; | ||
import com.google.android.gms.common.util.BiConsumer; | ||
|
||
public class AdRewardInterstitialExecutor extends Executor { | ||
|
||
public static RewardedInterstitialAd mRewardedInterstitialAd; | ||
|
||
public AdRewardInterstitialExecutor( | ||
Supplier<Context> contextSupplier, | ||
Supplier<Activity> activitySupplier, | ||
BiConsumer<String, JSObject> notifyListenersFunction, | ||
String pluginLogTag | ||
) { | ||
super(contextSupplier, activitySupplier, notifyListenersFunction, pluginLogTag, "AdRewardExecutor"); | ||
} | ||
|
||
@PluginMethod | ||
public void prepareRewardInterstitialAd(final PluginCall call, BiConsumer<String, JSObject> notifyListenersFunction) { | ||
final AdOptions adOptions = AdOptions.getFactory().createRewardInterstitialOptions(call); | ||
|
||
activitySupplier | ||
.get() | ||
.runOnUiThread( | ||
() -> { | ||
try { | ||
final AdRequest adRequest = RequestHelper.createRequest(adOptions); | ||
final String id = AdViewIdHelper.getFinalAdId(adOptions, adRequest, logTag, contextSupplier.get()); | ||
RewardedInterstitialAd.load( | ||
contextSupplier.get(), | ||
id, | ||
adRequest, | ||
RewardedInterstitialAdCallbackAndListeners.INSTANCE.getRewardedAdLoadCallback( | ||
call, | ||
notifyListenersFunction, | ||
adOptions | ||
) | ||
); | ||
} catch (Exception ex) { | ||
call.reject(ex.getLocalizedMessage(), ex); | ||
} | ||
} | ||
); | ||
} | ||
|
||
@PluginMethod | ||
public void showRewardInterstitialAd(final PluginCall call, BiConsumer<String, JSObject> notifyListenersFunction) { | ||
if (mRewardedInterstitialAd == null) { | ||
String errorMessage = "No Reward Video Ad can be show. It was not prepared or maybe it failed to be prepared."; | ||
call.reject(errorMessage); | ||
AdMobPluginError errorObject = new AdMobPluginError(-1, errorMessage); | ||
notifyListenersFunction.accept(RewardInterstitialAdPluginEvents.FailedToLoad, errorObject); | ||
return; | ||
} | ||
|
||
try { | ||
activitySupplier | ||
.get() | ||
.runOnUiThread( | ||
() -> { | ||
mRewardedInterstitialAd.show( | ||
activitySupplier.get(), | ||
RewardedInterstitialAdCallbackAndListeners.INSTANCE.getOnUserEarnedRewardListener(call, notifyListenersFunction) | ||
); | ||
} | ||
); | ||
} catch (Exception ex) { | ||
call.reject(ex.getLocalizedMessage(), ex); | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...com/getcapacitor/community/admob/rewardedinterstitial/RewardInterstitialAdPluginEvents.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.getcapacitor.community.admob.rewardedinterstitial | ||
|
||
import com.getcapacitor.community.admob.models.LoadPluginEventNames | ||
|
||
object RewardInterstitialAdPluginEvents: LoadPluginEventNames { | ||
const val Loaded = "onRewardedInterstitialAdLoaded" | ||
const val FailedToLoad = "onRewardedInterstitialAdFailedToLoad" | ||
const val Rewarded = "onRewardedInterstitialAdReward" | ||
override val Showed = "onRewardedInterstitialAdShowed" | ||
override val FailedToShow = "onRewardedInterstitialAdFailedToShow" | ||
override val Dismissed = "onRewardedInterstitialAdDismissed" | ||
} |
50 changes: 50 additions & 0 deletions
50
...acitor/community/admob/rewardedinterstitial/RewardedInterstitialAdCallbackAndListeners.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.getcapacitor.community.admob.rewardedinterstitial | ||
|
||
import com.getcapacitor.JSObject | ||
import com.getcapacitor.PluginCall | ||
import com.getcapacitor.community.admob.helpers.FullscreenPluginCallback | ||
import com.getcapacitor.community.admob.models.AdMobPluginError | ||
import com.getcapacitor.community.admob.models.AdOptions | ||
import com.google.android.gms.ads.LoadAdError | ||
import com.google.android.gms.ads.OnUserEarnedRewardListener | ||
import com.google.android.gms.ads.rewarded.RewardItem | ||
import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAd | ||
import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAdLoadCallback | ||
import com.google.android.gms.common.util.BiConsumer | ||
|
||
object RewardedInterstitialAdCallbackAndListeners { | ||
|
||
fun getOnUserEarnedRewardListener(call: PluginCall, notifyListenersFunction: BiConsumer<String, JSObject>): OnUserEarnedRewardListener { | ||
return OnUserEarnedRewardListener { item: RewardItem -> | ||
val response = JSObject() | ||
response.put("type", item.type) | ||
.put("amount", item.amount) | ||
notifyListenersFunction.accept(RewardInterstitialAdPluginEvents.Rewarded, response) | ||
call.resolve(response) | ||
} | ||
} | ||
|
||
fun getRewardedAdLoadCallback(call: PluginCall, notifyListenersFunction: BiConsumer<String, JSObject>, adOptions: AdOptions): RewardedInterstitialAdLoadCallback { | ||
return object : RewardedInterstitialAdLoadCallback() { | ||
override fun onAdLoaded(ad: RewardedInterstitialAd) { | ||
AdRewardInterstitialExecutor.mRewardedInterstitialAd = ad | ||
AdRewardInterstitialExecutor.mRewardedInterstitialAd.fullScreenContentCallback = FullscreenPluginCallback( | ||
RewardInterstitialAdPluginEvents, notifyListenersFunction) | ||
|
||
val adInfo = JSObject() | ||
adInfo.put("adUnitId", ad.adUnitId) | ||
call.resolve(adInfo) | ||
|
||
notifyListenersFunction.accept(RewardInterstitialAdPluginEvents.Loaded, adInfo) | ||
} | ||
|
||
override fun onAdFailedToLoad(adError: LoadAdError) { | ||
val adMobError = AdMobPluginError(adError) | ||
|
||
notifyListenersFunction.accept(RewardInterstitialAdPluginEvents.FailedToLoad, adMobError) | ||
call.reject(adError.message) | ||
} | ||
} | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
...oid/src/main/java/com/getcapacitor/community/admob/rewardedinterstitial/models/SsvInfo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.getcapacitor.community.admob.rewardedinterstitial.models | ||
|
||
import com.getcapacitor.PluginCall | ||
|
||
class SsvInfo( | ||
val customData: String? = null, | ||
val userId: String? = null) { | ||
|
||
constructor(pluginCall: PluginCall?) : this( | ||
pluginCall?.getObject("ssv")?.getString("customData"), | ||
pluginCall?.getObject("ssv")?.getString("userId") | ||
) | ||
|
||
constructor() : this(null, null) | ||
|
||
val hasInfo | ||
get(): Boolean { | ||
return customData != null || userId != null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
ios/Sources/AdMobPlugin/RewardedInterstitial/AdRewardInterstitialExecutor.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import Foundation | ||
import Capacitor | ||
import GoogleMobileAds | ||
|
||
class AdRewardInterstitialExecutor: NSObject, GADFullScreenContentDelegate { | ||
public weak var plugin: AdMob? | ||
var rewardedInterstitialAd: GADRewardedInterstitialAd! | ||
|
||
func prepareRewardInterstitialAd(_ call: CAPPluginCall, _ request: GADRequest, _ adUnitID: String) { | ||
GADRewardedInterstitialAd.load( | ||
withAdUnitID: adUnitID, | ||
request: request, | ||
completionHandler: { (ad, error) in | ||
if let error = error { | ||
NSLog("Rewarded ad failed to load with error: \(error.localizedDescription)") | ||
self.plugin?.notifyListeners(RewardInterstitialAdPluginEvents.FailedToLoad.rawValue, data: [ | ||
"code": 0, | ||
"message": error.localizedDescription | ||
]) | ||
call.reject("Loading failed") | ||
return | ||
} | ||
|
||
self.rewardedInterstitialAd = ad | ||
|
||
if let providedOptions = call.getObject("ssv") { | ||
let ssvOptions = GADServerSideVerificationOptions() | ||
|
||
if let customData = providedOptions["customData"] as? String { | ||
NSLog("Sending Custom Data: \(customData) to SSV callback") | ||
ssvOptions.customRewardString = customData | ||
} | ||
|
||
if let userId = providedOptions["userId"] as? String { | ||
NSLog("Sending UserId: \(userId) to SSV callback") | ||
ssvOptions.userIdentifier = userId | ||
} | ||
|
||
self.rewardedInterstitialAd?.serverSideVerificationOptions = ssvOptions | ||
} | ||
|
||
self.rewardedInterstitialAd?.fullScreenContentDelegate = self | ||
self.plugin?.notifyListeners(RewardInterstitialAdPluginEvents.Loaded.rawValue, data: [ | ||
"adUnitId": adUnitID | ||
]) | ||
call.resolve([ | ||
"adUnitId": adUnitID | ||
]) | ||
} | ||
) | ||
} | ||
|
||
func showRewardInterstitialAd(_ call: CAPPluginCall) { | ||
if let rootViewController = plugin?.getRootVC() { | ||
if let ad = self.rewardedInterstitialAd { | ||
ad.present(fromRootViewController: rootViewController, | ||
userDidEarnRewardHandler: { | ||
let reward = ad.adReward | ||
self.plugin?.notifyListeners(RewardInterstitialAdPluginEvents.Rewarded.rawValue, data: ["type": reward.type, "amount": reward.amount]) | ||
call.resolve(["type": reward.type, "amount": reward.amount]) | ||
} | ||
) | ||
} else { | ||
call.reject("Reward Video is Not Ready Yet") | ||
} | ||
} | ||
} | ||
|
||
public func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) { | ||
NSLog("RewardFullScreenDelegate Ad failed to present full screen content with error \(error.localizedDescription).") | ||
self.plugin?.notifyListeners(RewardInterstitialAdPluginEvents.FailedToShow.rawValue, data: [ | ||
"code": 0, | ||
"message": error.localizedDescription | ||
]) | ||
} | ||
|
||
public func adWillPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) { | ||
NSLog("RewardFullScreenDelegate Ad did present full screen content.") | ||
self.plugin?.notifyListeners(RewardInterstitialAdPluginEvents.Showed.rawValue, data: [:]) | ||
} | ||
|
||
public func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) { | ||
NSLog("RewardFullScreenDelegate Ad did dismiss full screen content.") | ||
self.plugin?.notifyListeners(RewardInterstitialAdPluginEvents.Dismissed.rawValue, data: [:]) | ||
} | ||
} |
Oops, something went wrong.