I'm trying to send a push notification using the ionic push notifications api, using c# with WebApi. The python example from ionic website is working perfectly, but I can't get it working in c#, although it seems that the requests are the same. This is my code:
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;
}
}
I keep getting bad request (400), with no further explanation.
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);
}
Here is how you do it:
[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;
}
}
I've used 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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.