#灵感
接入文档:https://inspire.sg.larksuite.com/docx/RPgndd77VofvNBxPcQ8lBMJngeb
渠道号:
![[Pasted image 20250814091956.png]]
hj和ds的生产环境配置:
测试环境:http://192.168.1.33:8080/platform_new/
正式环境:https://developer.ilnc.inspiregames.cn:8888/platform/
hj
123456
ds
123456
正式环境 用户信息
X项目:
junbao@inspiregames.cn
244466666
元素:
aide@inspiregames.cn
Yuan1234
正式环境 上报地址:
元素:ilnc.icongamesg.com
X项目:ilnc.doomsurvivor.com
参考接入逻辑:
using Firebase.Extensions;
using Ideatech;
using LuaInterface;
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Networking;
/// <summary>
/// FCM推送相关
/// </summary>
public class FirebaseMessageUtil
{
private static FirebaseMessageUtil _instance;
public bool isSuccess = false;//初始化是否成功的标志,在FirebaseUtils脚本中同步该标志位
private string fcmAppid_PT; //Application ID of fcm for PTServer
private string fcmAppid_MX; //Application ID of fcm for MXServer
private string fcmAppid_Test; //Application ID of fcm for TestServer
public string fcmToken; //token of fcm
private FirebaseMessageUtil() { }
public static FirebaseMessageUtil Instance
{
get
{
if (_instance == null)
{
_instance = new FirebaseMessageUtil();
}
return _instance;
}
}
[NoToLua]
public void InitializeFirebase()
{
Firebase.Messaging.FirebaseMessaging.MessageReceived += OnMessageReceived;
Firebase.Messaging.FirebaseMessaging.TokenReceived += OnTokenReceived;
if (!ThirdPartyConst.TryGetValue("fcmAppidpt", out fcmAppid_PT))
{
Debug.LogWarning("Failed to obtain the FCM application ID: fcmAppidpt");
}
if (!ThirdPartyConst.TryGetValue("fcmAppidmx", out fcmAppid_MX))
{
Debug.LogWarning("Failed to obtain the FCM application ID: fcmAppidmx");
}
if (!ThirdPartyConst.TryGetValue("fcmAppidtest", out fcmAppid_Test))
{
Debug.LogWarning("Failed to obtain the FCM application ID: fcmAppidtest");
}
}
[NoToLua]
public void RemoveFirebase()
{
Firebase.Messaging.FirebaseMessaging.MessageReceived -= OnMessageReceived;
Firebase.Messaging.FirebaseMessaging.TokenReceived -= OnTokenReceived;
}
private void OnMessageReceived(object sender, Firebase.Messaging.MessageReceivedEventArgs e)
{
Dictionary<string, object> args = new Dictionary<string, object>();
args.Add(NativeEventConst.EVENT_ID_KEY, NativeEventConst.FIREBASE_MESSAGE_RECEIVED);
var notification = e.Message.Notification;
if (notification != null)
{
args.Add("title", notification.Title);
args.Add("body", notification.Body);
var android = notification.Android;
if (android != null)
{
args.Add("android channel_id", android.ChannelId);
}
}
if (e.Message.From.Length > 0)
{
args.Add("from", e.Message.From);
}
if (e.Message.Link != null)
{
args.Add("link", e.Message.Link.ToString());
}
if (e.Message.Data.Count > 0)
{
foreach (KeyValuePair<string, string> item in e.Message.Data)
{
args.Add(item.Key, item.Value);
}
}
//NativeUtils.CallBackToLua(args);
}
private void OnTokenReceived(object sender, Firebase.Messaging.TokenReceivedEventArgs token)
{
fcmToken = token.Token;
Debug.Log("Received FCM Registration Token: " + token.Token);
}
/// <summary>
/// 检测json合法性
/// </summary>
/// <param name="jsonStr"></param>
/// <returns></returns>
private (bool isValid, string error) CheckJsonDataValid<TKey, TValue>(Dictionary<TKey, TValue> jsonMap)
{
if (jsonMap == null)
return (false, "The dictionary object is null");
var emptyItems = jsonMap
.Where(kv => IsInvalidValue(kv.Value))
.Select(kv => kv.Key.ToString())
.ToList();
return emptyItems.Count == 0
? (true, null)
: (false, $"The following key values are empty: {string.Join(", ", emptyItems)}");
}
private bool IsInvalidValue<T>(T value)
{
if (value == null) return true;
if (value is string str) return string.IsNullOrEmpty(str);
return false;
}
/// <summary>
/// 供Lua层调用,设备上报
/// </summary>
public void EquipmentPost(string url, string jsonData)
{
if (isSuccess)
{
//检测json键值合法性
Dictionary<string, string> paramsMap = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonData);
var (isValid, error) = CheckJsonDataValid(paramsMap);
if (!isValid)
{
Debug.LogWarning($"Equipment reporting failed: {error}");
return;
}
CoroutineManager.AddCoroutine(PostRequest(url, jsonData));
}
else
{
Debug.LogWarning("The device failed to report and the SDK was not successfully initialized");
}
}
[System.Serializable]
private class PostResponse
{
public int code;
public string message;
public string data;
}
private IEnumerator PostRequest(string url, string jsonData)
{
using (UnityWebRequest request = UnityWebRequest.Post(url, "POST"))
{
byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(jsonData);
request.uploadHandler = new UploadHandlerRaw(bodyRaw);
request.downloadHandler = new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
yield return request.SendWebRequest();
if (request.result != UnityWebRequest.Result.Success)
{
Debug.LogWarning("Equipment reporting failed." + request.error);
yield break;
}
var response = JsonUtility.FromJson<PostResponse>(request.downloadHandler.text);
if (response.code != 0)
{
Debug.LogWarning("The device failed to report the error code" + response.code);
}
};
}
/// <summary>
/// 供Lua调用,订阅消息主题,订阅成功后可以通过主题推送
/// </summary>
/// <param name="topic"></param>
public void SubscribeTopic(string topicJsonStr)
{
if (!isSuccess)
{
Debug.LogWarning("Subscription to the topic failed! The SDK was not initialized successfully");
return;
}
//检测json键值合法性
Dictionary<string, string> paramsMap = JsonConvert.DeserializeObject<Dictionary<string, string>>(topicJsonStr);
var (isValid, error) = CheckJsonDataValid(paramsMap);
if (!isValid)
{
Debug.LogWarning($"Failed to subscribe to the topic: {error}");
return;
}
foreach (string key in paramsMap.Keys)
{
Firebase.Messaging.FirebaseMessaging.SubscribeAsync(paramsMap[key]).ContinueWithOnMainThread(task =>
{
if (task.IsFaulted)
Debug.LogWarning($"Failed to subscribe to the topic: {task.Exception}");
});
}
}
/// <summary>
/// 供Lua调用,根据服务器类型,获取应用ID
/// </summary>
/// <param name="serverCode"></param>
/// <returns></returns>
public string GetAppIdByServer(string serverCode)
{
if (string.IsNullOrEmpty(serverCode))
{
return fcmAppid_Test;
}
else
{
string lowerServerCode = serverCode.ToLower();
if (lowerServerCode == "pt")
{
return fcmAppid_PT;
}
else if (lowerServerCode == "es")
{
return fcmAppid_MX;
}
else
{
return "";
}
}
}
}