Push的原理:ios
Push 的工做機制能夠簡單的歸納爲下圖
web
圖中,Provider是指某個iPhone軟件的Push服務器,這篇文章我將使用.net做爲Provider。
APNS 是Apple Push Notification Service(Apple Push服務器)的縮寫,是蘋果的服務器。xcode
上圖能夠分爲三個階段。服務器
第一階段:.net應用程序把要發送的消息、目的iPhone的標識打包,發給APNS。
第二階段:APNS在自身的已註冊Push服務的iPhone列表中,查找有相應標識的iPhone,並把消息發到iPhone。
第三階段:iPhone把發來的消息傳遞給相應的應用程序, 而且按照設定彈出Push通知。app
從上圖咱們能夠看到。ide
一、首先是應用程序註冊消息推送。google
二、 IOS跟APNS Server要deviceToken。應用程序接受deviceToken。spa
三、應用程序將deviceToken發送給PUSH服務端程序。.net
四、 服務端程序向APNS服務發送消息。code
五、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生成.net和APNS通訊的證書文件。
一、將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應用程序。