目录
此内容是否有帮助?

# Unity Push Integration Document

# FCM push

# 1.1 Report "Push ID"

  • Track the FCM Token after calling TE Login or switching accounts
//initialize TE SDK
ThinkingAnalyticsAPI.StartThinkingAnalytics("APP_ID", "SERVER_URL");
//set account ID
ThinkingAnalyticsAPI.Login("ACCOUNT_ID");
//report FCM token to TE system
Firebase.Messaging.FirebaseMessaging.GetTokenAsync().ContinueWith(task => {
    var result = task.Result;
    ThinkingAnalyticsAPI.UserSet(new Dictionary<string, object>() { 
        { "fcm_token", task.Result } 
    });
});
  • When FCM Token changes, update user properties:
private void Start()
{
    // register FCM Token and update delegate event
    Firebase.Messaging.FirebaseMessaging.TokenReceived += OnTokenReceived;
    Firebase.Messaging.FirebaseMessaging.MessageReceived += OnMessageReceived;
}

public void OnTokenReceived(object sender, Firebase.Messaging.TokenReceivedEventArgs token)
{
    //report FCM token to TE system
    ThinkingAnalyticsAPI.UserSet(new Dictionary<string, object>() { 
        { "fcm_token", token.Token } 
    });
}

# 1.2. Acquire push click events

When clicking on the notification, the user can track the push click event and get the push parameters in onCreate or onNewIntent.

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

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

# 1.3. Handle push messages

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

public void OnMessageReceived(object sender, Firebase.Messaging.MessageReceivedEventArgs e)
{
    // handle push messages (see appendix for TEHandlePushAction)
    this.TEHandlePushAction(e.Message.Data);
}

# JPush

WARNING

JPush official website does not provide Unity plugin yet, and you can refer to the open source plugin maintained by URORA: JPush Unity Plugin (opens new window).The following are implemented on the basis of JPush Unity Plugin (opens new window).

# 2.1 Report "Push ID"

  • Track the Registration ID of JPush after calling TE Login or switching accounts.
//initialize TE SDK
ThinkingAnalyticsAPI.StartThinkingAnalytics("APP_ID", "SERVER_URL");
//set account ID
ThinkingAnalyticsAPI.Login("ACCOUNT_ID");
// report JPush Registration ID to TE system
string jiguangId = JPushBinding.GetRegistrationId();
ThinkingAnalyticsAPI.UserSet(new Dictionary<string, object>() { 
    { "jiguang_id", jiguangId } 
});
  • Track the Registration ID of URORA in the OnGetRegistrationId of URORA.
void OnGetRegistrationId(string result)
{
    //report JPush Registration ID to TE system
    ThinkingAnalyticsAPI.UserSet(new Dictionary<string, object>() { 
        { "jiguang_id", jiguangId } 
    });
}

# 2.2. Acquire push click events

void OnReceiveNotification(string jsonStr)
{
    IDictionary<string, object> json = ThinkingAnalytics.Utils.TD_MiniJSON.Deserialize(jsonStr);
    if (json.ContainsKey("extras"))
    {
        object v = json["extras"];
        this.TETrackMessageReceived((IDictionary<string, string>)v);
    }
}

# 2.3. Handle push messages

void OnOpenNotification(string jsonStr)
{
    Debug.Log("recv---openNotification---" + jsonStr);
    IDictionary<string, object> json = ThinkingAnalytics.Utils.TD_MiniJSON.Deserialize(jsonStr);
    if (json.ContainsKey("extras"))
    {
        object v = json["extras"];
        this.TEHandlePushAction((IDictionary<string, string>)v);
    }
}

# Appendix

# TETrackMessageReceived

private void TETrackMessageReceived(IDictionary<string, string> data)
{
    //report FCM token to TE system
    Dictionary<string, object> te_extras = ThinkingAnalytics.Utils.TD_MiniJSON.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"]);
    }
    ThinkingAnalytics.ThinkingAnalyticsAPI.Track("ops_push_click", properties);
}

# TEHandlePushAction

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