如何在OneSignal中将特定用户添加到细分市场

时间:2016-06-13 06:34:48

标签: c# android onesignal

我正在为Android应用程序开发C#.NET MVC Web Api。目前,我正在使用OneSignal通过调用OneSignal Api并传递通知内容向用户发送推送通知。我需要知道如何将用户添加到特定的细分,以便我可以集体向个别用户以及该细分的用户发送通知。我搜索了他们的文档,但我不明白如何使用OneSignal.SendTag方法。那基本上怎么在Visual Studio中做呢?到目前为止,我已经这样做了:

string api_key = "dsabjd";
        var request = WebRequest.Create("https://onesignal.com/api/v1/notifications") as HttpWebRequest;
        if (user != null)
        {
            string message = "This job is posted by: \n" + user.Name + "\n" + user.Contact + "\n" +user.City;
            if (request != null)
            {
                request.KeepAlive = true;
                request.Method = "POST";
                request.ContentType = "application/json";

                request.Headers.Add("authorization", "Basic "+api_key);


                var serializer = new JavaScriptSerializer();
                var obj = new
                {
                    app_id = "1651",
                    contents = new { en = message },
                    //data = new { image = "http://dsadasdasd.png" },
                    data = new { image = imageUrl },
                    included_segments = new string[] { "All" }
                };
                var param = serializer.Serialize(obj);
                byte[] byteArray = Encoding.UTF8.GetBytes(param);

                try
                {
                    using (var writer = request.GetRequestStream())
                    {
                        writer.Write(byteArray, 0, byteArray.Length);
                    }

                    string responseContent=null;
                    using (var response = request.GetResponse() as HttpWebResponse)
                    {
                        using (var reader = new StreamReader(response.GetResponseStream()))
                        {
                            responseContent = reader.ReadToEnd();
                        }
                    }
                    if (responseContent != null)
                    {
                        // parsing the json returned by OneSignal Push API 
                        dynamic json = JObject.Parse(responseContent);
                        int noOfRecipients = json.recipients;
                        if (noOfRecipients > 0)
                        {
                            flag = true;
                        }
                    }

                }
                catch (WebException ex)
                {
                    flag = false;
                }


            }
        }

1 个答案:

答案 0 :(得分:1)

要设置代码,建议您在应用中使用OneSignal Android SDK中的sendTags离线支持,并为您处理重试。

如果您需要定位到单个用户,建议您在应用中调用idsAvailable并将其发送到您的服务器。您稍后可以使用create notification REST API POST调用上的include_player_ids字段向用户列表发送通知。