0

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 に保存し、通知リクエストを送信することはできましたが、ペイロード パラメータをオブジェクトとして送信することはできません。

これは、私の新しいログイン関数がどのように見えるかです:

    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 に答える