【问题标题】:Ionic push notification api in c# with WebApi带有WebApi的c#中的离子推送通知api
【发布时间】:2017-06-11 04:02:32
【问题描述】:

我正在尝试使用 ionic 推送通知 api、使用 c# 和 WebApi 发送推送通知。 ionic 网站上的 python 示例运行良好,但我无法在 c# 中运行,尽管请求似乎是相同的。 这是我的代码:

 using (var client = new HttpClient())
            {

                client.BaseAddress = new Uri("https://push.ionic.io/api/v1/");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Add("X-Ionic-Application-Id", IONIC_APP_ID);
                //client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                var keyBase64 = "Basic " + IONIC_PRIVATE_KEY_BASE_64;
                client.DefaultRequestHeaders.Add("Authorization", keyBase64);
                client.DefaultRequestHeaders.Add("Accept-Encoding", "identity");
                //client.DefaultRequestHeaders.Add("Content-Type", "application/json");


                var response = client.PostAsJsonAsync("push", json).Result;
                if (response.IsSuccessStatusCode)
                {
                    int a = 6;
                }
            }

我不断收到错误的请求 (400),没有进一步的解释。

【问题讨论】:

    标签: c# asp.net-web-api ionic-framework ionic


    【解决方案1】:

    好的,解决了! 问题出在 Content-Type 标头上,“PostAsJsonAsync”方法的默认内容类型是“application/json; charset=utf-8”,而 api 需要“application/json”。

    这行得通:

            using (var client = new HttpClient())
            {
    
                client.BaseAddress = new Uri(API_URL);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Add("X-Ionic-Application-Id", IONIC_APP_ID);
                var keyBase64 = "Basic " + IONIC_PRIVATE_KEY_BASE_64;
                client.DefaultRequestHeaders.Add("Authorization", keyBase64);
                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, api);
                request.Content = new StringContent(json, Encoding.UTF8, "application/json");
                var response = client.SendAsync(request).Result;                
            }
    

    为了清楚起见,调用函数是:

    public void Send(string regId, string msg, int notificationId)
        {
            dynamic data = new ExpandoObject();
            data.tokens = new List<string>() {regId};
            data.notification = new ExpandoObject() as dynamic;
            data.notification.alert = msg;
    
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(data);
            log.InfoFormat("Sending notifcation to {0}, message is {1} ", regId, msg);
            SendToIonic("push", json);           
    
        }
    

    【讨论】:

    • HttpRequestMessage 集中的“api”变量来自哪里?这也是你的 API_URL 吗?
    • 你的 json 是什么样的?
    • 在这种情况下,“API”是“推送”,请参阅我对 json 的答案编辑
    【解决方案2】:

    这是你的做法:

    [HttpGet]
        [Route("A_Test")]
        public HttpResponseMessage A_Test()
        {
    
            HttpResponseMessage response;
    
            try
            {
    
                string regId = "";
                string profile = "<Name of Security Profile>";
                string msg = "Test";
    
                string data = "{ \"tokens\":[],\"send_to_all\":" + "true" + ",\"profile\":\"" + profile + "\",\"notification\":{\"message\":\"" + msg + "\"}}";
    
                using (var client = new HttpClient())
                {
    
                    client.BaseAddress = new Uri("https://api.ionic.io/push/notifications");
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Add("X-Ionic-Application-Id", "<Ionic App ID>");
    
                    var keyBase64 = "Bearer " + "<Security Token>";
                    client.DefaultRequestHeaders.Add("Authorization", keyBase64);
    
                    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "https://api.ionic.io/push/notifications");
                    request.Content = new StringContent(data, Encoding.UTF8, "application/json");
    
                    response = new HttpResponseMessage(HttpStatusCode.OK);
                    response = client.SendAsync(request).Result;
    
                }
    
                return response;
            }
            catch (Exception ex)
            {
    
                response = new HttpResponseMessage(HttpStatusCode.BadRequest);
                response.Content = new ObjectContent(typeof(string), ex.Message, new JsonMediaTypeFormatter(), "application/json");
    
                return response;
            }
        }
    

    【讨论】:

      【解决方案3】:

      我用过 RestClient:

      public async Task Send(string userId, string devicePushToken, string content)
      {
      
          string json = JsonConvert.SerializeObject(new
          {
              tokens = new[] {devicePushToken},
              profile = "yourprofile",
              notification = new
              {
                  message = content
              }
          });
      
          var restClient = new RestClient("https://api.ionic.io/");
          var restRequest = new RestRequest("push/notifications", Method.POST);
          restRequest.AddHeader("Accept", "application/json");
      
          restRequest.AddParameter("application/json", json, ParameterType.RequestBody);
          restRequest.AddParameter("Authorization",
              "Bearer APIKEY",
              ParameterType.HttpHeader);
      
          await restClient.ExecuteTaskAsync(restRequest);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多