Search code examples
jsonwcfresthttpwebrequestparse-platform

How do I send API push message with .Net / Parse.com? (C#)


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.


Solution

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