原文: http://apns-c-sharp-net-vikram-jain.blogspot.com

=======================
Please, Install your certificate *.p12 on pc, and take firend name use here for refernce.
Please, set configuration file : <appSettings>
<add key="FriendName" value="Apple Production IOS Push Services: com.ABC.XYZ"/>
<add key="ProductionKeyFriendName" value="Production"/>
</appSettings>
==============
Send, push as per below in your class to call apns class:
Cls_APNS _Cls_APNS = new class_apns();
//Here pass custom fiels as per seprated by ";" ann asssigb value by "key=value;key=value"
_Cls_APNS.PushMessage("Message","DeviceToken",0,"id=12;name=ABC");
==================
Please create a class as per below (class_apns.cs). : using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Configuration;
using System.Data;
using System.Security.Authentication;
using System.IO; namespace Push.Class
{
public class class_apns
{
String CertificateName = "";
String CertificatePwd = "";
String FriendName = "Apple Development IOS Push Services: com.ABC.XYZ";
String ProductionKeyFriendName = "Production";
SslStream sslStream; public Cls_APNS()
{
FriendName = ConfigurationManager.AppSettings["FriendName"].ToString();
ProductionKeyFriendName = ConfigurationManager.AppSettings["ProductionKeyFriendName"].ToString();
} public bool ConnectToAPNS()
{
X509Certificate2Collection certs = new X509Certificate2Collection(); // Add the Apple cert to our collection
certs.Add(getServerCert()); // Apple development server address
string apsHost; if (getServerCert().ToString().Contains(ProductionKeyFriendName))
apsHost = "gateway.push.apple.com";
else
apsHost = "gateway.sandbox.push.apple.com"; // Create a TCP socket connection to the Apple server on port 2195
TcpClient tcpClient = new TcpClient(apsHost, 2195); // Create a new SSL stream over the connection
sslStream = new SslStream(tcpClient.GetStream()); // Authenticate using the Apple cert
sslStream.AuthenticateAsClient(apsHost, certs, SslProtocols.Default, false); //PushMessage(); return true;
} private X509Certificate getServerCert()
{
X509Certificate test = new X509Certificate(); //Open the cert store on local machine
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser); if (store != null)
{
// store exists, so open it and search through the certs for the Apple Cert
store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadOnly);
X509Certificate2Collection certs = store.Certificates; if (certs.Count > 0)
{
int i;
for (i = 0; i < certs.Count; i++)
{
X509Certificate2 cert = certs[i]; if (cert.FriendlyName.Contains(FriendName))
{
//Cert found, so return it.
return certs[i];
}
}
}
return test;
}
return test;
} private byte[] HexToData(string hexString)
{
if (hexString == null)
return null; if (hexString.Length % 2 == 1)
hexString = '0' + hexString; // Up to you whether to pad the first or last byte byte[] data = new byte[hexString.Length / 2]; for (int i = 0; i < data.Length; i++)
data[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16); return data;
} public bool PushMessage(string Mess, string DeviceToken, int Badge, string Custom_Field)
{
ConnectToAPNS();
List<string> Key_Value_Custom_Field = new List<string>();
String cToken = DeviceToken;
String cAlert = Mess;
int iBadge = Badge; // Ready to create the push notification
byte[] buf = new byte[256];
MemoryStream ms = new MemoryStream();
BinaryWriter bw = new BinaryWriter(ms);
bw.Write(new byte[] { 0, 0, 32 }); byte[] deviceToken = HexToData(cToken);
bw.Write(deviceToken); bw.Write((byte)0); // Create the APNS payload - new.caf is an audio file saved in the application bundle on the device
string msg = "";
msg = "{\"aps\":{\"alert\":\"" + cAlert + "\",\"badge\":\"" + iBadge.ToString() + "\",\"sound\":\"noti.aiff\"}"; String PayloadMess = "";
if (string.IsNullOrWhiteSpace(Custom_Field) == false)
{
List<string> list_Custom_Field = Custom_Field.Split(';').ToList(); if (list_Custom_Field.Count > 0)
{
for (int indx = 0; indx < list_Custom_Field.Count; indx++)
{
Key_Value_Custom_Field = list_Custom_Field[indx].Split('=').ToList();
if (Key_Value_Custom_Field.Count > 1)
{
if (PayloadMess != "") PayloadMess += ", ";
PayloadMess += "\"" + Key_Value_Custom_Field[0].ToString() + "\":\"" + Key_Value_Custom_Field[1].ToString() + "\"";
}
}
}
} if (PayloadMess != "")
{
msg += ", " + PayloadMess;
}
msg += "}"; // Write the data out to the stream
bw.Write((byte)msg.Length);
bw.Write(msg.ToCharArray());
bw.Flush(); if (sslStream != null)
{
sslStream.Write(ms.ToArray());
return true;
} return false;
} }
}

Send push notification on Apple (APNS) on c#.net的更多相关文章

  1. (转)Apple Push Notification Services in iOS 6 Tutorial: Part 1/2

    转自:http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1 Upda ...

  2. apple 官方文档 Push Notification Programming

    iOS Developer LibraryDeveloper Search Local and Push Notification Programming Guide PDF Table of Con ...

  3. 远程通知APNs(Apple Push Notification Server)

    推送通知是由应用服务提供商发起的,通过苹果的APNs(Apple Push Notification Server)发送到应用客户端.下面是苹果官方关于推送通知的过程示意图: 推送通知的过程可以分为以 ...

  4. Send Push Notifications to iOS Devices using Xcode 8 and Swift 3, APNs Auth Key

    Send Push Notifications to iOS Devices using Xcode 8 and Swift 3 OCT 6, 2016 Push notifications are ...

  5. Provider Communication with Apple Push Notification Service

    This chapter describes the interfaces that providers use for communication with Apple Push Notificat ...

  6. (转)How to build an Apple Push Notification provider server (tutorial)

    转自:https://blog.serverdensity.com/how-to-build-an-apple-push-notification-provider-server-tutorial/ ...

  7. (转)Apple Push Notification Services in iOS 6 Tutorial: Part 2/2

    转自:http://www.raywenderlich.com/32963/apple-push-notification-services-in-ios-6-tutorial-part-2 Upda ...

  8. (转)How to renew your Apple Push Notification Push SSL Certificate

    转自:https://blog.serverdensity.com/how-to-renew-your-apple-push-notification-push-ssl-certificate/ It ...

  9. (转)在SAE使用Apple Push Notification Service服务开发iOS应用, 实现消息推送

    在SAE使用Apple Push Notification Service服务开发iOS应用, 实现消息推送 From: http://saeapns.sinaapp.com/doc.html 1,在 ...

随机推荐

  1. ROC曲线与AUC值

    本文根据以下文章整理而成,链接: (1)http://blog.csdn.net/ice110956/article/details/20288239 (2)http://blog.csdn.net/ ...

  2. hibernate(七) hibernate中查询方式详解

    序言 之前对hibernate中的查询总是搞混淆,不明白里面具体有哪些东西.就是因为缺少总结.在看这篇文章之前,你应该知道的是数据库的一些查询操作,多表查询等,如果不明白,可以先去看一下 MySQL数 ...

  3. 那些年我们写过的T-SQL(下篇)

    下篇的内容很多都会在工作中用到,尤其是可编程对象,那些年我们写过的存储过程,有木有?到目前为止很多大型传统企业仍然很依赖存储过程.这部分主要难理解的部分是事务和锁机制这块,本文会进行简单的阐述.虽然很 ...

  4. Android入门(十九)WebView

    原文链接:http://www.orlion.ga/676/ WebView可以在自己的应用程序中嵌入一个浏览器来展示网页. 创建一个项目WebViewDemo,修改activity_main.xml ...

  5. javascript类型系统——字符串String类型

    × 目录 [1]定义 [2]引号 [3]反斜线[4]特点[5]转字符串 前面的话 javascript没有表示单个字符的字符型,只有字符串String类型,字符型相当于仅包含一个字符的字符串 字符串S ...

  6. Key Components and Internals of Spring Boot Framework--转

    原文地址:http://www.journaldev.com/7989/key-components-and-internals-of-spring-boot-framework In my prev ...

  7. MD5加密算法实现用户信息加密

    MD5加密算法类: public class MD5 { /** * MD5 加密 * @param str * @author Red * @return */ public final Strin ...

  8. PHP面向对象中的重要知识点(一)

    1. __construct: 内置构造函数,在对象被创建时自动调用.见如下代码: <?php class ConstructTest { private $arg1; private $arg ...

  9. nodejs学习笔记三——nodejs使用富文本插件ueditor

    在做自己的nodejs项目的时候遇到需要使用ueditor.原来下载的是ueditor的jsp版本.目录如下  在ueditor.config.js中有配置服务器home路径(这个home路径能找到u ...

  10. IIS各个版本中你需要知道的那些事儿

    一.写在前面 目前市面上所用的IIS版本估计都是>=6.0的.所以我们主要以下面三个版本进行讲解 服务器版本 IIS默认版本 server2003 6.0 server2008 7.0 serv ...