最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#推送信息到APNs的方法

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

本文實例講述了C#推送信息到APNs的方法。分享給大家供大家參考。具體實現(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;//過期時間戳
   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\":\"這是測試消息?。",\"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;
  }
}

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

相關(guān)文章

  • Unity編輯器資源導(dǎo)入處理函數(shù)OnPostprocessAudio使用案例

    Unity編輯器資源導(dǎo)入處理函數(shù)OnPostprocessAudio使用案例

    這篇文章主要為大家介紹了Unity編輯器資源導(dǎo)入處理函數(shù)OnPostprocessAudio使用案例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-08-08
  • C#中類與結(jié)構(gòu)的區(qū)別實例分析

    C#中類與結(jié)構(gòu)的區(qū)別實例分析

    這篇文章主要介紹了C#中類與結(jié)構(gòu)的區(qū)別,類與結(jié)構(gòu)是C#初學(xué)者比較輕易混淆的概念,本文加以實例說明,需要的朋友可以參考下
    2014-08-08
  • WPF利用DrawingContext實現(xiàn)繪制溫度計

    WPF利用DrawingContext實現(xiàn)繪制溫度計

    這篇文章主要為大家詳細介紹了如何利用WPF和DrawingContext實現(xiàn)繪制溫度計,文中的示例代碼講解詳細,對我們學(xué)習(xí)或工作有一定幫助,感興趣的小伙伴可以了解一下
    2022-09-09
  • 基于JWT.NET的使用(詳解)

    基于JWT.NET的使用(詳解)

    下面小編就為大家分享一篇基于JWT.NET的使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • C#實現(xiàn)批量Word轉(zhuǎn)換Html的示例代碼

    C#實現(xiàn)批量Word轉(zhuǎn)換Html的示例代碼

    這篇文章主要為大家詳細介紹了如何利用C#批量Word轉(zhuǎn)換Html的功能,文中的示例代碼講解詳細,對我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下
    2022-12-12
  • C# 使用Word模板導(dǎo)出數(shù)據(jù)的實現(xiàn)代碼

    C# 使用Word模板導(dǎo)出數(shù)據(jù)的實現(xiàn)代碼

    最近接到個需求,使用word模板導(dǎo)出數(shù)據(jù),怎么實現(xiàn)這個需求呢,今天小編通過實例代碼給大家介紹C# 使用Word模板導(dǎo)出數(shù)據(jù)的方法,感興趣的朋友一起看看吧
    2021-06-06
  • C#實現(xiàn)網(wǎng)頁畫圖功能

    C#實現(xiàn)網(wǎng)頁畫圖功能

    這篇文章主要為大家詳細介紹了C#實現(xiàn)網(wǎng)頁畫圖功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • C#檢測上傳文件真正類型的方法

    C#檢測上傳文件真正類型的方法

    這篇文章主要介紹了C#檢測上傳文件真正類型的方法,可有效的防止用戶通過修改后綴名來改變文件類型的功能,需要的朋友可以參考下
    2015-04-04
  • C#中委托和事件的區(qū)別詳解

    C#中委托和事件的區(qū)別詳解

    C# 中的委托(Delegate)類似于 C 或 C++ 中函數(shù)的指針。事件是在委托類型變量前加上 event 關(guān)鍵字,其本質(zhì)是用來對委托類型的變量進行封裝,類似于類的屬性對字段的封裝。本文就來聊聊C#中委托和事件的區(qū)別,感興趣的可以了解一下
    2022-11-11
  • 在C#中調(diào)用VBScript、javascript等腳本的實現(xiàn)代碼

    在C#中調(diào)用VBScript、javascript等腳本的實現(xiàn)代碼

    在C#中調(diào)用VBScript、javascript等腳本的實現(xiàn)步驟,需要的朋友可以參考下。
    2009-11-11

最新評論

改则县| 定结县| 湘潭县| 朔州市| 新河县| 新营市| 郁南县| 梁河县| 屏东市| 喜德县| 达州市| 河间市| 石家庄市| 昌宁县| 迁西县| 桃园市| 蒙城县| 汨罗市| 神农架林区| 左贡县| 临海市| 朝阳县| 息烽县| 绩溪县| 无棣县| 武义县| 鹤山市| 会同县| 探索| 威信县| 沾化县| 都兰县| 济源市| 赞皇县| 越西县| 汉沽区| 曲周县| 宣化县| 封开县| 亚东县| 盐池县|