c#中的离子推送通知api

时间:2016-01-15 21:47:45

标签: c# ios asp.net ionic-framework

基于Tom Raz发布Ionic push notification api in c# with WebApi。我尝试使用离子推送通知api实现一个发送推送通知的方法,这是我的代码:

    public void sendToIonic(string regId, string msg) {

        using (var client = new HttpClient())
        {
            string data = "{ \"user_ids\":[\" "+ regId + "\"],\"notification\":{\"alert\":\" "+ msg + "\"}}";

            string json = Newtonsoft.Json.JsonConvert.SerializeObject(data);

            client.BaseAddress = new Uri("https://push.ionic.io/api/v1/push");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Add("X-Ionic-Application-Id", "1d74b1f2");
            var keyBase64 = "Basic %s" + "53a03dc7d9ce58511263e40580294f62af36b89be7cc4db2";
            client.DefaultRequestHeaders.Add("Authorization", keyBase64);
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "https://push.ionic.io/api/v1/push");

            request.Content = new StringContent(data, Encoding.UTF8, "application/json");
            var response = client.SendAsync(request).Result;
        }

    }

但我一直得到回复error 403 Forbidden;。这是完整的回复:

  

{StatusCode:403,ReasonPhrase:'Forbidden',版本:1.1,内容:System.Net.Http.StreamContent,标题:   {     连接:保持活力     Access-Control-Allow-Origin:*     Access-Control-Allow-Headers:Origin,X-Requested-With,Content-Type,Accept     日期:2016年1月15日星期五20:50:33 GMT     ETag:W /“35-nGlZv / eC8CyS + KJzQ1P9rg”     服务器:牛仔     通过:1.1 vegur     X-Powered-By:快递     内容长度:53     Content-Type:application / json;字符集= utf-8的   }}

不确定是什么问题。我被卡住了。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

var keyBase64 = "Basic %s" + "..."
  

%s这是在python中连接的。   您需要转换为Base64

您可以使用

client.DefaultRequestHeaders.Add("X-Ionic-Application-Id", "1d74b1f2");
var IONIC_PRIVATE_KEY_BASE_64 = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes("53a03dc7d9ce58511263e40580294f62af36b89be7cc4db2:")); // ends with two points
var keyBase64 = "Basic " + IONIC_PRIVATE_KEY_BASE_64 ;
client.DefaultRequestHeaders.Add("Authorization", keyBase64);

http://docs.ionic.io/docs/push-sending-push#section-authenticating-your-requests