最近兩天在研究ios的消息推送機制。研究這個東西,還是充滿興趣的。
Push的原理:
Push 的工作機制可以簡單的概括為下圖

圖中,Provider是指某個iPhone軟體的Push伺服器,這篇文章我将使用.net作為Provider。
APNS 是Apple Push Notification Service(Apple Push伺服器)的縮寫,是蘋果的伺服器。
上圖可以分為三個階段。
第一階段:.net應用程式把要發送的消息、目的iPhone的辨別打包,發給APNS。
第二階段:APNS在自身的已注冊Push服務的iPhone清單中,查找有相應辨別的iPhone,并把消息發到iPhone。
第三階段:iPhone把發來的消息傳遞給相應的應用程式, 并且按照設定彈出Push通知。
從上圖我們可以看到。
1、首先是應用程式注冊消息推送。
2、 IOS跟APNS Server要deviceToken。應用程式接受deviceToken。
3、應用程式将deviceToken發送給PUSH服務端程式。
4、 服務端程式向APNS服務發送消息。
5、APNS服務将消息發送給iPhone應用程式。
無論是iPhone用戶端跟APNS,還是Provider和APNS都需要通過證書進行連接配接的。下面我介紹一下幾種用到的證書。
幾種證書:
一、*.certSigningRequest檔案
1、生成Certificate Signing Request (CSR):
2、填寫你的郵箱和Common Name,這裡填寫為PushChat。選擇儲存到硬碟。
這樣就在本地生成了一個PushChat.certSigningRequest檔案。
二、生成*.p12檔案
1、導出密鑰,并輸入你的密碼。
輸入你的密碼:
這樣就生成了一個PushChatKey.p12檔案。
三、建立一個App ID 和SSL certificate檔案
1、用你的付過費的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個檔案。
1、PushChat.certSigningRequest
2、PushChatKey.p12
3、aps_developer_identity.cer
現在我們建立一個簡單的iPhone應用程式。
1、打開Xcode,選擇建立一個View-based Application。命名如下圖:
2、在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來的通知。
3、在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
1、将aps_developer_identity.cer轉換成 aps_developer_identity.pem格式。
openssl x509 -in aps_developer_identity.cer -inform DER -out aps_developer_identity.pem -outform PEM
2、将p12格式的私鑰轉換成pem,需要設定4次密碼,密碼都設定為:abc123。
openssl pkcs12 -nocerts -out PushChat_Noenc.pem -in PushChat.p12
3、用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應用程式。