2

这是我使用 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"
    }
  } 

上面的代码在哪里传递我的请求参数(正文)?如何将我的请求设置为 JSON 格式?提前致谢。请帮我解决这个问题。

4

2 回答 2

10

使用 .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;
}
于 2013-06-21T11:07:54.177 回答
0

要向所有应用程序用户发送通知,您必须像这样设置数据字段:

postString = "{\"data\": { \"alert\": \"Test Notification 2 From Parse Via Chinwag Admin\" },\"where\": { \"deviceType\": \"ios\" }}";
于 2015-11-25T10:55:50.957 回答