OK, solved it!
The problem was with the Content-Type header, the "PostAsJsonAsync" method default content-type is "application/json; charset=utf-8", while the api is expecting "application/json".
This works:
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;
}
For clarity, the calling function is :
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);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…