0

我正在尝试通过 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 不允许这样做?我有哪些选择?

4

2 回答 2

2

我找到了做到这一点的方法。首先,我必须像这样登录并将会话保存在 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);
        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", "{ \"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);
        }
    }

我在 AddBody 方法中发送有效负载对象时遇到问题,因此我决定使用 AddParameter 将其作为字符串发送,并且它没有问题,这也可以使用来自JSON.NET的JavaScriptSerializer或 JsonConverter 来完成。希望它可以帮助其他人。

于 2013-04-24T17:00:41.447 回答
0

您需要提供有效的应用密钥

var myAppKey = "WhateverYourAppKeyIs";
var request = new RestRequest("/v1/push_notification/notify.json?key=" + myAppKey , Method.POST)
于 2013-04-18T17:33:30.503 回答