update: 新增 Notification 接口

dev
胡宇飞 2024-06-20 18:30:22 +08:00
parent cd7074f490
commit fa3ec5adef
7 changed files with 118 additions and 41 deletions

View File

@ -20,6 +20,10 @@ namespace Guru
/// </summary>
public bool IAPEnabled = true;
/// <summary>
/// 自动申请推送授权信息
/// </summary>
public bool AutoNotificationPermission = true;
/// <summary>
/// 自动记录完成的关卡
/// </summary>
public bool AutoRecordFinishedLevels = true;

View File

@ -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;
/// <summary>
@ -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;
/// <summary>
/// 启动Consent流程
@ -88,10 +89,12 @@ namespace Guru
/// </summary>
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
/// <summary>
/// 显示系统的 ATT 弹窗
/// </summary>
public static void RequestAttDialog()
public void RequestAttDialog()
{
LogI($"RequestATTDialog");
ATTManager.RequestATTDailog(ReportAttStatus);
@ -186,7 +191,7 @@ namespace Guru
/// <summary>
/// iOS 平台检查 ATT 状态
/// </summary>
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,43 +226,64 @@ namespace Guru
// ATT 状态已授权
break;
}
CheckNotiPermission(); // Consent 回调后检查 Notification 权限
}
#endif
#endregion
#region Android 13 PushNotification Permission
#region Notification Permission Check
private const string PERMISSION_POST_NOTIFICATION = "android.permission.POST_NOTIFICATIONS";
private void CheckPermission()
/// <summary>
/// 初始化 Noti Service
/// </summary>
private void InitNotiPermission()
{
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);
bool hasNotiGranted = false;
_notiStatue = "no_determined";
NotificationService.Initialize(); // 初始化 Noti 服务
SetNotiPerm(NotificationService.GetStatus());
}
/// <summary>
/// 请求Notification
/// 检查 Noti 状态
/// </summary>
private void RequestNotification()
private void CheckNotiPermission()
{
#if UNITY_ANDROID
UnityEngine.Debug.Log("--- Target 33 Request Notification ---");
// Android直接申请授权
if (!UnityEngine.Android.Permission.HasUserAuthorizedPermission(PERMISSION_POST_NOTIFICATION))
var status = NotificationService.GetStatus();
// 如果未启用自动 Noti 授权,则直接上报状态
if (!_initConfig.AutoNotificationPermission)
{
UnityEngine.Android.Permission.RequestUserPermission(PERMISSION_POST_NOTIFICATION);
UnityEngine.Debug.LogWarning($"[SDK] ---- AutoNotificationPermission is OFF, Project should request permission own.");
return;
}
#endif
bool isGranted = NotificationService.IsPermissionGranted();
UnityEngine.Debug.Log($"[SDK] ---- Check Noti Permission: {isGranted}");
if (isGranted)
{
SetNotiPerm(status);
return;
}
RequestNotificationPermission(); // 请求授权
}
/// <summary>
/// 请求推送授权
/// </summary>
/// <param name="callback"></param>
public static void RequestNotificationPermission(Action<string> callback = null)
{
UnityEngine.Debug.Log($"[SDK] ---- RequestNotificationPermission");
NotificationService.RequestPermission(status =>
{
UnityEngine.Debug.Log($"[SDK] ---- Get Notification Permission:{status}");
SetNotiPerm(status);
callback?.Invoke(status);
});
}

View File

@ -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
/// <summary>
/// 获取 AndroidSDK 的系统版本号
/// </summary>
/// <returns></returns>
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<int>("SDK_INT");
Debug.LogWarning($"[SDK] --- Android SDK Version:{sdkInt}");
return sdkInt;
}
}
catch (Exception ex)
{
Debug.LogError(ex);
}
return 0;
}
#endif
#endregion
}
}

View File

@ -277,6 +277,8 @@ namespace Guru
// -------- 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 =>
{

View File

@ -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":{