什么是APNS?
蘋果推送通知服務(APNs)是推送通知的網關,iPhone ipad 對于應用程序在后臺運行有諸多限制,考慮到手機電池電量,應用不允許在后臺進行過多的操作。因此,當用戶切換到其他程序后,原先的程序無法保持運行狀態。對于那些需要保持持續連接狀態的應用程序(比如社區網絡應用),將不能收到實時的信息。推送是解決輪詢所造成的流量消耗和電量消耗的一個比較好的解決方案
為解決這一限制,蘋果推出了APNs(蘋果推送通知服務 Apple Push Notification services)。APNs 允許設備與蘋果的推送通知服務器保持常連接狀態。當你想發送一個推送通知給某個用戶的iPhone上的應用程序時,你可以使用 APNs 發送一個推送消息給目標設備上已安裝的某個應用程序。
蘋果的推送服務APNs基本原理簡單來說就是蘋果利用自己專門的推送服務器(APNs)接收來自我們自己應用服務器的需要被推送的信息,然后推送到指定的iOS設備上,然后由設備通知到我們的應用程序,設備以通知或者聲音的形式通知用戶有新的消息。推送的前提是裝有我們應用的設備需要向APNs服務器注冊,注冊成功后APNs服務器會返給我們一個device_token,拿到這個token后我們將這個token發給我們自己的應用服務器,當有需要被推送的消息時,我們的應用服務器會將消息按指定的格式打包,然后結合設備的device_token一并發給APNs服務器,由于我們的應用和APNs維持一個基于TCP的長連接,APNs將新消息推送到我們設備上,然后在屏幕上顯示出新消息來。
推送流程
- 獲取設備device_token階段
上圖完成了如下步驟:
1.Device連接APNs服務器并攜帶設備序列號
2.連接成功,APNs經過打包和處理產生device_token并返回給注冊的Device
3.Device攜帶獲取的device_token向我們自己的應用服務器注冊
4.完成需要被推送的Device在APNs服務器和我們自己的應用服務器注冊
執行順序如下所示:
這里要提到的一點是,我們的設備和APNS服務器之間的通訊是基于SSL協議的TCP流通訊,二者之間維持一個長連接,當從APNS服務器注冊成功后,一定要將device_token發送給我們的應用服務器,因為在推送過程中,首相是由我們的應用服務器(上圖中Provider)將需要推送的消息結合device_token按指定格式(后面會提到)打包然后發送給APNS服務器,然后由APNS服務器推送給我們的設備。
消息推送過程
推送的過程經過如下步驟:
1.首先,安裝了具有推送功能的應用,我們的設備在有網絡的情況下會連接蘋果推送服務器,連接過程中,APNS會驗證device_token,連接成功后維持一個長連接;
2.Provider(我們自己的服務器)收到需要被推送的消息并結合被推送設備的device_token一起打包發送給APNS服務器;
3.APNS服務器將推送信息推送給指定device_token的設備;
4.設備收到推送消息后通知我們的應用程序并顯示和提示用戶(聲音、彈出框)
3.3 完整流程介紹
比較直觀的流程參照下圖:
應用啟用推送通知功能,需要用戶確認;
應用收到設備識別ID(device token),相當于接收推送通知的地址;
應用將設備識別ID發送到你開發的服務器;
當有推送通知的需要時,你就可以通過你開發的服務組件發送信息到蘋果的服務器上;
蘋果推送通知服務將信息推送到用戶的設備上。
上圖顯示了我們的應用服務器將消息推送到我們的App的完整路徑,其實真正完成推送的是APNS服務器,我們自己的應用服務器只是將需要推送的消息告訴蘋果服務器,至于如何維護消息隊列或如何保證消息能被推送到指定的設備上,這些都由蘋果APNS給我們做完了
Push機制類型
四種:徽章、提示框、聲音和橫幅,具體表現形式如下圖
- Push機制的4個組件
- Provider
- APNS
- iPhone設備
- Client App
其中APNS(Apple Push Notification Service)是由蘋果提供的消息推送服務中心,所有的消息都經由這里轉發給相應的設備
證書生成
![Uploading 19175943_bPmM_098454.jpeg . . .]
先概述下大致過程,然后下面會截圖給出詳細的步驟
在Mac上生成 Apple推送通知SSL許可證:
- 登錄到 apple Developer Connection Portal 并點擊 App IDs
- 創建一個不使用通配符的 App ID 。通配符 ID 不能用于推送通知服務。例如,我們的iPhone程序ID像這樣:54im.com.PushChat
- 點擊App ID旁的“Configure”,然后按下按鈕生產 推送通知許可證。根據“向導”指導的步驟生成一個簽名并上傳,最后下載生成的許可證。
- 通過雙擊.cer文件將你的 aps_developer_identity.cer 引入Keychain中。
- 在Mac上啟動 Keychain助手,然后在login keychain中選擇 Certificates分類。你將看到一個可擴展選項“Apple Development Push Servicescom.54im.PushChat”
- 擴展此選項然后右擊“Apple Development Push Services” > Export “Apple Development Push Services:com.54im.PushChat”。保存為 PushChat_cert.p12 文件。
- 擴展“Apple Development Push Services” 對“Private Key”做同樣操作,保存為 PushChat_key.p12 文件。
- 需要通過終端命令將這些文件轉換為PEM格式:
openssl pkcs12 -clcerts -nokeys -out cert.pem -in PushChat_cert.p12 - 轉換得到key的pem:
openssl pkcs12 -nocerts -out key.pem -in PushChat_key.p12 - 如果你想要移除密碼,要么在導出/轉換時不要設定或者執行:
openssl rsa -in key.pem -out key.unencrypted.pem - 最后,你需要將鍵和許可文件合成為apns-dev.pem文件,此文件在連接到APNS時需要使用:
cat apns-dev-cert.pem key.unencrypted.pem > ck.pem
5.2.2 配置詳解
- 創建APPID
首先登陸我們的Apple Developer后臺為將要使用推送服務的App新建一個App ID,如下圖,點擊新建后輸入基本信息
APPID創建好后,我們點編輯剛剛生成好的APPID,生成下development證書,生產情況下用 Production證書
創建正式過程中,要求上傳一張Certificate Signing Request 證書請求簽名文件
-
生成證書請求文件
輸入證書信息
3. 生成PUSH證書
還記得剛剛蘋果開發者那里要上傳的證書不,將生成好的這個.certSigningRequest證書上傳上去,
下載aps_development.cer這個證書到mac上,如果是發布版的推送證書,就為aps_production.cer。然后雙擊該證書,將推送證書安裝到我們的Mac機器上,安裝成功后會看到如下界面(如果是發布版,則證書的Development部分顯示的是Production)
需要為certificate和它之下的private key各自export出一個.p12文件。(會出現設置密碼過程)
-
導出公鑰
19175956_NaCN.jpeg
導出私鑰
- key轉換
需要將上面的2個.p12文件轉成.pem格式:
openssl pkcs12 -clcerts -nokeys -out cert.pem -in Push_Chat_cert.p12
openssl pkcs12 -nocerts -out key.pem -in Push_Chat_cert_key.p12
如果需要對key不進行加密
openssl rsa -in key.pem -out key.unencrypted.pem
然后就可以 合并兩個.pem文件, 這個ck.pem就是服務端需要的證書了
cat cert.pem key.unencrypted.pem > apns-dev.pem
創建 Provisioning Profile
5.3 創建 provisioning profile
- key轉換
需要將上面的2個.p12文件轉成.pem格式:
openssl pkcs12 -clcerts -nokeys -out cert.pem -in Push_Chat_cert.p12
openssl pkcs12 -nocerts -out key.pem -in Push_Chat_cert_key.p12
如果需要對key不進行加密
openssl rsa -in key.pem -out key.unencrypted.pem
然后就可以 合并兩個.pem文件, 這個ck.pem就是服務端需要的證書了
cat cert.pem key.unencrypted.pem > apns-dev.pem
創建 Provisioning Profile
5.3 創建 provisioning profile
將該prifiles文件下載下來,名字是PushChat.mobileprovision,其實不用下載,xcode里面會根據你的項目id自動去拉對于的這個文件
客戶端制作
#import "AppDelegate.h"
@implementation AppDelegate
/**
* This is what you need to add to your applicationDidFinishLaunching
*/
- (void)applicationDidFinishLaunching:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication]
registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
// Clear application badge when app launches
application.applicationIconBadgeNumber = 0;
}
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
// Add registration for remote notifications
[[UIApplication sharedApplication]
registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
// Clear application badge when app launches
application.applicationIconBadgeNumber = 0;
}
/*
* -------------------------------------------------------
* BEGIN APNS CODE
*/
/**
* Fetch and Format Device Token and Register Important Information to Remote Server
*/
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
#if !TARGET_IPHONE_SIMULATOR
// Get Bundle Info for Remote Registration (handy if you have more than one app)
NSString *appName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
NSString *appVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
// Check what Notifications the user has turned on. We registered for all three, but they may have manually disabled some or all of them.
NSUInteger rntypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
// Set the defaults to disabled unless we find otherwise...
NSString *pushBadge = (rntypes & UIRemoteNotificationTypeBadge) ? @"enabled" : @"disabled";
NSString *pushAlert = (rntypes & UIRemoteNotificationTypeAlert) ? @"enabled" : @"disabled";
NSString *pushSound = (rntypes & UIRemoteNotificationTypeSound) ? @"enabled" : @"disabled";
// Get the users Device Model, Display Name, Unique ID, Token & Version Number
UIDevice *dev = [UIDevice currentDevice];
NSString *deviceName = dev.name;
NSString *deviceModel = dev.model;
NSString *deviceSystemVersion = dev.systemVersion;
// Prepare the Device Token for Registration (remove spaces and < >)
NSString *deviceToken = [[[[devToken description] stringByReplacingOccurrencesOfString:@"<"withString:@""] stringByReplacingOccurrencesOfString:@">" withString:@""] stringByReplacingOccurrencesOfString: @" " withString: @""];
// Build URL String for Registration
// !!! CHANGE "www.mywebsite.com" TO YOUR WEBSITE. Leave out the http://
// !!! SAMPLE: "secure.awesomeapp.com"
NSString *host = @"121.199.25.24/pushchat";
// !!! CHANGE "/apns.php?" TO THE PATH TO WHERE apns.php IS INSTALLED
// !!! ( MUST START WITH / AND END WITH ? ).
// !!! SAMPLE: "/path/to/apns.php?"
NSString *urlString = [NSString stringWithFormat:@"/apns.php?task=%@&appname=%@&appversion=%@&devicetoken=%@&devicename=%@&devicemodel=%@&deviceversion=%@&pushbadge=%@&pushalert=%@&pushsound=%@", @"register", appName,appVersion, deviceToken, deviceName, deviceModel, deviceSystemVersion, pushBadge, pushAlert, pushSound];
// Register the Device Data
// !!! CHANGE "http" TO "https" IF YOU ARE USING HTTPS PROTOCOL
NSURL *url = [[NSURL alloc] initWithScheme:@"http" host:host path:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *urlR, NSData *returnData, NSError *e) {
NSLog(@"Return Data: %@", returnData);
}];
NSLog(@"Register URL: %@", url);
#endif
}
/**
* Failed to Register for Remote Notifications
*/
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
#if !TARGET_IPHONE_SIMULATOR
NSLog(@"Error in registration. Error: %@", error);
#endif
}
/**
* Remote Notification Received while application was open.
*/
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
#if !TARGET_IPHONE_SIMULATOR
NSLog(@"remote notification: %@",[userInfo description]);
NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];
NSString *alert = [apsInfo objectForKey:@"alert"];
NSLog(@"Received Push Alert: %@", alert);
NSString *sound = [apsInfo objectForKey:@"sound"];
NSLog(@"Received Push Sound: %@", sound);
//AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
NSString *badge = [apsInfo objectForKey:@"badge"];
NSLog(@"Received Push Badge: %@", badge);
application.applicationIconBadgeNumber = [[apsInfo objectForKey:@"badge"] integerValue];
#endif
}
/*
* END APNS CODE
-------------------------------------------------------
*/
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
并且修改里面的push服務器地址
NSString *host = @"api.54im.com/pushchat";
現在可以把項目編譯到iphone或者ipad上面了,注意項目 General中team配置。