How to send some additional data when sending push notifications using OneSignal?

I am developing an Android application and also writing a C # Web Api spelling for it. Now I can send push notifications using the code below. But I have to send a json object that will contain the URL for the image so that when the user clicks the notification, the activity in the application opens and using this URL loads the image using Picasso. How can I do it?

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

with this line "message" I also want to send a json object.

Edit + Solution: OneSignal allows you to send additional data using the "data" tag in the encoded line that I send, as shown below:

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

So, in Android, it will be displayed in JsonObject addData p>

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

And you can easily use it. :)

+8
json android c # onesignal
source share

No one has answered this question yet.

See related questions:

2480
How do I send JSON data using Curl from a terminal / command line in Test Spring REST?
1270
How to transfer data between actions in an Android application?
928
How to write JSON data to a file?
809
How to send an object from one Android activity to another using intentions?
251
Android push notifications
4
OneSignal Push Notifications User Segmentation
2
Onesignal: how to send web push notifications only to some users?
0
Send OneSignal Timeout Notification
0
OneSignal push notification filter

All Articles