I'm trying to send a notify alert to all the devices subscribed to my channel through the RestSharp API for C#. This is how my 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 a 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 I get is the following:
{
"meta": {
"status": "fail",
"code": 401,
"cc_code": 1000,
"message": "You need to sign in or sign up before continuing."
}
}
Why is it asking me to login? All what I'm trying to do is to send a notify message to the devices.
Any help will be appreciated.
EDIT
I was able to login, store the connection information into a CookieContainer and send the notification request, but I can't send the payload parameter as an object.
This is how my 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 is how my SendPush function looks like 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 to send an object as a parameter but it doesn't seem to be valid, I don't know why. If I just try to send something like this:
request.AddParameter("payload", "Warning");
I get a response from the Appcelerator API, but not the behavior I want to in the mobile app, since the payload is missing several properties.
How should I send that object as a parameter with RestSharp? Or RestSharp doesn't allow that? What are my options?
I found the way to do this. First I have to login and save the session in a CookieContainer like 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 I make a call to my SendPush method which looks like this 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 a 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 was having trouble sending the payload object in the AddBody method, so I decided to send it as a string with AddParameter and it worked with no problem, this can be done too using the JavaScriptSerializer or JsonConverter from JSON.NET. Hope it helps other people.