转:点击打开连接ios
最近两天在研究ios的消息推送机制。研究这个东西,仍是充满兴趣的。web
Push的原理:xcode
Push 的工做机制能够简单的归纳为下图
服务器
图中,Provider是指某个iPhone软件的Push服务器,这篇文章我将使用.net做为Provider。
APNS 是Apple Push Notification Service(Apple Push服务器)的缩写,是苹果的服务器。app
上图能够分为三个阶段。ide
第一阶段:.net应用程序把要发送的消息、目的iPhone的标识打包,发给APNS。
第二阶段:APNS在自身的已注册Push服务的iPhone列表中,查找有相应标识的iPhone,并把消息发到iPhone。
第三阶段:iPhone把发来的消息传递给相应的应用程序, 而且按照设定弹出Push通知。google
从上图咱们能够看到。spa
一、首先是应用程序注册消息推送。.net
二、 IOS跟APNS Server要deviceToken。应用程序接受deviceToken。code
三、应用程序将deviceToken发送给PUSH服务端程序。
四、 服务端程序向APNS服务发送消息。
五、APNS服务将消息发送给iPhone应用程序。
不管是iPhone客户端跟APNS,仍是Provider和APNS都须要经过证书进行链接的。下面我介绍一下几种用到的证书。
几种证书:
1、*.certSigningRequest文件
一、生成Certificate Signing Request (CSR):
二、填写你的邮箱和Common Name,这里填写为PushChat。选择保存到硬盘。
这样就在本地生成了一个PushChat.certSigningRequest文件。
2、生成*.p12文件
一、导出密钥,并输入你的密码。
输入你的密码:
这样就生成了一个PushChatKey.p12文件。
3、新建一个App ID 和SSL certificate文件
一、用你的付过费的apple账号登陆到iOS Provisioning Portal。新建一个App ID。
Description:中输入PushChat
Bundle Seed ID:默认选择Generate New
Bundle Identifier:输入com.mysoft.PushChat
点击提交
这样就会生成下面这条记录:
点击配置:
出现下面界面,点击继续:
这里咱们选择前面生成好的PushChat.certSigningRequest文件,点击生成。
正在生成
生成完毕,咱们把它下载下来。命名为aps_developer_identity.cer。
点击完成,你会发现状态变成Enabled。
到如今为止,咱们已经生成了3个文件。
一、PushChat.certSigningRequest
二、PushChatKey.p12
三、aps_developer_identity.cer
如今咱们建立一个简单的iPhone应用程序。
一、打开Xcode,选择建立一个View-based Application。命名以下图:
二、在PushChatAppDelegate中的didFinishLaunchingWithOptions方法中加入下面代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window.rootViewController = self.viewController; [self.window makeKeyAndVisible]; // Let the device know we want to receive push notifications [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; return YES; }
经过registerForRemoteNotificationTypes方法,告诉应用程序,能接受push来的通知。
三、在xcode中运行,会弹出下面的提示框:
选择OK。表示此应用程序开启消息通知服务。
在 PushChatAppDelegate.m代码中添加下面方法获取deviceToken :
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken { NSLog(@"My token is: %@", deviceToken); } - (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error { NSLog(@"Failed to get token, error: %@", error); }
获取到的deviceToken,咱们能够经过webservice服务提交给.net应用程序,这里我简单处理,直接打印出来,拷贝到.net应用环境中使用。
发送通知的.net应用程序出来须要知道deviceToken以外,还须要一个与APNS链接的证书。
这个证书能够经过咱们前面生成的两个文件中获得。
使用OpenSSL
一、将aps_developer_identity.cer转换成 aps_developer_identity.pem格式。
openssl x509 -in aps_developer_identity.cer -inform DER -out aps_developer_identity.pem -outform PEM
二、将p12格式的私钥转换成pem,须要设置4次密码,密码都设置为:abc123。
openssl pkcs12 -nocerts -out PushChat_Noenc.pem -in PushChat.p12
三、用certificate和the key 建立PKCS#12格式的文件。
openssl pkcs12 -export -in aps_developer_identity.pem -inkey PushChat_Noenc.pem -certfile PushChat.certSigningRequest -name "aps_developer_identity" -out aps_developer_identity.p12
这样咱们就获得了在.net应用程序中使用的证书文件:aps_developer_identity.p12。
在.net应用程序中发送通知。
有个开源的类库:apns-sharp。
地址是:http://code.google.com/p/apns-sharp/。
咱们下载源代码,对里面的JdSoft.Apple.Apns.Notifications作相应的调整就能用了。
咱们根据DeviceToken和p12File对JdSoft.Apple.Apns.Notifications.Test作相应的调整,以下图。
这样就OK了。
效果:
通知的代码:
for (int i = 1; i <= count; i++) { //Create a new notification to send Notification alertNotification = new Notification(testDeviceToken); alertNotification.Payload.Alert.Body = string.Format("Testing {0}...", i); alertNotification.Payload.Sound = "default"; alertNotification.Payload.Badge = i; //Queue the notification to be sent if (service.QueueNotification(alertNotification)) Console.WriteLine("Notification Queued!"); else Console.WriteLine("Notification Failed to be Queued!"); //Sleep in between each message if (i < count) { Console.WriteLine("Sleeping " + sleepBetweenNotifications + " milliseconds before next Notification..."); System.Threading.Thread.Sleep(sleepBetweenNotifications); } }
用手机拍的ipad上面的显示:
总结:这篇文章主要是详细的讲述了ios消息推送机制的实现,如何经过.net应用程序发送消息给ios应用程序。