jquery - Sending push notification through Urban Airship via JavaScript -
i've set simple web site consisting of button javascript onclick method want send push notification phone using urban airship.
if use quotes around data property, "500 (internal server error)". if don't use quotes around data property, popup authorization window urban airship in write app key , master secret. seems accept this, afterwards "405 (method not allowed)".
according chrome dev tools, both ways handled get, though it's specified post (which required). wrong?
function sendbuttonclick(){ jquery.ajax({ type: 'post', contenttype: 'application/json; charset=utf-8', username:'my app key', password:'my app secret', url: 'https://go.urbanairship.com/api/push/broadcast/', data: {"android": {"alert": "alerttest", "extra": {"extra test": "extra value test"}}}, datatype: 'jsonp', }); };
thanks in advance!
what server side technology using? have there. basically, cannot cross domain calls via desktop browser. way can is, call server side method payload , have server side send out notification. here sample code c# wrote.
public interface inotification { void set(string deviceid, string alert, int? badge, string sound); } public class basenotification { public list<string> aliases { get; set; } public list<string> tags { get; set; } } public class iosnotification : basenotification, inotification { public list<string> device_tokens { get; set; } public notificationbody aps { get; set; } public iosnotification() { device_tokens = new list<string>(); } public void set(string deviceid, string alert, int? badge, string sound) { device_tokens.add(deviceid); aps = new notificationbody { alert = alert, badge = badge.hasvalue ? badge.value : 0, sound = sound }; } } //in static extensions public static string tojsonstring<t>(this ienumerable<t> items) { var jsonstring = jsonconvert.serializeobject(items, formatting.indented, new jsonserializersettings { contractresolver = new lowercasecontractresolver(), nullvaluehandling = nullvaluehandling.ignore }); jsonstring = jsonstring.replace("\r\n", string.empty); return jsonstring; } protected internal void sendpushnotification(list<inotification> payload, string urikey) { var json = payload.tojsonstring(); var uri = getappsettings(urikey); var encoding = new utf8encoding(); var contentlength = encoding.getbytecount(json); var request = (webrequest)webrequest.create(uri); request.method = "post"; credentialcache credentialcache = new credentialcache(); credentialcache.add(new uri(uri), "basic", getcredentials()); request.credentials = credentialcache; request.headers.add("authorization", "basic " + convert.tobase64string(new asciiencoding().getbytes(_username + ":" + _password))); request.contenttype = "application/json"; request.contentlength = contentlength; using (var stream = request.getrequeststream()) { stream.write(encoding.getbytes(json), 0, contentlength); stream.close(); var response = request.getresponse(); response.close(); } }
Comments
Post a Comment