如何使用 .Net/Parse.com 发送 API 推送消息?(C#) [英] How do I send API push message with .Net / Parse.com? (C#)

查看:24
本文介绍了如何使用 .Net/Parse.com 发送 API 推送消息?(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我使用 PARSE 发送推送消息的应用程序代码

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"
    }
  } 

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

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.

推荐答案

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

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

这篇关于如何使用 .Net/Parse.com 发送 API 推送消息?(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆