Skip to main content

Unity push notification integration

Last updated 10/07/2026

1. FCM push​

1.1 Report the "push ID"​

  • Upload the FCM token after calling AE Login or switching accounts.
//Initialize the AE SDK
TDAnalytics.Init("APPID", "SERVER");
//Set the account ID
TDAnalytics.Login("ACCOUNT_ID");
//Report the FCM token to AE
Firebase.Messaging.FirebaseMessaging.GetTokenAsync().ContinueWith(task => {
var result = task.Result;
//Switch to the main thread to report. For UnityMainThreadDispatcher, see the appendix
UnityMainThreadDispatcher.Instance.Enqueue(() =>
{
//Report the FCM token to AE
TDAnalytics.UserSet(new Dictionary<string, object>() {
{ "fcm_token", task.Result }
});
});
});
  • When the FCM token changes, update the user property:
private void Start()
{
// Register the FCM token update delegate
Firebase.Messaging.FirebaseMessaging.TokenReceived += OnTokenReceived;
Firebase.Messaging.FirebaseMessaging.MessageReceived += OnMessageReceived;
}

public void OnTokenReceived(object sender, Firebase.Messaging.TokenReceivedEventArgs token)
{
//Report the FCM token to AE
TDAnalytics.UserSet(new Dictionary<string, object>() {
{ "fcm_token", token.Token }
});
}

1.2 Track push click events​

Upload the push click event when the user clicks the notification. You can get the push parameters in the FCM OnMessageReceived callback.

private void Start()
{
// Register the FCM message delegate
Firebase.Messaging.FirebaseMessaging.MessageReceived += OnMessageReceived;
}

public void OnMessageReceived(object sender, Firebase.Messaging.MessageReceivedEventArgs e)
{
// Report the push event (for TETrackMessageReceived, see the appendix)
if (e.Message.Data.ContainsKey("te_extras"))
{
this.TETrackMessageReceived(e.Message.Data);
}
}

1.3 Handle push messages​

Use one of the following two solutions.

  • Regular parameters: Call the TEHandlePushAction method.
  • Passthrough parameters: Call the TEHandlePassThroughAction method.
private void Start()
{
// Register the FCM message delegate
Firebase.Messaging.FirebaseMessaging.MessageReceived += OnMessageReceived;
}

public void OnMessageReceived(object sender, Firebase.Messaging.MessageReceivedEventArgs e)
{
//To handle regular parameters, call the following method (for TEHandlePushAction, see the appendix)
this.TEHandlePushAction(e.Message.Data);
//To handle passthrough parameters, call the following method
this.TEHandlePassThroughAction(e.Message.Data);
}

2. JPush​

tip

JPush does not yet provide an official Unity plugin. You can refer to the open-source plugin maintained by JPush: JPush Unity Plugin. Everything below is implemented on top of the JPush Unity Plugin.

2.1 Report the "push ID"​

  • Upload the JPush Registration ID after calling AE Login or switching accounts.
//Initialize the AE SDK
TDAnalytics.Init("APPID", "SERVER");
//Set the account ID
TDAnalytics.Login("ACCOUNT_ID");
//Report the JPush Registration ID to AE
string jiguangId = JPushBinding.GetRegistrationId();
TDAnalytics.UserSet(new Dictionary<string, object>() {
{ "jiguang_id", jiguangId }
});
  • Upload the JPush Registration ID in the JPush OnGetRegistrationId callback.
void OnGetRegistrationId(string result)
{
//Report the JPush Registration ID to AE
TDAnalytics.UserSet(new Dictionary<string, object>() {
{ "jiguang_id", result }
});
}

2.2 Track push click events​

void OnOpenNotification(string jsonStr)
{
IDictionary<string, object> json = ThinkingData.Analytics.Utils.TDMiniJson.Deserialize(jsonStr);
if (json.ContainsKey("te_extras"))
{
object v = json["te_extras"];
this.TETrackMessageReceived((IDictionary<string, string>)v);
}
}

2.3 Handle push messages​

Use one of the following two solutions.

  • Regular parameters: Call the TEHandlePushAction method.
  • Passthrough parameters: Call the TEHandlePassThroughAction method.
void OnOpenNotification(string jsonStr)
{
Debug.Log("recv---openNotification---" + jsonStr);
IDictionary<string, object> json = ThinkingData.Analytics.Utils.TDMiniJson.Deserialize(jsonStr);
if (json.ContainsKey("extras"))
{
object v = json["extras"];
//To handle regular parameters, call the following method
this.TEHandlePushAction((IDictionary<string, string>)v);
//To handle passthrough parameters, call the following method
this.TEHandlePassThroughAction((IDictionary<string, string>)v);
}
}

Appendix​

Example of push parameters received by the client​

The following shows only the extended field parameters received by the client

{
"te_extras": {
//Jump mode when the push is clicked
"ops_loading_type": "OPEN_APP",
//Passthrough parameters
"passthrough_params": {
"param1": "abc",
"param2": 101,
"param3": [{
"subText1": "xyz",
"subText2": 2
}]
},
//AE operation channel receipt properties
"#ops_receipt_properties": {
"ops_task_id": "0082",
"ops_project_id": 1,
"ops_task_instance_id": "0082_20230331",
"ops_push_language": "default",
"ops_task_exec_detail_id": "55"
}
}
}

TETrackMessageReceived​

private void TETrackMessageReceived(IDictionary<string, string> data)
{
//Report the FCM message event to AE
Dictionary<string, object> te_extras = ThinkingData.Analytics.Utils.TDMiniJson.Deserialize(data["te_extras"]);
Dictionary<string, object> properties = new Dictionary<string, object>();
if (te_extras.ContainsKey("#ops_receipt_properties"))
{
properties.Add("#ops_receipt_properties", te_extras["#ops_receipt_properties"]);
}
TDAnalytics.Track("te_ops_push_click", properties);
TDAnalytics.Flush();
}

TEHandlePushAction​

private void TEHandlePushAction(IDictionary<string, string> data)
{
Dictionary<string, object> te_extras = ThinkingData.Analytics.Utils.TDMiniJson.Deserialize(data["te_extras"]);
string type = te_extras["ops_loading_type"].ToString();
if ("OPEN_APP".Equals(type))
{
// Handle the open App message --> launch the App
}
else if ("OPEN_URL".Equals(type))
{
string url = te_extras["ops_url"].ToString();
if (!string.IsNullOrEmpty(url))
{
// Handle the open URL message --> handle the URL
}
}
else if ("CUSTOMIZED".Equals(type))
{
string custom = te_extras["ops_customized"].ToString();
if (!string.IsNullOrEmpty(custom))
{
// Handle the custom message --> handle the custom message
}
}
}

TEHandlePassThroughAction​

private void TEHandlePassThroughAction(IDictionary<string, string> data)
{
Dictionary<string, object> te_extras = ThinkingData.Analytics.Utils.TDMiniJson.Deserialize(data["te_extras"]);
string passThroughParams = te_extras["passthrough_params"].ToString();
//passThroughParams is the passthrough parameter. Implement the specific business logic next
}

UnityMainThreadDispatcher​

public class UnityMainThreadDispatcher : MonoBehaviour
{
private static readonly Queue<Action> _executionQueue = new Queue<Action>();

public static UnityMainThreadDispatcher Instance { get; private set; }

private void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(this.gameObject);
}
else
{
Destroy(gameObject);
}
}

public void Update()
{
lock (_executionQueue)
{
while (_executionQueue.Count > 0)
{
_executionQueue.Dequeue().Invoke();
}
}
}

public void Enqueue(IEnumerator action)
{
lock (_executionQueue)
{
_executionQueue.Enqueue(() => { StartCoroutine(action); });
}
}

public void Enqueue(Action action)
{
Enqueue(ActionWrapper(action));
}

IEnumerator ActionWrapper(Action a)
{
a();
yield return null;
}
}
Was this page helpful?