c# - Example code to send push notification from remote server to universal windows app? -
i'm trying send push notification small code write in c#, here code:
public void launch() { string channeluri = "channel uri"; string sid = "sid package"; string secret = "client secret"; // toast notification var toast = @"<toast><visual><binding template=""toasttext01""><text id=""1"">hello!</text></binding></visual></toast>"; console.writeline(this.posttowns(secret, sid, channeluri, toast)); console.readline(); } // post wns public string posttowns(string secret, string sid, string uri, string xml, string type = "wns/toast") { try { // should cache access token var accesstoken = getaccesstoken(secret, sid); byte[] contentinbytes = encoding.utf8.getbytes(xml); httpwebrequest request = httpwebrequest.create(uri) httpwebrequest; request.method = "post"; request.headers.add("x-wns-type", type); request.headers.add("authorization", string.format("bearer {0}", accesstoken.accesstoken)); using (stream requeststream = request.getrequeststream()) requeststream.write(contentinbytes, 0, contentinbytes.length); using (httpwebresponse webresponse = (httpwebresponse)request.getresponse()) return webresponse.statuscode.tostring(); } catch (webexception webexception) { string exceptiondetails = webexception.response.headers["www-authenticate"] ?? string.empty; if (exceptiondetails.contains("token expired")) { getaccesstoken(secret, sid); // implement maximum retry policy return posttowns(uri, xml, secret, sid, type); } else { // log response return "exception: " + webexception.message; } } catch (exception ex) { return "exception: " + ex.message; } } // authorization [datacontract] public class oauthtoken { [datamember(name = "access_token")] public string accesstoken { get; set; } [datamember(name = "token_type")] public string tokentype { get; set; } } private oauthtoken getoauthtokenfromjson(string jsonstring) { using (var ms = new memorystream(encoding.unicode.getbytes(jsonstring))) { var ser = new datacontractjsonserializer(typeof(oauthtoken)); var oauthtoken = (oauthtoken)ser.readobject(ms); return oauthtoken; } } protected oauthtoken getaccesstoken(string secret, string sid) { var urlencodedsecret = httputility.urlencode(secret); var urlencodedsid = httputility.urlencode(sid); var body = string.format("grant_type=client_credentials&client_id={0}&client_secret={1}&scope=notify.windows.com", urlencodedsid, urlencodedsecret); string response; using (var client = new webclient()) { client.headers.add("content-type", "application/x-www-form-urlencoded"); response = client.uploadstring("https://login.live.com/accesstoken.srf", body); } return getoauthtokenfromjson(response); } where channeluri, sid , secret fixed variables obtained earlier. problem remote server return 400 error (bad request) dont't know why. channel creation successful, followed guide: https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh868221.aspx , sid , secret variables refer app created in microsoft developer dashboard. can me?
http 400 error indicates bad request, means request not understood server due malformed syntax. in other word, request sent client doesn't follow server's rules.
there 4 required headers must included in push notifications: x-wns-type, content-type, content-length, , authorization.
while using httpwebresponse, should able set content-length automatically, still need set content-type manually. , toast, tile, , badge notifications, header must set "text/xml". more info, please see send notification request.
httpwebrequest request = httpwebrequest.create(uri) httpwebrequest; request.method = "post"; request.headers.add("x-wns-type", type); request.contenttype = "text/xml"; request.headers.add("authorization", string.format("bearer {0}", accesstoken.accesstoken));
Comments
Post a Comment