如何使用PARSE REST API与ASP.NET一起向特定设备发送通知?

时间:2014-11-11 09:34:50

标签: c# .net api

我可以在我的应用程序中仅向一个特定设备发送推送消息(可能带有设备令牌吗?)而不是发送到所有设备?

这是我的代码

    public static bool PushNotification(string pushMessage)
    {
        bool isPushMessageSend = false;

        string postString = "";
        string urlpath = "https://api.parse.com/1/push";
        var httpWebRequest = (HttpWebRequest)WebRequest.Create(urlpath);

        postString = "{ \"deviceType\": \"ios\"," +
                        "\"deviceToken\": \"xxxxxxxxxxx\"," +
                        "\"channels\": [\"\"], " +
                         "\"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;
    }

2 个答案:

答案 0 :(得分:1)

根据文档,您可以向查询发送通知。查询可以获得具有给定id的特定设备。

下面的代码是等效的:

curl -X POST \
-H "X-Parse-Application-Id: APP_ID" \
-H "X-Parse-REST-API-Key: API_KEY" \
-H "Content-Type: application/json" \
-d '{
      "where": {
      "installationId": "ca100000-ec00-0000-0000-000000000000"
    },
    "data": {
      "alert": "Special delivery! Just for you!"
    }
  }' \

https://api.parse.com/1/push

我用我的真实证书尝试过它,并且它有效。希望你能实现它的C#版本。

答案 1 :(得分:1)

感谢SafaOrhan,它使用C#:

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

      string urlpath = "https://api.parse.com/1/push";
      var httpWebRequest = (HttpWebRequest)WebRequest.Create(urlpath);

      string postString = "{ \"where\": { \"installationId\": \"DEVICE-INSTALATION-ID\" }, \"data\": { \"alert\": \"Special delivery! Just for you!\" } }";

      httpWebRequest.ContentType = "application/json";
      httpWebRequest.ContentLength = postString.Length;
      httpWebRequest.Headers.Add("X-Parse-Application-Id", "YOUR APP KEY");
      httpWebRequest.Headers.Add("X-Parse-REST-API-KEY", "YOUR 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;
 }