Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
300 views
Welcome To Ask or Share your Answers For Others

1 Answer

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);           

    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...