如何使用OneSignal发送推送通知时发送一些其他数据?

时间:2016-06-06 10:32:33

标签: c# android json onesignal

我正在开发一个Android应用程序以及为它编写C#Web Api。现在我可以使用下面的代码发送推送通知。但我必须发送一个json对象,其中包含一个图像的URL,以便当用户单击该通知时,应用程序中的活动打开并使用该URL使用Picasso加载图像。我该怎么办?

private void SendPushNotifications(int userId)
    {
        string appId = "myAppId";
        var request = WebRequest.Create("https://onesignal.com/api/v1/notifications") as HttpWebRequest;
        var user = db.Users.FirstOrDefault(x => x.Id == userId);
        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 "+appId);

                byte[] byteArray = Encoding.UTF8.GetBytes("{"
                                                          + "\"app_id\": \"app_id\","
                                                          + "\"contents\": {\"en\": \""+ message +"\"},"
                                                          + "\"included_segments\": [\"All\"]}");

                string responseContent = null;

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

                    using (var response = request.GetResponse() as HttpWebResponse)
                    {
                        using (var reader = new StreamReader(response.GetResponseStream()))
                        {
                            responseContent = reader.ReadToEnd();
                        }
                    }
                }
                catch (WebException ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                    System.Diagnostics.Debug.WriteLine(new StreamReader(ex.Response.GetResponseStream()).ReadToEnd());
                }

                if (responseContent != null) System.Diagnostics.Debug.WriteLine(responseContent);
            }
        }
    }

用这个"消息" string我也想发送一个json对象。

1 个答案:

答案 0 :(得分:1)

因此,我找到了解决此问题的方法。

解决方案:OneSignal允许通过在我发送的编码字符串中使用“数据”标签来发送AdditionalData,如下所示:

byte[] byteArray = Encoding.UTF8.GetBytes("{"
             + "\"app_id\": \"app_id\","
             + "\"data\": {\"foo\": \"bear\"},"
             + "\"contents\": {\"en\": \"" + message + "\"},"
             + "\"included_segments\": [\"All\"]}");

因此在Android中,它将被映射到JsonObject AdditionalData

OneSignal.startInit(this)
        .setNotificationOpenedHandler(new OneSignal.NotificationOpenedHandler() {
            @Override
            public void notificationOpened(String message, JSONObject additionalData, boolean isActive) {
                Toast.makeText(MainActivity.this,message,Toast.LENGTH_SHORT).show();
            }
        })
        .init();

您可以轻松使用它。 :)