Send push notification on Apple (APNS) on c#.net
原文: 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的更多相关文章
- (转)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 ...
- apple 官方文档 Push Notification Programming
iOS Developer LibraryDeveloper Search Local and Push Notification Programming Guide PDF Table of Con ...
- 远程通知APNs(Apple Push Notification Server)
推送通知是由应用服务提供商发起的,通过苹果的APNs(Apple Push Notification Server)发送到应用客户端.下面是苹果官方关于推送通知的过程示意图: 推送通知的过程可以分为以 ...
- 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 ...
- Provider Communication with Apple Push Notification Service
This chapter describes the interfaces that providers use for communication with Apple Push Notificat ...
- (转)How to build an Apple Push Notification provider server (tutorial)
转自:https://blog.serverdensity.com/how-to-build-an-apple-push-notification-provider-server-tutorial/ ...
- (转)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 ...
- (转)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 ...
- (转)在SAE使用Apple Push Notification Service服务开发iOS应用, 实现消息推送
在SAE使用Apple Push Notification Service服务开发iOS应用, 实现消息推送 From: http://saeapns.sinaapp.com/doc.html 1,在 ...
随机推荐
- 面试小记---外部脚本必须包含 <script> 标签吗?
外部脚本必须包含 <script> 标签吗? 答案是否定的. 身为小白的我一开始也是以为这句话的对了,因为本来嘛,引用外部脚本不都用的是<script>标签中的src属性吗.所 ...
- AMD 和 CMD 的区别有哪些?
看到玉伯在介绍seajs和requirejs时,说“RequireJS 遵循的是 AMD(异步模块定义)规范,SeaJS 遵循的是 CMD (通用模块定义)规范”. 能否详细(举例)说明下这个2个规范 ...
- KlayGE 4.4中渲染的改进(五):OpenGL 4.4和OpenGLES 3
转载请注明出处为KlayGE游戏引擎,本文的永久链接为http://www.klayge.org/?p=2796 上一篇我们提到了SSSSS,作为本系列的最后一篇,本文将介绍KlayGE 4.4的Op ...
- 自动生成Mapper和Entity工具MybatisGenerator的使用
新建一个XML文件crmGeneratorConfig.xml,文件具体内容如下.把MybatisGenerator.zip解压出来,把MybatisGenerator文件夹复制到Eclipse安装目 ...
- 在Abp中执行sql语句
目录 前言 最近使用ABP(ASP.NET Boilerplate)做项目,有些查询(或存储过程)直接使用sql语句会更方便一点. 定义一个接口 在Core项目中定义一个接口,如:ISqlExecut ...
- JavaWeb:实现文件上传
JavaWeb:实现文件上传 理解文件上传: 1.上传文件就是把客户端的文件发送给服务器端. 2.HTTP响应的正文部分最常见的是HTML文档,但是也可以是其他任意格式的数据,如图片和声音文件中的数据 ...
- 敏捷个人-认识自我,管理自我 v0.8.pdf 下载
2009年我在blog上写了个人管理系列的一些blog,其中一些文章深受大家的喜欢.想到写这个系列是源于在实施敏捷Scrum方法时,对方法实施是否对人的水平需要高要求的一些思考.自组织团队是建立在敏捷 ...
- Android之Activity的生命周期
PS:写一发关于Activity的生命周期,也算是面试的重点内容. 学习内容: 1.Activity的生命周期 2.面对多种情况的时候Activity的生命周期 3.onSaveInstanceSta ...
- java并发编程实战学习(3)--基础构建模块
转自:java并发编程实战 5.3阻塞队列和生产者-消费者模式 BlockingQueue阻塞队列提供可阻塞的put和take方法,以及支持定时的offer和poll方法.如果队列已经满了,那么put ...
- Javascript动画效果(四)
Javascript动画效果(四) 前面我们自己写了一个小小的关于js动画的插件,下面我们来使用之前的框架来完成我们想要的动画效果.我们经常在淘宝网中看到,鼠标经过某一图片时,该图片有从上滚出而又从下 ...