2

This is my application code for sending push message using PARSE

public static string ParseAuthenticate(string strUserName, string 
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.parse.com/1/push");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Headers.Add("X-Parse-Application-Id", "my app id");
httpWebRequest.Headers.Add("X-Parse-REST-API-KEY", "my rest api key"); 
httpWebRequest.Method = "POST";
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var responseText = streamReader.ReadToEnd();
return responseText;
}
}

Request body
{
    "channels": [
      "test"
    ],
    "data": {
      "alert": "12345"
    }
  } 

Above code where is pass my request parameter(body)? how to frame my request as JSON format? Thanks in advance.Please help me to solve this issue.

muthukumar
  • 148
  • 1
  • 10

2 Answers2

10

Bellow code is running for push notification using parse in .net.

private bool PushNotification(string pushMessage)
{
    bool isPushMessageSend = false;

    string postString = "";
    string urlpath = "https://api.parse.com/1/push";
    var httpWebRequest = (HttpWebRequest)WebRequest.Create(urlpath);
    postString = "{ \"channels\": [ \"Trials\"  ], " +
                     "\"data\" : {\"alert\":\"" + pushMessage + "\"}" +
                     "}";
    httpWebRequest.ContentType = "application/json";
    httpWebRequest.ContentLength = postString.Length;
    httpWebRequest.Headers.Add("X-Parse-Application-Id", "My Parse App Id");
    httpWebRequest.Headers.Add("X-Parse-REST-API-KEY", "My Rest API Key");
    httpWebRequest.Method = "POST";
    StreamWriter requestWriter = new StreamWriter(httpWebRequest.GetRequestStream());
    requestWriter.Write(postString);
    requestWriter.Close();
    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
        var responseText = streamReader.ReadToEnd();
        JObject jObjRes = JObject.Parse(responseText);
        if (Convert.ToString(jObjRes).IndexOf("true") != -1)
        {
            isPushMessageSend = true;
        }
    }

    return isPushMessageSend;
}
muthukumar
  • 148
  • 1
  • 10
  • Thanks. This was very helpful. If you get a 400 Bad Request response from Parse, you may want to add a try/catch WebException handler to assist with debugging and error handling. See [this answer](http://stackoverflow.com/questions/692342/net-httpwebrequest-getresponse-raises-exception-when-http-status-code-400-ba) for more info. – Tod Birdsall Jun 05 '15 at 18:37
  • This is good. But if you get an error with "Bytes to be written to the stream exceed the Content-Length bytes size specified." when using special characters or non-latin characters then you might want to comment out/remove the "httpWebRequest.ContentLength = postString.Length;" part – HenrikP Aug 18 '15 at 07:32
0

To send Notification to all app user you have to set the Data field like so:

postString = "{\"data\": { \"alert\": \"Test Notification 2 From Parse Via Chinwag Admin\" },\"where\": { \"deviceType\": \"ios\" }}";
Szabolcs Páll
  • 1,402
  • 6
  • 25
  • 31
  • Actually when you want to send a message to all users you have to use postString = "{ \"data\": { \"alert\": \"" + message + "\" } , \"where\" : { } }"; – Joost Schepel Jan 14 '16 at 12:45