2

我正在为 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;
                }


            }
        }
4

1 回答 1

2

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

如果您需要针对单个用户,建议在您的应用程序中调用idsAvailable并将其发送到您的服务器。您可以稍后使用创建通知include_player_idsREST API POST 调用中的字段向用户列表发送通知。

于 2016-06-13T20:56:44.360 回答