为什么我不能使用c#中的Appcelerator REST API发送推送通知

本文关键字:API REST Appcelerator 通知 中的 不能 为什么 | 更新日期: 2023-09-27 18:09:59

我正试图通过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);
        }
    }

我得到的响应如下:

{
  "meta": {
    "status": "fail",
    "code": 401,
   "cc_code": 1000,
   "message": "You need to sign in or sign up before continuing."
  }
}

为什么要我登录?我所要做的就是给设备发送一个通知信息。

任何帮助都将不胜感激。

编辑

我可以登录,将连接信息存储到CookieContainer中并发送通知请求,但我不能将有效载荷参数作为对象发送。

我的新Login函数是这样的:

    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不允许?我有什么选择?

为什么我不能使用c#中的Appcelerator REST API发送推送通知

我找到了这样做的方法。首先,我必须登录并将会话保存在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的字符串发送,并且它没有问题,这也可以使用JavaScriptSerializer或JsonConverter从JSON.NET完成。

你需要提供一个有效的应用程序密钥

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