Why I can't send a push notification using the Appcelerator REST API in C#? -
i'm trying send notify alert devices subscribed channel through restsharp api c#. how code looks like:
public void sendpush() { try { var client = new restclient(" https://api.cloud.appcelerator.com"); var request = new restrequest("/v1/push_notification/notify.json?key=appkey", method.post) { requestformat = dataformat.json, }; request.addbody(new { channel = "alert", payload = new { title = "notificación", badge = 1, alert = "alerta: proximo arribo de sismo la ciudad de mexico", sound = "default" } }); var response = client.execute(request); var content = response.content; debug.writeline(content); } catch (exception ex) { debug.writeline("message " + ex.message + " \n inner exception " + ex.innerexception + " \n stack trace" + ex.stacktrace); } }
the response following:
{ "meta": { "status": "fail", "code": 401, "cc_code": 1000, "message": "you need sign in or sign before continuing." } }
why asking me login? i'm trying send notify message devices.
any appreciated.
edit
i able login, store connection information cookiecontainer , send notification request, can't send payload parameter object.
this how new login function looks like:
public void login() { client = new restclient("https://api.cloud.appcelerator.com"); client.cookiecontainer = new system.net.cookiecontainer(); request = new restrequest("/v1/users/login.json?key={appkey}", method.post) { requestformat = dataformat.json }; request.addurlsegment("appkey", "key"); request.addbody(new { login = "user", password = "pass" }); var response = client.execute(request); var content = response.content; debug.writeline(content); sendpush(); }
this how sendpush function looks now:
public void sendpush() { try { client.baseurl = "https://api.cloud.appcelerator.com"; request.resource = "/v1/push_notification/notify.json?key={appkey}"; request.method = method.post; request.addurlsegment("appkey", "key"); request.addparameter("channel", "alert"); request.addparameter("payload", new { title = "notification", badge = 1, alert = "warning", sound = "default" }); var response = client.execute(request); var content = response.content; debug.writeline(content); } catch (exception ex) { debug.writeline("message " + ex.message + " \n inner exception " + ex.innerexception + " \n stack trace" + ex.stacktrace); } }
i'm trying send object parameter doesn't seem valid, don't know why. if try send this:
request.addparameter("payload", "warning");
i response appcelerator api, not behavior want in mobile app, since payload missing several properties.
how should send object parameter restsharp? or restsharp doesn't allow that? options?
i found way this. first have login , save session in cookiecontainer this:
public void login() { client = new restclient("https://api.cloud.appcelerator.com"); client.cookiecontainer = new system.net.cookiecontainer(); request = new restrequest("/v1/users/login.json?key={appkey}", method.post) { requestformat = dataformat.json, }; request.addurlsegment("appkey", "key"); request.addbody(new { login = "user", password = "pass" }); var response = client.execute(request); sendpush(); }
then make call sendpush method looks now:
public void sendpush() { try { client.baseurl = "https://api.cloud.appcelerator.com"; request.resource = "/v1/push_notification/notify.json?key={appkey}"; request.method = method.post; request.addurlsegment("appkey", "key"); request.addparameter("channel", "alert"); request.addparameter("payload", "{ \"title\" : \"notificación\", \"badge\" : 1, \"alert\" : \"alerta: sismo detectado en: " + direccion + " proximo arribo de sismo la ciudad de mexico\", \"sound\" : \"default\"}"); var response = client.execute(request); } catch (exception ex) { debug.writeline("message " + ex.message + " \n inner exception " + ex.innerexception + " \n stack trace" + ex.stacktrace); } }
i having trouble sending payload object in addbody method, decided send string addparameter , worked no problem, can done using javascriptserializer or jsonconverter json.net. hope helps other people.
Comments
Post a Comment