如何在使用OneSignal发送推送通知时发送一些附加数据?

13

我正在开发一款Android应用程序,并为其编写C# Web Api。现在,我可以使用以下代码发送推送通知。但是,我必须发送一个包含图像URL的JSON对象,以便当用户点击通知时,应用程序中的活动将打开并使用Picasso加载该URL中的图像。我该如何做?

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);
            }
        }
    }

使用这个 "message" 字符串,我也想发送一个JSON对象。


你应该回答自己的问题,这样问题就会被标记为已回答。 - AJ -
1个回答

2

我找到了解决这个问题的方法。

解决方案:OneSignal允许使用编码字符串中的"data"标签发送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();

你可以轻松地使用它。:)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接