欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#推送信息到APNs的方法

 更新時(shí)間:2015年05月06日 11:11:56   作者:chenzym  
這篇文章主要介紹了C#推送信息到APNs的方法,涉及C#推送通知到蘋(píng)果APNs的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了C#推送信息到APNs的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

class Program
{
  public static DateTime? Expiration { get; set; }
  public static readonly DateTime DoNotStore = DateTime.MinValue;
  private static readonly DateTime UNIX_EPOCH = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  private static string DeviceToken = "273eeddaef02192cf4ba5b666453b258f2d2a1ad02f549105fd03fea789d809d";
  public const int DEVICE_TOKEN_BINARY_SIZE = 32;
  public const int DEVICE_TOKEN_STRING_SIZE = 64;
  public const int MAX_PAYLOAD_SIZE = 256;
  private static X509Certificate certificate;
  private static X509CertificateCollection certificates;
  static void Main(string[] args)
  {
   string hostIP = "gateway.sandbox.push.apple.com";//
   int port = 2195;
   string password = "ankejiaoyu";//
   string certificatepath = "aps_developer_identity.p12";//bin/debug
   string p12Filename = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, certificatepath);
   certificate = new X509Certificate2(System.IO.File.ReadAllBytes(p12Filename), password, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
   certificates = new X509CertificateCollection();
   certificates.Add(certificate);
   TcpClient apnsClient = new TcpClient();
   apnsClient.Connect(hostIP, port);
   SslStream apnsStream = new SslStream(apnsClient.GetStream(), false, new RemoteCertificateValidationCallback(validateServerCertificate), new LocalCertificateSelectionCallback(selectLocalCertificate));
   try
   {
    //APNs已不支持SSL 3.0 
    apnsStream.AuthenticateAsClient(hostIP, certificates, System.Security.Authentication.SslProtocols.Tls, false);
   }
   catch (System.Security.Authentication.AuthenticationException ex)
   {
    Console.WriteLine("error+"+ex.Message);
   }
   if (!apnsStream.IsMutuallyAuthenticated)
   {
    Console.WriteLine("error:Ssl Stream Failed to Authenticate!");
   }
   if (!apnsStream.CanWrite)
   {
    Console.WriteLine("error:Ssl Stream is not Writable!");
   }
   Byte[] message = ToBytes();
   apnsStream.Write(message);
  }
  public static byte[] ToBytes()
  {
   // Without reading the response which would make any identifier useful, it seems silly to
   // expose the value in the object model, although that would be easy enough to do. For
   // now we'll just use zero.
   int identifier = 0;
   byte[] identifierBytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(identifier));
   // APNS will not store-and-forward a notification with no expiry, so set it one year in the future
   // if the client does not provide it.
   int expiryTimeStamp = -1;//過(guò)期時(shí)間戳
   if (Expiration != DoNotStore)
   {
    //DateTime concreteExpireDateUtc = (Expiration ?? DateTime.UtcNow.AddMonths(1)).ToUniversalTime();
    DateTime concreteExpireDateUtc = (Expiration ?? DateTime.UtcNow.AddSeconds(20)).ToUniversalTime();
    TimeSpan epochTimeSpan = concreteExpireDateUtc - UNIX_EPOCH;
    expiryTimeStamp = (int)epochTimeSpan.TotalSeconds;
   }
   byte[] expiry = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(expiryTimeStamp));
   byte[] deviceToken = new byte[DeviceToken.Length / 2];
   for (int i = 0; i < deviceToken.Length; i++)
    deviceToken[i] = byte.Parse(DeviceToken.Substring(i * 2, 2), System.Globalization.NumberStyles.HexNumber);
   if (deviceToken.Length != DEVICE_TOKEN_BINARY_SIZE)
   {
    Console.WriteLine("Device token length error!");
   }
   byte[] deviceTokenSize = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(Convert.ToInt16(deviceToken.Length)));
   string str = "{\"aps\":{\"alert\":\"這是測(cè)試消息?。",\"badge\":1,\"sound\":\"anke.mp3\"}}";
   byte[] payload = Encoding.UTF8.GetBytes(str);
   byte[] payloadSize = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(Convert.ToInt16(payload.Length)));
   List<byte[]> notificationParts = new List<byte[]>();
   //1 Command
   notificationParts.Add(new byte[] { 0x01 }); // Enhanced notification format command
   notificationParts.Add(identifierBytes);
   notificationParts.Add(expiry);
   notificationParts.Add(deviceTokenSize);
   notificationParts.Add(deviceToken);
   notificationParts.Add(payloadSize);
   notificationParts.Add(payload);
   return BuildBufferFrom(notificationParts);
  }
  private static byte[] BuildBufferFrom(IList<byte[]> bufferParts)
  {
   int bufferSize = 0;
   for (int i = 0; i < bufferParts.Count; i++)
    bufferSize += bufferParts[i].Length;
   byte[] buffer = new byte[bufferSize];
   int position = 0;
   for (int i = 0; i < bufferParts.Count; i++)
   {
    byte[] part = bufferParts[i];
    Buffer.BlockCopy(bufferParts[i], 0, buffer, position, part.Length);
    position += part.Length;
   }
   return buffer;
  }
  private static bool validateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
  {
   return true; // Dont care about server's cert
  }
  private static X509Certificate selectLocalCertificate(object sender, string targetHost, X509CertificateCollection localCertificates,
   X509Certificate remoteCertificate, string[] acceptableIssuers)
  {
   return certificate;
  }
}

希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • C#用戶(hù)定義類(lèi)型轉(zhuǎn)換詳解

    C#用戶(hù)定義類(lèi)型轉(zhuǎn)換詳解

    用于自定義類(lèi)和結(jié)構(gòu)能夠進(jìn)行隱式轉(zhuǎn)換和顯示轉(zhuǎn)換.例如:將一個(gè)自定義類(lèi)類(lèi)型轉(zhuǎn)換成整型,浮點(diǎn)型等,反之亦然
    2014-01-01
  • c#多線(xiàn)程之間的排他鎖的實(shí)現(xiàn)

    c#多線(xiàn)程之間的排他鎖的實(shí)現(xiàn)

    我們很多時(shí)候會(huì)碰到這樣的問(wèn)題,使用多線(xiàn)程刷一個(gè)表的數(shù)據(jù)時(shí)需要多個(gè)線(xiàn)程不能重復(fù)提取數(shù)據(jù),那么這個(gè)時(shí)候就需要使用到線(xiàn)程的排他鎖了,本文就詳細(xì)的介紹一下
    2021-08-08
  • C#實(shí)現(xiàn)的UDP收發(fā)請(qǐng)求工具類(lèi)實(shí)例

    C#實(shí)現(xiàn)的UDP收發(fā)請(qǐng)求工具類(lèi)實(shí)例

    這篇文章主要介紹了C#實(shí)現(xiàn)的UDP收發(fā)請(qǐng)求工具類(lèi),結(jié)合具體實(shí)例形式分析了C#針對(duì)UDP請(qǐng)求的監(jiān)聽(tīng)、接收、發(fā)送等相關(guān)操作技巧,需要的朋友可以參考下
    2017-06-06
  • 在.NET中讀取嵌入和使用資源文件的方法

    在.NET中讀取嵌入和使用資源文件的方法

    本文分別介紹了使用GetManifestResourceStream讀取嵌入資源,和使用. resx資源文件嵌入資源,希望對(duì)大家有所幫助。
    2016-05-05
  • 淺析C# 基礎(chǔ)語(yǔ)法的使用

    淺析C# 基礎(chǔ)語(yǔ)法的使用

    本篇文章對(duì)C#中基礎(chǔ)語(yǔ)法的使用。進(jìn)行了詳細(xì)的分析介紹。需要的朋友參考下
    2013-05-05
  • 基于C#實(shí)現(xiàn)簡(jiǎn)單的隨機(jī)抽獎(jiǎng)小程序

    基于C#實(shí)現(xiàn)簡(jiǎn)單的隨機(jī)抽獎(jiǎng)小程序

    臨近春節(jié),大街小巷的地方都有抽獎(jiǎng)活動(dòng),那么基于C#是如何實(shí)現(xiàn)簡(jiǎn)單的抽獎(jiǎng)程序的呢,下面小編給大家分享了具體代碼,有需要的朋友參考下
    2016-01-01
  • C#使用Winform編寫(xiě)一個(gè)圖片預(yù)覽器管理

    C#使用Winform編寫(xiě)一個(gè)圖片預(yù)覽器管理

    這篇文章主要為大家詳細(xì)介紹了C#如何使用Winform編寫(xiě)一個(gè)通用圖片預(yù)覽器管理,包含滾輪放大縮小,剪切,下一頁(yè),方向變化等,需要的可以參考下
    2024-02-02
  • C#實(shí)現(xiàn)windows系統(tǒng)重啟和關(guān)機(jī)的代碼詳解

    C#實(shí)現(xiàn)windows系統(tǒng)重啟和關(guān)機(jī)的代碼詳解

    這篇文章主要介紹了C#實(shí)現(xiàn)windows系統(tǒng)重啟和關(guān)機(jī)的的方法,涉及C#調(diào)用windows系統(tǒng)命令實(shí)現(xiàn)控制開(kāi)機(jī)、關(guān)機(jī)等操作的技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下
    2024-02-02
  • C#網(wǎng)絡(luò)編程之Socket編程

    C#網(wǎng)絡(luò)編程之Socket編程

    本文詳細(xì)講解了C#網(wǎng)絡(luò)編程的Socket編程,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-02-02
  • C#/VB.NET中從?PDF?文檔中提取所有表格

    C#/VB.NET中從?PDF?文檔中提取所有表格

    這篇文章主要介紹了C#/VB.NET中從PDF文檔中提取所有表格,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-08-08

最新評(píng)論