diff --git a/Runtime/GuruCore/Runtime/ExtensionKit/DeviceIDUtil.cs b/Runtime/GuruCore/Runtime/ExtensionKit/DeviceIDUtil.cs new file mode 100644 index 0000000..26451eb --- /dev/null +++ b/Runtime/GuruCore/Runtime/ExtensionKit/DeviceIDUtil.cs @@ -0,0 +1,46 @@ + +namespace Guru +{ + public class DeviceIDUtil + { + /// + /// UUID (V4) + /// + /// + public static string UUID => System.Guid.NewGuid().ToString(); + +#if UNITY_IOS + /// + /// IOS 或者 IDFV + /// 当获取到非法的值时, 用 UUID 代替 + /// + /// + public static string IDFV() + { + var idfv = UnityEngine.SystemInfo.deviceUniqueIdentifier; + if (string.IsNullOrEmpty(idfv) || idfv.Contains("0000-0000-0000")) + { + idfv = UUID; + } + return idfv; + } + +#endif + +#if UNITY_ANDROID + /// + /// Android ID + /// 可通过Unity 本身的接口来获取 + /// + /// + public static string AndroidID() + { + var aid = UnityEngine.SystemInfo.deviceUniqueIdentifier; + if (string.IsNullOrEmpty(aid)) aid = UUID; + return aid; + } +#endif + + + } +} \ No newline at end of file diff --git a/Runtime/GuruCore/Runtime/ExtensionKit/DeviceIDUtil.cs.meta b/Runtime/GuruCore/Runtime/ExtensionKit/DeviceIDUtil.cs.meta new file mode 100644 index 0000000..e94385b --- /dev/null +++ b/Runtime/GuruCore/Runtime/ExtensionKit/DeviceIDUtil.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: fc9bb1c0043f4fa58b3f0f8edd4f902f +timeCreated: 1706670074 \ No newline at end of file diff --git a/Runtime/GuruCore/Runtime/IPM/Scripts/DeviceUtil.cs b/Runtime/GuruCore/Runtime/IPM/Scripts/DeviceUtil.cs index 96d4b8f..bb0217f 100644 --- a/Runtime/GuruCore/Runtime/IPM/Scripts/DeviceUtil.cs +++ b/Runtime/GuruCore/Runtime/IPM/Scripts/DeviceUtil.cs @@ -33,13 +33,13 @@ namespace Guru private static void GetIOSDeviceInfo() { -#if UNITY_IOS && !UNITY_EDITOR +#if UNITY_IOS string content = iOSDeviceInfo(); Debug.Log($"GetDeviceInfo:{content}"); if(!string.IsNullOrEmpty(content)) { string[] infos = content.Split('$'); - IPMConfig.IPM_DEVICE_ID = infos[0]; + IPMConfig.SetDeviceId(infos[0]); IPMConfig.IPM_APP_VERSION = infos[1]; IPMConfig.IPM_TIMEZONE = infos[2]; IPMConfig.IPM_MODEL = infos[3]; @@ -49,6 +49,8 @@ namespace Guru IsGetDeviceInfoSuccess = true; } #endif + + } public static void SetiOSBadge() diff --git a/Runtime/GuruCore/Runtime/IPM/Scripts/IPMConfig.cs b/Runtime/GuruCore/Runtime/IPM/Scripts/IPMConfig.cs index 0eab7c4..8371208 100644 --- a/Runtime/GuruCore/Runtime/IPM/Scripts/IPMConfig.cs +++ b/Runtime/GuruCore/Runtime/IPM/Scripts/IPMConfig.cs @@ -12,6 +12,7 @@ namespace Guru public static string IPM_X_APP_ID => GuruSettings.Instance.IPMSetting.AppId; public static int TOKEN_VALID_TIME => GuruSettings.Instance.IPMSetting.TokenValidTime; public static readonly int FIREBASE_TOKEN_VALID_TIME = TimeUtil.HOUR_TO_SECOND; + public static bool UsingUUID = false; public static readonly string Header_Param_APPID = "X-APP-ID"; @@ -47,13 +48,50 @@ namespace Guru { if (string.IsNullOrEmpty(SavedDeviceId)) { - SavedDeviceId = SystemInfo.deviceUniqueIdentifier; // 目前使用 UNITY 接口来生成对应的 DeviceID + if (UsingUUID) + { + SavedDeviceId = GenerateDeviceIdV2(); + } + else + { + SavedDeviceId = GenerateDeviceIdV1(); + } } return SavedDeviceId; // 优先使用缓存的 DeviceID } } + + private static bool IsValidDeviceId(string deviceId) + { + return !(string.IsNullOrEmpty(deviceId) || deviceId.Contains("0000-0000-0000")); + } + /// - /// 缓存设备 ID + /// 用生成设备ID (V1) + /// 使用 Unity 的 SystemInfo.deviceUniqueIdentifier 作为设备 ID + /// + /// + private static string GenerateDeviceIdV1() + { + var did = SystemInfo.deviceUniqueIdentifier; + if (!IsValidDeviceId(did)) + { + //空串, 或者IOS 生成了无效的 IDFV, 则使用 DeviceIDUtil 代替 + did = DeviceIDUtil.UUID; + } + return did; + } + + /// + /// 用生成设备ID (V2) + /// 直接使用 DeviceIDUtil 作为设备的唯一标识 + /// + /// + private static string GenerateDeviceIdV2() => DeviceIDUtil.UUID; + + + /// + /// 缓存设备ID /// private static string SavedDeviceId { @@ -61,6 +99,20 @@ namespace Guru set => PlayerPrefs.SetString(nameof(SavedDeviceId), value); } + /// + /// 设置 DeviceID, 此处设计覆写原则 + /// + /// + /// + public static void SetDeviceId(string deviceId, bool overwrite = false) + { + // 不写入空字符串 + if (string.IsNullOrEmpty(deviceId)) return; + // 如果设备ID无效 或 需要覆盖写入 + if (!IsValidDeviceId(SavedDeviceId) || overwrite) + SavedDeviceId = deviceId; + } + #if DEBUG public static readonly string IPM_URL = "https://dev.saas.castbox.fm/"; #else