|
|
@@ -20,6 +20,7 @@ public class FirebaseCloudMessage : MonoBehaviour
|
|
|
{
|
|
|
|
|
|
private static string firebaseMessageReportUrl = String.Empty; //灵光平台 上报地址
|
|
|
+ private static string firebaseMessageNotificationUrl = String.Empty; //灵光平台 触达率 上报地址
|
|
|
|
|
|
private static string _android_channel; //安卓渠道号(和服务端确认即可)
|
|
|
private static string _ios_channel; //ios渠道号(和服务端确认即可)
|
|
|
@@ -31,8 +32,10 @@ public class FirebaseCloudMessage : MonoBehaviour
|
|
|
private static string userID; //玩家ID
|
|
|
private static bool inited = false; //是否初始化完成
|
|
|
|
|
|
- private string _platform; //移动平台
|
|
|
- private string _channel; //渠道号
|
|
|
+ private static string _platform; //移动平台
|
|
|
+ private static string _channel; //渠道号
|
|
|
+
|
|
|
+ private static bool isFirebaseEnable = false; //判断firebase依赖是否正常,云推送后是否可用
|
|
|
|
|
|
// private static FirebaseApp app;
|
|
|
|
|
|
@@ -47,7 +50,6 @@ public class FirebaseCloudMessage : MonoBehaviour
|
|
|
/// <param name="country">国家</param>
|
|
|
public static void Init(string appID,string ReportUrl,string android_channel = "2001",string ios_channel = "1001",string language = "en",string country = "US")
|
|
|
{
|
|
|
-
|
|
|
Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread(task => {
|
|
|
var dependencyStatus = task.Result;
|
|
|
if (dependencyStatus == Firebase.DependencyStatus.Available) {
|
|
|
@@ -55,17 +57,26 @@ public class FirebaseCloudMessage : MonoBehaviour
|
|
|
// where app is a Firebase.FirebaseApp property of your application class.
|
|
|
// app = Firebase.FirebaseApp.DefaultInstance;
|
|
|
firebaseMessageReportUrl = $"{ReportUrl}/api/app/{appID}/user/update";
|
|
|
+ firebaseMessageNotificationUrl = $"{ReportUrl}/api/app/{appID}/user/click";
|
|
|
_android_channel = android_channel;
|
|
|
_ios_channel = ios_channel;
|
|
|
_language = language;
|
|
|
_country = country;
|
|
|
|
|
|
RequestNotificationPermission();
|
|
|
+ isFirebaseEnable = true;
|
|
|
+
|
|
|
+ // 如果已经有userID,立即初始化Firebase
|
|
|
+ if (!string.IsNullOrEmpty(userID))
|
|
|
+ {
|
|
|
+ GetInstance().InitializeFirebase();
|
|
|
+ }
|
|
|
|
|
|
// Set a flag here to indicate whether Firebase is ready to use by your app.
|
|
|
}
|
|
|
else {
|
|
|
Debug.Log("当前设备 谷歌框架等依赖缺失 不支持Firebase");
|
|
|
+ isFirebaseEnable = false;
|
|
|
// UnityEngine.Debug.LogError(System.String.Format(
|
|
|
// "Could not resolve all Firebase dependencies: {0}", dependencyStatus));
|
|
|
// Firebase Unity SDK is not safe to use here.
|
|
|
@@ -75,7 +86,7 @@ public class FirebaseCloudMessage : MonoBehaviour
|
|
|
|
|
|
/// <summary>
|
|
|
/// 设置玩家ID
|
|
|
- /// </summary>
|
|
|
+ /// </summary>w
|
|
|
/// <param name="userid"></param>
|
|
|
public static void SetUserID(string userid)
|
|
|
{
|
|
|
@@ -83,9 +94,19 @@ public class FirebaseCloudMessage : MonoBehaviour
|
|
|
Debug.Log($"Firebase 功能测试 设置玩家ID{userid}");
|
|
|
#endif
|
|
|
userID = userid;
|
|
|
+
|
|
|
#if !UNITY_EDITOR
|
|
|
- // 设置完用户ID后,初始化Firebase获取token
|
|
|
- GetInstance().InitializeFirebase();
|
|
|
+ // 如果Firebase可用,直接初始化;如果不可用,等依赖检查完成后再初始化
|
|
|
+ // 如果Firebase还没初始化完成,userID已经缓存了,等Init完成后再调用InitializeFirebase
|
|
|
+ if (isFirebaseEnable)
|
|
|
+ {
|
|
|
+ GetInstance().InitializeFirebase();
|
|
|
+
|
|
|
+ if (!String.IsNullOrEmpty(fcmToken) && !String.IsNullOrEmpty(userID))
|
|
|
+ {
|
|
|
+ PostToServer();
|
|
|
+ }
|
|
|
+ }
|
|
|
#endif
|
|
|
}
|
|
|
/// <summary>
|
|
|
@@ -94,6 +115,11 @@ public class FirebaseCloudMessage : MonoBehaviour
|
|
|
/// <param name="topic"></param>
|
|
|
public static void SubscribeTopic(string topicJsonStr)
|
|
|
{
|
|
|
+ if (!isFirebaseEnable)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
if (String.IsNullOrEmpty(fcmToken))
|
|
|
{
|
|
|
Debug.LogError("灵光系统 无法获得fcm token令牌");
|
|
|
@@ -125,6 +151,11 @@ public class FirebaseCloudMessage : MonoBehaviour
|
|
|
/// <param name="topicJsonStr"></param>
|
|
|
public static void unSubscribeTopic(string topicJsonStr)
|
|
|
{
|
|
|
+ if (!isFirebaseEnable)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
if (String.IsNullOrEmpty(fcmToken))
|
|
|
{
|
|
|
Debug.LogError("灵光系统 无法获得fcm token令牌");
|
|
|
@@ -175,11 +206,24 @@ public class FirebaseCloudMessage : MonoBehaviour
|
|
|
/// </summary>
|
|
|
private void InitializeFirebase()
|
|
|
{
|
|
|
+ if (!isFirebaseEnable)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
// 避免重复初始化
|
|
|
if (inited)
|
|
|
{
|
|
|
return;
|
|
|
}
|
|
|
+
|
|
|
+ // 检查是否有userID,没有的话等待SetUserID调用
|
|
|
+ if (string.IsNullOrEmpty(userID))
|
|
|
+ {
|
|
|
+ Debug.Log("等待userID设置后再初始化Firebase");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
inited = true;
|
|
|
|
|
|
Firebase.Messaging.FirebaseMessaging.MessageReceived += OnMessageReceived;
|
|
|
@@ -191,6 +235,11 @@ public class FirebaseCloudMessage : MonoBehaviour
|
|
|
/// </summary>
|
|
|
private void RemoveFirebase()
|
|
|
{
|
|
|
+ if (!isFirebaseEnable)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
Firebase.Messaging.FirebaseMessaging.MessageReceived -= OnMessageReceived;
|
|
|
Firebase.Messaging.FirebaseMessaging.TokenReceived -= OnTokenReceived;
|
|
|
inited = false;
|
|
|
@@ -234,6 +283,25 @@ public class FirebaseCloudMessage : MonoBehaviour
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ //消息中获取traceId 发送给服务端 触达率
|
|
|
+ if (e.Message.Data.Count > 0)
|
|
|
+ {
|
|
|
+ foreach (KeyValuePair<string, string> item in e.Message.Data)
|
|
|
+ {
|
|
|
+ if (item.Key.Equals("traceId"))
|
|
|
+ {
|
|
|
+ string traceId = item.Value;
|
|
|
+#if Firebase_Debug
|
|
|
+ Debug.Log($"Firebase 功能测试 触达率id:{traceId}");
|
|
|
+#endif
|
|
|
+
|
|
|
+ // Debug.Log("OnMessageReceived traceId:" + traceId);
|
|
|
+ CoroutineManager.Instance.StartManagedCoroutine(NotificationPostRequest(firebaseMessageNotificationUrl, traceId));
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
private void OnTokenReceived(object sender, Firebase.Messaging.TokenReceivedEventArgs token)
|
|
|
@@ -248,7 +316,7 @@ public class FirebaseCloudMessage : MonoBehaviour
|
|
|
/// <summary>
|
|
|
/// 发送给灵光服务器
|
|
|
/// </summary>
|
|
|
- private void PostToServer()
|
|
|
+ private static void PostToServer()
|
|
|
{
|
|
|
SetPlatform();
|
|
|
var deviceData = new
|
|
|
@@ -269,7 +337,7 @@ public class FirebaseCloudMessage : MonoBehaviour
|
|
|
/// <summary>
|
|
|
/// 设置 移动平台 & 渠道号相关(渠道号和服务端确认即可)
|
|
|
/// </summary>
|
|
|
- private void SetPlatform()
|
|
|
+ private static void SetPlatform()
|
|
|
{
|
|
|
switch (Application.platform)
|
|
|
{
|
|
|
@@ -463,10 +531,7 @@ public class FirebaseCloudMessage : MonoBehaviour
|
|
|
|
|
|
if (request.result != UnityWebRequest.Result.Success)
|
|
|
{
|
|
|
-#if Firebase_Debug
|
|
|
- Debug.Log($"Firebase 功能测试,错误码:{request.error}");
|
|
|
-#endif
|
|
|
- Debug.LogWarning("Equipment reporting failed." + request.error);
|
|
|
+ Debug.LogError($"灵光系统 错误信息:{request.error}");
|
|
|
yield break;
|
|
|
}
|
|
|
|
|
|
@@ -483,7 +548,34 @@ public class FirebaseCloudMessage : MonoBehaviour
|
|
|
}
|
|
|
};
|
|
|
}
|
|
|
+
|
|
|
+ private IEnumerator NotificationPostRequest(string url,string traceId)
|
|
|
+ {
|
|
|
+ using (UnityWebRequest request = UnityWebRequest.PostWwwForm(url, "POST"))
|
|
|
+ {
|
|
|
+ byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(traceId);
|
|
|
+
|
|
|
+ request.uploadHandler = new UploadHandlerRaw(bodyRaw);
|
|
|
+ request.downloadHandler = new DownloadHandlerBuffer();
|
|
|
+ request.SetRequestHeader("Content-Type", "text/plain");
|
|
|
+
|
|
|
+ yield return request.SendWebRequest();
|
|
|
+
|
|
|
+ if (request.result != UnityWebRequest.Result.Success)
|
|
|
+ {
|
|
|
+ Debug.LogWarning("灵光系统 触达率 错误信息:" + request.error);
|
|
|
+ yield break;
|
|
|
+ }
|
|
|
+
|
|
|
+ var response = JsonUtility.FromJson<PostResponse>(request.downloadHandler.text);
|
|
|
+ if (response.code != 0)
|
|
|
+ {
|
|
|
+ Debug.LogWarning("灵光系统 触达率 错误码:" + response.code);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
```
|
|
|
|
|
|
## NotificationServiceExtensionCreator.cs
|
|
|
@@ -500,13 +592,14 @@ using UnityEditor.iOS.Xcode.PBX;
|
|
|
using System.Diagnostics;
|
|
|
using System.Threading;
|
|
|
using System;
|
|
|
+using System.Text.RegularExpressions;
|
|
|
using Debug = UnityEngine.Debug;
|
|
|
|
|
|
public static class FirebaseNotificationServiceExtensionCreator
|
|
|
{
|
|
|
private const string ExtensionName = "FirebaseNotificationService";
|
|
|
|
|
|
- [PostProcessBuild(200)]
|
|
|
+ [PostProcessBuild(99999)]
|
|
|
public static void OnPostProcessBuild(BuildTarget target, string buildPath)
|
|
|
{
|
|
|
if (target != BuildTarget.iOS) return;
|
|
|
@@ -756,104 +849,33 @@ public static class FirebaseNotificationServiceExtensionCreator
|
|
|
|
|
|
if (!podfileContent.Contains("target 'FirebaseNotificationService'"))
|
|
|
{
|
|
|
- // 检测 UnityFramework 中的 Firebase 版本
|
|
|
- string firebaseVersion = DetectFirebaseVersionInUnityFramework(podfileContent);
|
|
|
+ // 查找 UnityFramework target 并添加子 target
|
|
|
+ string pattern = @"(target\s+'UnityFramework'\s+do[^}]*?)(end)";
|
|
|
+ string replacement = $"$1\n target 'FirebaseNotificationService' do\n end\n$2";
|
|
|
|
|
|
- // 在文件末尾添加 FirebaseNotificationService target
|
|
|
- string firebaseTargetContent = $@"
|
|
|
-
|
|
|
-target 'FirebaseNotificationService' do
|
|
|
- pod 'Firebase/Messaging', '{firebaseVersion}'
|
|
|
-end
|
|
|
-
|
|
|
-use_frameworks! :linkage => :static";
|
|
|
-
|
|
|
- podfileContent += firebaseTargetContent;
|
|
|
- File.WriteAllText(podfilePath, podfileContent);
|
|
|
-
|
|
|
-#if Firebase_Debug
|
|
|
- Debug.Log($"FireBase功能测试 在现有 Podfile 中添加 FirebaseNotificationService target,使用 Firebase 版本: {firebaseVersion}");
|
|
|
-#endif
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
-#if Firebase_Debug
|
|
|
- Debug.Log("FireBase功能测试 FirebaseNotificationService target 已存在于 Podfile 中");
|
|
|
-#endif
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// 检测 UnityFramework target 中的 Firebase 版本
|
|
|
- /// </summary>
|
|
|
- private static string DetectFirebaseVersionInUnityFramework(string podfileContent)
|
|
|
- {
|
|
|
- // 默认版本
|
|
|
- string defaultVersion = "12.0.0";
|
|
|
-
|
|
|
- try
|
|
|
- {
|
|
|
- // 查找 UnityFramework target 中的 Firebase 版本
|
|
|
- int unityFrameworkStart = podfileContent.IndexOf("target 'UnityFramework' do");
|
|
|
- if (unityFrameworkStart == -1)
|
|
|
- {
|
|
|
- Debug.LogWarning("FireBase功能测试 未找到 UnityFramework target,使用默认版本: " + defaultVersion);
|
|
|
- return defaultVersion;
|
|
|
- }
|
|
|
-
|
|
|
- int unityFrameworkEnd = podfileContent.IndexOf("end", unityFrameworkStart);
|
|
|
- if (unityFrameworkEnd == -1)
|
|
|
- {
|
|
|
- Debug.LogWarning("FireBase功能测试 UnityFramework target 格式错误,使用默认版本: " + defaultVersion);
|
|
|
- return defaultVersion;
|
|
|
- }
|
|
|
-
|
|
|
- // 提取 UnityFramework target 的内容
|
|
|
- string unityFrameworkContent = podfileContent.Substring(unityFrameworkStart, unityFrameworkEnd - unityFrameworkStart);
|
|
|
-
|
|
|
- // 查找 Firebase 相关的 pod 声明
|
|
|
- string[] firebasePods = { "Firebase/Analytics", "Firebase/Core", "Firebase/Messaging", "Firebase" };
|
|
|
- string detectedVersion = null;
|
|
|
-
|
|
|
- foreach (string pod in firebasePods)
|
|
|
- {
|
|
|
- int podIndex = unityFrameworkContent.IndexOf($"pod '{pod}'");
|
|
|
- if (podIndex != -1)
|
|
|
- {
|
|
|
- // 找到 pod 声明,提取版本号
|
|
|
- int versionStart = unityFrameworkContent.IndexOf("'", podIndex + pod.Length + 6);
|
|
|
- if (versionStart != -1)
|
|
|
- {
|
|
|
- int versionEnd = unityFrameworkContent.IndexOf("'", versionStart + 1);
|
|
|
- if (versionEnd != -1)
|
|
|
- {
|
|
|
- detectedVersion = unityFrameworkContent.Substring(versionStart + 1, versionEnd - versionStart - 1);
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (!string.IsNullOrEmpty(detectedVersion))
|
|
|
+ if (System.Text.RegularExpressions.Regex.IsMatch(podfileContent, pattern, System.Text.RegularExpressions.RegexOptions.Singleline))
|
|
|
{
|
|
|
+ podfileContent = System.Text.RegularExpressions.Regex.Replace(podfileContent, pattern, replacement, System.Text.RegularExpressions.RegexOptions.Singleline);
|
|
|
+ File.WriteAllText(podfilePath, podfileContent);
|
|
|
+
|
|
|
#if Firebase_Debug
|
|
|
- Debug.Log($"FireBase功能测试 检测到 UnityFramework 中的 Firebase 版本: {detectedVersion}");
|
|
|
+ Debug.Log("FireBase功能测试 在 UnityFramework target 内添加 FirebaseNotificationService 子 target");
|
|
|
#endif
|
|
|
- return detectedVersion;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- Debug.LogWarning("FireBase功能测试 未在 UnityFramework 中找到 Firebase 版本信息,使用默认版本: " + defaultVersion);
|
|
|
- return defaultVersion;
|
|
|
+ Debug.LogWarning("FireBase功能测试 未找到 UnityFramework target,无法添加子 target");
|
|
|
}
|
|
|
}
|
|
|
- catch (Exception ex)
|
|
|
+ else
|
|
|
{
|
|
|
- Debug.LogError($"FireBase功能测试 检测 Firebase 版本时发生错误: {ex.Message}");
|
|
|
- return defaultVersion;
|
|
|
+#if Firebase_Debug
|
|
|
+ Debug.Log("FireBase功能测试 FirebaseNotificationService target 已存在于 Podfile 中");
|
|
|
+#endif
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// 执行 pod install 安装 Firebase 通知扩展组件
|
|
|
/// </summary>
|