Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

asp.net web api - Ionic push notification api in c# with WebApi

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

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

1 Reply

0 votes
by (71.8m points)

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

    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...