我尝试使用离线推送通知api发送推送通知,使用带WebApi的c#。 来自离子网站的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),没有进一步的解释。
答案 0 :(得分:2)
好的,解决了! 问题在于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);
}
答案 1 :(得分:0)
以下是您的操作方法:
[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;
}
}
答案 2 :(得分:0)
我使用过RestClient:
__file__