APNS蘋果遠(yuǎn)程推送

什么是APNS?

蘋果推送通知服務(wù)(APNs)是推送通知的網(wǎng)關(guān),iPhone ipad 對于應(yīng)用程序在后臺運(yùn)行有諸多限制,考慮到手機(jī)電池電量,應(yīng)用不允許在后臺進(jìn)行過多的操作。因此,當(dāng)用戶切換到其他程序后,原先的程序無法保持運(yùn)行狀態(tài)。對于那些需要保持持續(xù)連接狀態(tài)的應(yīng)用程序(比如社區(qū)網(wǎng)絡(luò)應(yīng)用),將不能收到實(shí)時的信息。推送是解決輪詢所造成的流量消耗和電量消耗的一個比較好的解決方案

為解決這一限制,蘋果推出了APNs(蘋果推送通知服務(wù) Apple Push Notification services)。APNs 允許設(shè)備與蘋果的推送通知服務(wù)器保持常連接狀態(tài)。當(dāng)你想發(fā)送一個推送通知給某個用戶的iPhone上的應(yīng)用程序時,你可以使用 APNs 發(fā)送一個推送消息給目標(biāo)設(shè)備上已安裝的某個應(yīng)用程序。

蘋果的推送服務(wù)APNs基本原理簡單來說就是蘋果利用自己專門的推送服務(wù)器(APNs)接收來自我們自己應(yīng)用服務(wù)器的需要被推送的信息,然后推送到指定的iOS設(shè)備上,然后由設(shè)備通知到我們的應(yīng)用程序,設(shè)備以通知或者聲音的形式通知用戶有新的消息。推送的前提是裝有我們應(yīng)用的設(shè)備需要向APNs服務(wù)器注冊,注冊成功后APNs服務(wù)器會返給我們一個device_token,拿到這個token后我們將這個token發(fā)給我們自己的應(yīng)用服務(wù)器,當(dāng)有需要被推送的消息時,我們的應(yīng)用服務(wù)器會將消息按指定的格式打包,然后結(jié)合設(shè)備的device_token一并發(fā)給APNs服務(wù)器,由于我們的應(yīng)用和APNs維持一個基于TCP的長連接,APNs將新消息推送到我們設(shè)備上,然后在屏幕上顯示出新消息來。

推送流程

  • 獲取設(shè)備device_token階段

上圖完成了如下步驟:

1.Device連接APNs服務(wù)器并攜帶設(shè)備序列號
2.連接成功,APNs經(jīng)過打包和處理產(chǎn)生device_token并返回給注冊的Device
3.Device攜帶獲取的device_token向我們自己的應(yīng)用服務(wù)器注冊
4.完成需要被推送的Device在APNs服務(wù)器和我們自己的應(yīng)用服務(wù)器注冊

執(zhí)行順序如下所示:


這里要提到的一點(diǎn)是,我們的設(shè)備和APNS服務(wù)器之間的通訊是基于SSL協(xié)議的TCP流通訊,二者之間維持一個長連接,當(dāng)從APNS服務(wù)器注冊成功后,一定要將device_token發(fā)送給我們的應(yīng)用服務(wù)器,因?yàn)樵谕扑瓦^程中,首相是由我們的應(yīng)用服務(wù)器(上圖中Provider)將需要推送的消息結(jié)合device_token按指定格式(后面會提到)打包然后發(fā)送給APNS服務(wù)器,然后由APNS服務(wù)器推送給我們的設(shè)備。

消息推送過程

推送的過程經(jīng)過如下步驟:

19175942_bHsb.jpeg

1.首先,安裝了具有推送功能的應(yīng)用,我們的設(shè)備在有網(wǎng)絡(luò)的情況下會連接蘋果推送服務(wù)器,連接過程中,APNS會驗(yàn)證device_token,連接成功后維持一個長連接;

2.Provider(我們自己的服務(wù)器)收到需要被推送的消息并結(jié)合被推送設(shè)備的device_token一起打包發(fā)送給APNS服務(wù)器;

3.APNS服務(wù)器將推送信息推送給指定device_token的設(shè)備;

4.設(shè)備收到推送消息后通知我們的應(yīng)用程序并顯示和提示用戶(聲音、彈出框)

3.3 完整流程介紹

比較直觀的流程參照下圖:

![](http://upload-images.jianshu.io/upload_images/5220087-da89da5c9b8b43fb.jpeg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
應(yīng)用啟用推送通知功能,需要用戶確認(rèn);

應(yīng)用收到設(shè)備識別ID(device token),相當(dāng)于接收推送通知的地址;

應(yīng)用將設(shè)備識別ID發(fā)送到你開發(fā)的服務(wù)器;

當(dāng)有推送通知的需要時,你就可以通過你開發(fā)的服務(wù)組件發(fā)送信息到蘋果的服務(wù)器上;

蘋果推送通知服務(wù)將信息推送到用戶的設(shè)備上。

上圖顯示了我們的應(yīng)用服務(wù)器將消息推送到我們的App的完整路徑,其實(shí)真正完成推送的是APNS服務(wù)器,我們自己的應(yīng)用服務(wù)器只是將需要推送的消息告訴蘋果服務(wù)器,至于如何維護(hù)消息隊(duì)列或如何保證消息能被推送到指定的設(shè)備上,這些都由蘋果APNS給我們做完了

Push機(jī)制類型

四種:徽章、提示框、聲音和橫幅,具體表現(xiàn)形式如下圖

  • Push機(jī)制的4個組件
  • Provider
  • APNS
  • iPhone設(shè)備
  • Client App
    其中APNS(Apple Push Notification Service)是由蘋果提供的消息推送服務(wù)中心,所有的消息都經(jīng)由這里轉(zhuǎn)發(fā)給相應(yīng)的設(shè)備

證書生成

![Uploading 19175943_bPmM_098454.jpeg . . .]

先概述下大致過程,然后下面會截圖給出詳細(xì)的步驟

在Mac上生成 Apple推送通知SSL許可證:

  1. 登錄到 apple Developer Connection Portal 并點(diǎn)擊 App IDs
  2. 創(chuàng)建一個不使用通配符的 App ID 。通配符 ID 不能用于推送通知服務(wù)。例如,我們的iPhone程序ID像這樣:54im.com.PushChat
  3. 點(diǎn)擊App ID旁的“Configure”,然后按下按鈕生產(chǎn) 推送通知許可證。根據(jù)“向?qū)А敝笇?dǎo)的步驟生成一個簽名并上傳,最后下載生成的許可證。
  4. 通過雙擊.cer文件將你的 aps_developer_identity.cer 引入Keychain中。
  5. 在Mac上啟動 Keychain助手,然后在login keychain中選擇 Certificates分類。你將看到一個可擴(kuò)展選項(xiàng)“Apple Development Push Servicescom.54im.PushChat”
  6. 擴(kuò)展此選項(xiàng)然后右擊“Apple Development Push Services” > Export “Apple Development Push Services:com.54im.PushChat”。保存為 PushChat_cert.p12 文件。
  7. 擴(kuò)展“Apple Development Push Services” 對“Private Key”做同樣操作,保存為 PushChat_key.p12 文件。
  8. 需要通過終端命令將這些文件轉(zhuǎn)換為PEM格式:
    openssl pkcs12 -clcerts -nokeys -out cert.pem -in PushChat_cert.p12
  9. 轉(zhuǎn)換得到key的pem:
    openssl pkcs12 -nocerts -out key.pem -in PushChat_key.p12
  10. 如果你想要移除密碼,要么在導(dǎo)出/轉(zhuǎn)換時不要設(shè)定或者執(zhí)行:
    openssl rsa -in key.pem -out key.unencrypted.pem
  11. 最后,你需要將鍵和許可文件合成為apns-dev.pem文件,此文件在連接到APNS時需要使用:
    cat apns-dev-cert.pem key.unencrypted.pem > ck.pem

5.2.2 配置詳解

  1. 創(chuàng)建APPID

首先登陸我們的Apple Developer后臺為將要使用推送服務(wù)的App新建一個App ID,如下圖,點(diǎn)擊新建后輸入基本信息

APPID創(chuàng)建好后,我們點(diǎn)編輯剛剛生成好的APPID,生成下development證書,生產(chǎn)情況下用 Production證書

創(chuàng)建正式過程中,要求上傳一張Certificate Signing Request 證書請求簽名文件

  1. 生成證書請求文件
    輸入證書信息


3. 生成PUSH證書

還記得剛剛蘋果開發(fā)者那里要上傳的證書不,將生成好的這個.certSigningRequest證書上傳上去,


下載aps_development.cer這個證書到mac上,如果是發(fā)布版的推送證書,就為aps_production.cer。然后雙擊該證書,將推送證書安裝到我們的Mac機(jī)器上,安裝成功后會看到如下界面(如果是發(fā)布版,則證書的Development部分顯示的是Production)

需要為certificate和它之下的private key各自export出一個.p12文件。(會出現(xiàn)設(shè)置密碼過程)

  1. 導(dǎo)出公鑰


    19175956_NaCN.jpeg

導(dǎo)出私鑰


19175957_UHfr.jpeg
  1. key轉(zhuǎn)換

需要將上面的2個.p12文件轉(zhuǎn)成.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不進(jìn)行加密
openssl rsa -in key.pem -out key.unencrypted.pem

然后就可以 合并兩個.pem文件, 這個ck.pem就是服務(wù)端需要的證書了
cat cert.pem key.unencrypted.pem > apns-dev.pem

創(chuàng)建 Provisioning Profile

5.3 創(chuàng)建 provisioning profile

  1. key轉(zhuǎn)換

需要將上面的2個.p12文件轉(zhuǎn)成.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不進(jìn)行加密
openssl rsa -in key.pem -out key.unencrypted.pem

然后就可以 合并兩個.pem文件, 這個ck.pem就是服務(wù)端需要的證書了
cat cert.pem key.unencrypted.pem > apns-dev.pem

創(chuàng)建 Provisioning Profile
5.3 創(chuàng)建 provisioning profile


![](http://upload-images.jianshu.io/upload_images/5220087-3f1d413cc7539c33.jpeg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
19180001_675W.jpeg

將該prifiles文件下載下來,名字是PushChat.mobileprovision,其實(shí)不用下載,xcode里面會根據(jù)你的項(xiàng)目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服務(wù)器地址

NSString *host = @"api.54im.com/pushchat";

現(xiàn)在可以把項(xiàng)目編譯到iphone或者ipad上面了,注意項(xiàng)目 General中team配置。


![ ![ ![](http://upload-images.jianshu.io/upload_images/5220087-dc3dc5068d2a89c9.jpeg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ](http://upload-images.jianshu.io/upload_images/5220087-082f7287a6652996.jpeg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ](http://upload-images.jianshu.io/upload_images/5220087-5f97f03508dee99a.jpeg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容