为什么我能';t使用C#中的Appcelerator REST API发送推送通知?

为什么我能';t使用C#中的Appcelerator REST API发送推送通知?,c#,push-notification,appcelerator,restsharp,C#,Push Notification,Appcelerator,Restsharp,我正试图通过C#的RestSharp API向订阅我频道的所有设备发送通知警报。我的代码是这样的: public void SendPush() { try { var client = new RestClient(" https://api.cloud.appcelerator.com"); var request = new RestRequest("/v1/push_notification

我正试图通过C#的RestSharp API向订阅我频道的所有设备发送通知警报。我的代码是这样的:

    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);
        }
    }
    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();
    }
我得到的答复如下:

{
  "meta": {
    "status": "fail",
    "code": 401,
   "cc_code": 1000,
   "message": "You need to sign in or sign up before continuing."
  }
}
为什么要我登录?我要做的就是向设备发送通知消息

任何帮助都将不胜感激

编辑

我能够登录,将连接信息存储到CookieContainer中并发送通知请求,但我不能将负载参数作为对象发送

这是我的新登录功能的外观:

    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);
        }
    }
    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");
    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();
    }
我从Appcelerator API获得响应,但不是我想要在移动应用程序中实现的行为,因为负载缺少几个属性


我应该如何使用RestSharp将该对象作为参数发送?还是RestSharp不允许?我的选项是什么?

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

var myAppKey = "WhateverYourAppKeyIs";
var request = new RestRequest("/v1/push_notification/notify.json?key=" + myAppKey , Method.POST)

我找到了这样做的方法。首先,我必须登录并将会话保存在CookieContainer中,如下所示:

            request.AddParameter("payload", "Warning");
    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将其作为字符串发送,并且它工作正常,这也可以使用或JsonConverter来完成。希望它能帮助其他人。

我正在这样做,我尝试了生产和开发密钥,但都没有成功。如果是这样,您可能需要联系appcelerator支持部门。据我所知,问题似乎不在你的代码中。检查我编辑的问题,似乎我最终不得不登录并将信息存储到CookieContainer中,但我无法将对象作为参数发送,有什么想法吗?我想你想为有效负载调用AddBody而不是AddParameter,但我对RestSharp不太熟悉。我一直在使用HttpWebRequest或.NET4.5HttpClienti,我正在尝试相同的代码,但它不起作用。即使在提供了正确的用户名和密码之后,也会出现一个错误,即其中任何一个都不正确。我可以登录到仪表板并从那里访问应用程序。知道我为什么会出现这个错误吗?我已经有一段时间没有使用Appcelerator了,但我想当你说你可以登录到仪表板时,你指的是Appcelerator用户的凭据,而不是你用户集合中的用户。不要使用Appcelerator凭据登录,您需要使用您在用户集合中创建的用户,这些是您要登录到应用程序的用户。我稍后再查,但这应该是你的问题。但是在我登录到仪表板之后,我可以进入箭头面板,然后我可以向注册的设备发送推送消息。我在客户端使用token方法。哦,似乎我误读了这句话,我混淆了用户登录和使用Appcelerator服务的登录,很抱歉,我已经有一段时间没有使用Appcelerator了。你能给我提供登录请求的响应吗?下面是我得到的响应。返回的请求带有HTTP状态代码401无效的电子邮件/用户名或密码。我已经三次检查了用户名和密码。我可以使用它登录dashboard.appcelerator.com。