我正在尝试通过 RestSharp API for C# 向订阅我频道的所有设备发送通知警报。这就是我的代码的样子:
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);
}
}
我得到的响应如下:
{
"meta": {
"status": "fail",
"code": 401,
"cc_code": 1000,
"message": "You need to sign in or sign up before continuing."
}
}
为什么要我登录?我要做的就是向设备发送通知消息。
任何帮助将不胜感激。
编辑
我能够登录,将连接信息存储到 CookieContainer 中并发送通知请求,但我无法将有效负载参数作为对象发送。
这就是我的新登录功能的样子:
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();
}
这就是我的 SendPush 函数现在的样子:
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);
}
}
我试图发送一个对象作为参数,但它似乎无效,我不知道为什么。如果我只是尝试发送这样的东西:
request.AddParameter("payload", "Warning");
我从 Appcelerator API 得到响应,但不是我想要在移动应用程序中的行为,因为有效负载缺少几个属性。
我应该如何使用 RestSharp 将该对象作为参数发送?还是 RestSharp 不允许这样做?我有哪些选择?