Skip to content

Commit

Permalink
Update: Getting userId operation had moved to AdsDemoManager and adde…
Browse files Browse the repository at this point in the history
…d builder pattern in HMSAdsKitManager for creating ads type.

- Added userId operation for rewardconfigverify in AdsDemoManager.
- HMSAdsKitManager edited using builder pattern and demo manger edited according to this.
  • Loading branch information
Andronovo-bit authored and alihan98ersoy committed Apr 4, 2024
1 parent 1c899bc commit 27d0a14
Show file tree
Hide file tree
Showing 2 changed files with 261 additions and 99 deletions.
122 changes: 113 additions & 9 deletions Assets/Huawei/Demos/Ads/AdsDemoManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@
using UnityEngine;
using HmsPlugin;
using HuaweiMobileServices.Ads;
using System;
using HuaweiMobileServices.Utils;
using HuaweiMobileServices.Id;
using static HmsPlugin.HMSAdsKitManager;

public class AdsDemoManager : MonoBehaviour
{
//private Toggle testAdStatusToggle;
private readonly string TAG = "[HMS] AdsDemoManager: ";
private AdLoadMethod RewardedAdLoadMethod = AdLoadMethod.Default;
private AdLoadMethod InterstitialAdLoadMethod = AdLoadMethod.Default;
private AdLoadMethod BannerAdLoadMethod = AdLoadMethod.Default;
private AdLoadMethod SplashAdLoadMethod = AdLoadMethod.Default;
private RewardVerifyConfig rewardVerifyConfig;

private const string TAG = "[HMS] AdsDemoManager: ";

#region Singleton

Expand All @@ -32,14 +42,25 @@ private void Awake()

private void Start()
{
HMSAdsKitManager.Instance = new HMSAdsKitManager(hasPurchasedNoAds: false)
{
OnRewarded = OnRewarded,
OnRewardedAdLoaded = OnRewardedAdLoaded,
OnInterstitialAdClosed = OnInterstitialAdClosed,
ConsentOnFail = OnConsentFail,
ConsentOnSuccess = OnConsentSuccess
};
BeforeBuildManager();
}

private void Init()
{
HMSAdsKitManager.Instance = new Builder()
.SetHasPurchasedNoAds(false)
.SetRewardedAdLoadMethod(RewardedAdLoadMethod, rewardVerifyConfig)
.SetBannerAdLoadMethod(InterstitialAdLoadMethod)
.SetInterstitialAdLoadMethod(BannerAdLoadMethod)
.SetSplashAdLoadMethod(SplashAdLoadMethod)
.Build();

HMSAdsKitManager.Instance.OnRewardedAdLoaded = OnRewardedAdLoaded;
HMSAdsKitManager.Instance.OnRewarded = OnRewarded;
HMSAdsKitManager.Instance.OnInterstitialAdClosed = OnInterstitialAdClosed;
HMSAdsKitManager.Instance.ConsentOnSuccess = OnConsentSuccess;
HMSAdsKitManager.Instance.ConsentOnFail = OnConsentFail;

HMSAdsKitManager.Instance.RequestConsentUpdate();

//testAdStatusToggle = GameObject.FindGameObjectWithTag("Toggle").GetComponent<Toggle>();
Expand Down Expand Up @@ -150,4 +171,87 @@ public void SetTestAdStatus()
HMSAdsKitManager.Instance.DestroyBannerAd();
HMSAdsKitManager.Instance.LoadAllAds();
}

private void BeforeBuildManager()
{
if (RewardedAdLoadMethod == AdLoadMethod.WithConfig)
{
AssignLoadRewardAdWithConfig();
return;
}
Init();
}

private void AssignLoadRewardAdWithConfig()
{
var user = HMSAccountKitManager.Instance.HuaweiId;
Debug.Log($"{TAG} GetCurrentUser is null : {user == null}");
if (user != null)
{
SetRewardAdConfig(user.OpenId);
return;
}

HMSAccountKitManager.Instance.OnSignInSuccess += OnSignInAccountSilentSuccess;
HMSAccountKitManager.Instance.OnSignInFailed += OnSignInAccountSilentFailed;

HMSAccountKitManager.Instance.SilentSignIn();

}
private void OnSignInAccountSilentSuccess(AuthAccount authHuaweiId)
{
Debug.Log($"{TAG} SignIn success. AuthHuaweiId: {authHuaweiId?.OpenId}");
if (authHuaweiId != null)
{
SetRewardAdConfig(authHuaweiId.OpenId);
return;
}
else
{
Debug.LogError($"{TAG} SignIn failed. AuthHuaweiId is null.");
}
}
private void OnSignInAccountSilentFailed(HMSException exception)
{
Debug.LogError($"{TAG} SignIn failed with exception: {exception.WrappedExceptionMessage}");
if (exception.WrappedExceptionMessage.Contains("2002"))
{
Debug.Log($"{TAG} SilentSignIn failed. User is not signed in. Trying SignIn.");
HMSAccountKitManager.Instance.OnSignInSuccess -= OnSignInAccountSilentSuccess;
HMSAccountKitManager.Instance.OnSignInFailed -= OnSignInAccountSilentFailed;

HMSAccountKitManager.Instance.OnSignInSuccess += OnSignInAccountSuccess;
HMSAccountKitManager.Instance.OnSignInFailed += OnSignInAccountFailed;

HMSAccountKitManager.Instance.SignIn();
}
}
private void OnSignInAccountSuccess(AuthAccount authHuaweiId)
{
Debug.Log($"{TAG} SignIn success. AuthHuaweiId: {authHuaweiId?.OpenId}");
if (authHuaweiId != null)
{
SetRewardAdConfig(authHuaweiId.OpenId);
return;
}
else
{
Debug.LogError($"{TAG} SignIn failed. AuthHuaweiId is null.");
}
}
private void OnSignInAccountFailed(HMSException exception)
{
Debug.LogError($"{TAG} SignIn failed with exception: {exception.WrappedExceptionMessage}");
}
private void SetRewardAdConfig(string userId)
{
var exampleData = $"{{\"userId\":\"{userId}\", \"date\":\"{DateTime.Now}\"}}";
rewardVerifyConfig = new RewardVerifyConfig.Builder()
.SetUserId(userId)
.SetData(exampleData)
.Build();
Init();

}

}

0 comments on commit 27d0a14

Please sign in to comment.