iPhone推送通知urbanairship [英] iphone push notification urbanairship

查看:118
本文介绍了iPhone推送通知urbanairship的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我米要通过urbanairship API从我的服务器端(C#)发送通知

i"m want to send notification from my server side (c#) via urbanairship api

有没有在C#中的例子怎么办呢?

is there any example in c# how to do it?

感谢

推荐答案

基本上...

using System;
using System.IO;
using System.Net;
using System.Text;

namespace Examples.System.Net
{
    public class WebRequestPostExample
    {
        public static void Main ()
        {
            // Create a request using a URL that can receive a post. 
            WebRequest request = WebRequest.Create ("https://go.urbanairship.com/api/push/");
            // Set the Method property of the request to POST.
            request.Method = "POST";

            // Create POST data and convert it to a byte array. 

爆发了多行,所以你可以阅读

broken out to multiple lines so you can read it

            string postData = "{
    "device_tokens": [
        "some device token",
        "another device token"
    ],
    "aliases": [
        "user1",
        "user2"
    ],
    "tags": [
        "tag1",
        "tag2"
    ],
    "schedule_for": [
        "2010-07-27 22:48:00",
        "2010-07-28 22:48:00"
    ],
    "exclude_tokens": [
        "device token you want to skip",
        "another device token you want to skip"
    ],
    "aps": {
         "badge": 10,
         "alert": "Hello from Urban Airship!",
         "sound": "cat.caf"
    }
}";

然后

            byte[] byteArray = Encoding.UTF8.GetBytes (postData);
            // Set the ContentType property of the WebRequest.
            request.ContentType = "application/json";
            // Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length;

            //Do a http basic authentication somehow
            string username = "<application key from urban airship>";
            string password = "<master secret from urban airship>"; 
            string usernamePassword = username + ":" + password;
            CredentialCache mycache = new CredentialCache();
            mycache.Add( new Uri( "https://go.urbanairship.com/api/push/" ), "Basic", new NetworkCredential( username, password ) );
            request.Credentials = mycache;
            request.Headers.Add( "Authorization", "Basic " + Convert.ToBase64String( new ASCIIEncoding().GetBytes( usernamePassword ) ) );

            // Get the request stream.
            Stream dataStream = request.GetRequestStream ();
            // Write the data to the request stream.
            dataStream.Write (byteArray, 0, byteArray.Length);
            // Close the Stream object.
            dataStream.Close ();
            // Get the response.
            WebResponse response = request.GetResponse ();
            // Display the status.
            Console.WriteLine (((HttpWebResponse)response).StatusDescription);
            // Get the stream containing content returned by the server.
            dataStream = response.GetResponseStream ();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader (dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd ();
            // Display the content.
            Console.WriteLine (responseFromServer);
            // Clean up the streams.
            reader.Close ();
            dataStream.Close ();
            response.Close ();
        }
    }
}

请参阅 API文档,的 MSDN 和的这里了解更多有关HTTPS

这篇关于iPhone推送通知urbanairship的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆