diff --git a/Runtime/Code/Config/GuruSDKInitConfig.cs b/Runtime/Code/Config/GuruSDKInitConfig.cs index df86a33..fa361b7 100644 --- a/Runtime/Code/Config/GuruSDKInitConfig.cs +++ b/Runtime/Code/Config/GuruSDKInitConfig.cs @@ -20,6 +20,10 @@ namespace Guru /// public bool IAPEnabled = true; /// + /// 自动申请推送授权信息 + /// + public bool AutoNotificationPermission = true; + /// /// 自动记录完成的关卡 /// public bool AutoRecordFinishedLevels = true; diff --git a/Runtime/Code/SDK/GuruSDK.Ads.cs b/Runtime/Code/SDK/GuruSDK.Ads.cs index b583de2..10e9d04 100644 --- a/Runtime/Code/SDK/GuruSDK.Ads.cs +++ b/Runtime/Code/SDK/GuruSDK.Ads.cs @@ -1,14 +1,13 @@ - namespace Guru { using UnityEngine; using System; - + using Guru.Notification; public partial class GuruSDK { - + private const float CONSENT_FLOW_TIMEOUT = 10; // Consent Flow 超时时间(秒) private static AdsInitSpec _adInitSpec; /// @@ -53,7 +52,7 @@ namespace Guru if (userAllow) { #if UNITY_IOS - CheckAttStatus(); + Instance.CheckAttStatus(); #else StartAdService(spec); #endif @@ -81,6 +80,8 @@ namespace Guru private bool _hasConsentCalled = false; private bool _adServiceHasStarted = false; + private string _notiStatue = ""; + private bool _hasNotiGranted = false; /// /// 启动Consent流程 @@ -88,10 +89,12 @@ namespace Guru /// private void StartConsentFlow() { + LogI($"#4.5 --- StartConsentFlow ---"); + float time = 1; if (!_adServiceHasStarted && _appServicesConfig != null) { - time = _appServicesConfig.IsAdsCompliance() ? 10 : 1f; // 启动合规判定后, 延迟最多 10 秒后启动广告 + time = _appServicesConfig.IsAdsCompliance() ? CONSENT_FLOW_TIMEOUT : 1f; // 启动合规判定后, 延迟最多 10 秒后启动广告 } Delay(time, AdServiceHandler); // 广告延迟启动 @@ -130,7 +133,7 @@ namespace Guru #if UNITY_IOS CheckAttStatus(); // [iOS] Consent 启动后检查 ATT 初始值 #elif UNITY_ANDROID - CheckPermission(); // [Android] Consent 回调后检查 Push 推送权限 + CheckNotiPermission(); // Consent 回调后检查 Notification 权限 #endif // 内部处理后继逻辑 @@ -154,6 +157,8 @@ namespace Guru StartAdService(_adInitSpec); } + + #endregion #region IOS ATT 广告授权流程 @@ -167,7 +172,7 @@ namespace Guru /// /// 显示系统的 ATT 弹窗 /// - public static void RequestAttDialog() + public void RequestAttDialog() { LogI($"RequestATTDialog"); ATTManager.RequestATTDailog(ReportAttStatus); @@ -186,7 +191,7 @@ namespace Guru /// /// iOS 平台检查 ATT 状态 /// - private static void CheckAttStatus(bool autoReCall = false) + private void CheckAttStatus(bool autoReCall = false) { _autoReCallAtt = autoReCall; @@ -194,7 +199,7 @@ namespace Guru Delay(1, () => ATTManager.CheckStatus(ReportAttStatus)); } - private static void ReportAttStatus(string status) + private void ReportAttStatus(string status) { LogI($"{Tag} --- Get Att status:{status} att Type:{_attType} recall:{_autoReCallAtt}"); SetUserProperty(Analytics.ParameterATTStatus, status); // 当前的状态 @@ -221,45 +226,66 @@ namespace Guru // ATT 状态已授权 break; } + + CheckNotiPermission(); // Consent 回调后检查 Notification 权限 } #endif #endregion - #region Android 13 PushNotification Permission - - private const string PERMISSION_POST_NOTIFICATION = "android.permission.POST_NOTIFICATIONS"; - - private void CheckPermission() - { - float delay = 1; - UnityEngine.Debug.Log($"---- Check PushPermission ---"); -#if UNITY_ANDROID - // 如果已经请求过权限的话, 则不做动作 - if (UnityEngine.Android.Permission.HasUserAuthorizedPermission(PERMISSION_POST_NOTIFICATION)) - { - UnityEngine.Debug.Log($"--- PushPermission has passed ---"); - return; - } -#endif - Invoke(nameof(RequestNotification), delay); - } - + #region Notification Permission Check + /// - /// 请求Notification + /// 初始化 Noti Service /// - private void RequestNotification() + private void InitNotiPermission() { -#if UNITY_ANDROID - UnityEngine.Debug.Log("--- Target 33 Request Notification ---"); - // Android直接申请授权 - if (!UnityEngine.Android.Permission.HasUserAuthorizedPermission(PERMISSION_POST_NOTIFICATION)) - { - UnityEngine.Android.Permission.RequestUserPermission(PERMISSION_POST_NOTIFICATION); - } -#endif + bool hasNotiGranted = false; + _notiStatue = "no_determined"; + NotificationService.Initialize(); // 初始化 Noti 服务 + SetNotiPerm(NotificationService.GetStatus()); } + /// + /// 检查 Noti 状态 + /// + private void CheckNotiPermission() + { + var status = NotificationService.GetStatus(); + + // 如果未启用自动 Noti 授权,则直接上报状态 + if (!_initConfig.AutoNotificationPermission) + { + UnityEngine.Debug.LogWarning($"[SDK] ---- AutoNotificationPermission is OFF, Project should request permission own."); + return; + } + + bool isGranted = NotificationService.IsPermissionGranted(); + UnityEngine.Debug.Log($"[SDK] ---- Check Noti Permission: {isGranted}"); + if (isGranted) + { + SetNotiPerm(status); + return; + } + + RequestNotificationPermission(); // 请求授权 + } + + /// + /// 请求推送授权 + /// + /// + public static void RequestNotificationPermission(Action callback = null) + { + UnityEngine.Debug.Log($"[SDK] ---- RequestNotificationPermission"); + NotificationService.RequestPermission(status => + { + UnityEngine.Debug.Log($"[SDK] ---- Get Notification Permission:{status}"); + SetNotiPerm(status); + callback?.Invoke(status); + }); + } + #endregion diff --git a/Runtime/Code/SDK/GuruSDK.Callback.cs b/Runtime/Code/SDK/GuruSDK.Callbacks.cs similarity index 100% rename from Runtime/Code/SDK/GuruSDK.Callback.cs rename to Runtime/Code/SDK/GuruSDK.Callbacks.cs diff --git a/Runtime/Code/SDK/GuruSDK.Callback.cs.meta b/Runtime/Code/SDK/GuruSDK.Callbacks.cs.meta similarity index 100% rename from Runtime/Code/SDK/GuruSDK.Callback.cs.meta rename to Runtime/Code/SDK/GuruSDK.Callbacks.cs.meta diff --git a/Runtime/Code/SDK/GuruSDK.System.cs b/Runtime/Code/SDK/GuruSDK.System.cs index 0d69685..be5a66c 100644 --- a/Runtime/Code/SDK/GuruSDK.System.cs +++ b/Runtime/Code/SDK/GuruSDK.System.cs @@ -1,3 +1,6 @@ +using System; +using UnityEngine; + namespace Guru { public partial class GuruSDK @@ -41,5 +44,48 @@ namespace Guru } #endregion + + + #region Android System + +#if UNITY_ANDROID + + + /// + /// 获取 AndroidSDK 的系统版本号 + /// + /// + public static int GetAndroidSystemVersion() + { + try + { + // sdkInt 是 Android SDK 的整数版本号,例如 Android 10 对应 29。 + // release 是 Android 版本的字符串表示,例如 "10"。 + using (AndroidJavaClass jc = new AndroidJavaClass("android.os.Build$VERSION")) + { + int sdkInt = jc.GetStatic("SDK_INT"); + Debug.LogWarning($"[SDK] --- Android SDK Version:{sdkInt}"); + return sdkInt; + } + } + catch (Exception ex) + { + Debug.LogError(ex); + } + + return 0; + } + + + +#endif + + + + + + #endregion + + } } \ No newline at end of file diff --git a/Runtime/Code/SDK/GuruSDK.cs b/Runtime/Code/SDK/GuruSDK.cs index 50b6cba..209bbd3 100644 --- a/Runtime/Code/SDK/GuruSDK.cs +++ b/Runtime/Code/SDK/GuruSDK.cs @@ -274,9 +274,11 @@ namespace Guru /// private void InitAllServices() { - //-------- Init Analytics --------- + // -------- Init Analytics --------- InitUserProperties(); SetSDKEventPriority(); + // -------- Init Noti ----------- + InitNotiPermission(); bool useKeywords = false; bool useIAP = _initConfig.IAPEnabled; @@ -413,7 +415,6 @@ namespace Guru // StartConsentFlow(); Try(() => { - LogI($"#4.5 --- StartConsentFlow ---"); StartConsentFlow(); }, ex => { diff --git a/package.json b/package.json index 8a8f360..aac4d90 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "com.guru.unity.sdk", "displayName": "Guru SDK", - "version": "1.0.13", + "version": "1.0.14", "description": "Guru SDK for unity project", "unity": "2020.3", "author":{