欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS消息推送原理及具體實(shí)現(xiàn)代碼

 更新時(shí)間:2016年09月21日 15:37:33   作者:山天大畜  
這篇文章主要為大家詳細(xì)介紹了iOS 消息推送原理及具體實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

一、消息推送原理

在實(shí)現(xiàn)消息推送之前先提及幾個(gè)于推送相關(guān)概念,如下圖1-1:

1、Provider:就是為指定IOS設(shè)備應(yīng)用程序提供Push的服務(wù)器,(如果IOS設(shè)備的應(yīng)用程序是客戶(hù)端的話,那么Provider可以理解為服務(wù)端[消息的發(fā)起者]);

2、APNS:Apple Push Notification Service[蘋(píng)果消息推送服務(wù)器];

3、iPhone:用來(lái)接收APNS下發(fā)下來(lái)的消息;

4、Client App:IOS設(shè)備上的應(yīng)用程序,用來(lái)接收iphone傳遞APNS下發(fā)的消息到制定的一個(gè)客戶(hù)端app[消息的最終響應(yīng)者];

上圖可以分為三個(gè)階段:

階段一:Provider[服務(wù)端]把要發(fā)送的消息,目的IOS設(shè)備標(biāo)識(shí)打包,發(fā)送給APNS;

階段二:APNS在自身的已注冊(cè)Push服務(wù)的IOS設(shè)備列表中,查找有相應(yīng)標(biāo)識(shí)的IOS設(shè)備,并將消息發(fā)送到IOS設(shè)備;

階段三:IOS設(shè)備把發(fā)送的消息傳遞給對(duì)應(yīng)的應(yīng)用程序,并且按照設(shè)定彈出Push通知。

具體過(guò)程,如下圖1-2:

1、[Client App]注冊(cè)消息推送;

2、[Client App]跟[APNS Service]要deviceToken, Client App接收deviceToken;

3、[Client App]將deviceToken發(fā)送給[Provider]Push服務(wù)端程序;

4、當(dāng)Push服務(wù)端程序滿(mǎn)足發(fā)送消息條件了,[Provider]向[APNS Service]發(fā)送消息;

5、[APNS Service]將消息發(fā)送給[Client App].

二、消息推送實(shí)現(xiàn)

1、生成*.certSigningRequest文件,步驟如下:

[MacBook-應(yīng)用程序-實(shí)用工具-鑰匙串訪問(wèn)-證書(shū)助手-從證書(shū)機(jī)構(gòu)求證書(shū)-證書(shū)信息(用戶(hù)電子郵箱地址{填寫(xiě)您的郵箱,如:your@email.com},常用名稱(chēng){任意,如:PushDemo},請(qǐng)求是:{單選,選擇‘存儲(chǔ)到磁盤(pán)'})-繼續(xù)-保存],這時(shí)會(huì)在您指定的地方生成你指定的文件,默認(rèn)為CertificateSigningRequest.certSigningRequest文件,這里命名為:PushDemo.certSigningRequest.在此*.certSigningRequest已經(jīng)生成,具體操作步驟如圖所示。

如果生成成功,則會(huì)在[鑰匙串訪問(wèn)|登錄|密鑰]欄目中列出與*.certSigningRequest關(guān)聯(lián)的密鑰,這里是PushDemo,如圖所示:

2、新建一個(gè)App ID(在蘋(píng)果開(kāi)發(fā)者賬號(hào)中配置)

(1)登錄iOS Dev Center,登錄成功后,點(diǎn)擊(iOS Provisioning Portal對(duì)應(yīng)鏈接),如圖所示:

(2)創(chuàng)建New App ID[App IDsàManageàNew App ID]( Description{填寫(xiě)您對(duì)此App ID的描述,如:iShop},Bundle Seed ID(App ID Prefix){選擇綁定App ID前綴,如:默認(rèn)選擇Generate New},Bundle Identifier(App ID Suffix){填寫(xiě)綁定App ID后綴,如:com.yourcorp.iShop}),如下圖所示:

這樣就會(huì)生成下面這條記錄,如圖所示:

(3)配置上一步中生成的App ID,讓其支持消息推送[點(diǎn)擊2-6中的Configureà選中Enable for Apple Push Notification serviceà點(diǎn)擊Configure],如圖所示:

(4) Generate a Certificate Signing Request(生成部署請(qǐng)求認(rèn)證)[點(diǎn)擊2-7中的2ConfigureàContinueà步驟1生成的*certSigningRequest文件(這里是iShop.certSigningRequest)-Generate-生成完成后將其下載下來(lái),命名為:aps_developer.cer],雙擊aps_developer.cer證書(shū){將證書(shū)與密鑰關(guān)聯(lián),并將證書(shū)導(dǎo)入到MacBook中},如下圖所示:

(5)創(chuàng)建Development Provisioning Profiles[開(kāi)發(fā)許可配置文件](Provisioning| Development|New Profile),具體操作流程如下圖所示:

點(diǎn)擊圖中Submit,生成Development Provisioning Profiles[開(kāi)發(fā)許可配置文件],這里是:iShopDevprofile.mobileprovision如下圖所示:

下載此開(kāi)發(fā)許可證書(shū)(用于聯(lián)機(jī)調(diào)試)。

總結(jié),到現(xiàn)在為止,我們已經(jīng)生成:A:*.certSigningRequest文件(在步驟(4)中使用,用于生成證書(shū)B(niǎo))、B:aps_developer_identity.cer證書(shū)(在Provider[Push服務(wù)器]服務(wù)端應(yīng)用使用)、C:*..mobileprovision開(kāi)發(fā)許可配置文件(在Client App客戶(hù)端應(yīng)用聯(lián)機(jī)調(diào)試使用)。

3、新建一個(gè)項(xiàng)目

1. 創(chuàng)建一個(gè)"single view application" project,為省事,你設(shè)置的"Company Identifier" + "Production“必須和step 5創(chuàng)建的App ID的"bundle identifier"一致。
2. 在AppDelegate.mfile的"didFinishLaunchingWithOptions" method里,添加下列代碼 (用于為app register push notification feature):

//Letthedeviceknowwewanttoreceivepushnotifications
[[UIApplicationsharedApplication]registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert)];



3.在AppDelegate.mfile里添加下列2個(gè)methods (用來(lái)handle register remote notification with device token和register error的events)
-(void)application:(UIApplication*)applicationdidRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken

{
NSLog(@"Mytokenis:%@",deviceToken);
}

-(void)application:(UIApplication*)applicationdidFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
NSLog(@"Failedtogettoken,error:%@",error);
}

4. 運(yùn)行該app in real device (simulator doesn't support push notification)。這時(shí)你會(huì)在device上看到一個(gè)popup window (該窗口只會(huì)出現(xiàn)一次,重裝app不會(huì)再出現(xiàn)),提示你該app會(huì)send push notification給你,如果同意則clickOK,不同意則click "Now allow”。如果選擇了OK,那么在"Setting > Notifications“里會(huì)有你的app在list里。而且這時(shí)你的Xcode output console會(huì)顯示你的device token。

5: export "PushDemo" private key to a ".p12" file(該文件會(huì)在后面生成apns provider的.p12 or .pem file時(shí)用到)
1). right click "PushDemo“ private key and select "Export ..."PushDemo
2). Save the private key as “PushDemoKey.p12” file, click Save button
3). 這時(shí)會(huì)讓你輸入2次用于加密該.p12 file的密碼,例如用"123321",接著會(huì)要求你輸入一次your mac account password

6: 在5中生成的“PushDemoKey.p12” file和step 6生成的"aps_development.cer" file是用于APNS provider side的源文件,APNS Provider side進(jìn)行push message時(shí)要用到的"cert + key" file就是通過(guò)這2個(gè)file來(lái)生成。該Step就是用來(lái)生成for APNS provider side (php version)要用到這個(gè)"cert + key" pem file.

1)open Terminal, go to Desktop (假設(shè)這2個(gè)file都在desktop里)
2)執(zhí)行下列命令來(lái)生成和apns cer file對(duì)應(yīng)的pem file "PushDemoCert.pem"

opensslx509-inaps_development.cer-informder-outPushDemoCert.pem

3)執(zhí)行下列命令來(lái)生成和private key .p12 file對(duì)應(yīng)的pem file "PushDemoKey.pem" (注意:執(zhí)行過(guò)程會(huì)要求你輸入"PushDemoKey.p12"創(chuàng)建時(shí)設(shè)置的密碼,以及設(shè)置"PushDemoKey.pem”的密碼)

opensslpkcs12-nocerts-outPushDemoKey.pem-inPushDemoKey.p12

4)執(zhí)行下列命令把step 11.2生成的cert pem file和step 11.3生成的key pem file合成為一個(gè)pem file"PushDemoCK.pem"

catPushDemoCert.pemPushDemoKey.pem>PushDemoCK.pem

7:在5生成的“PushDemoKey.p12” file和step 6生成的"aps_development.cer" file是用于APNS provider side的源文件,該step是簡(jiǎn)單測(cè)試這2個(gè)file是否有效
1)open Terminal, go to Desktop (假設(shè)這2個(gè)file都在desktop里)
2) 執(zhí)行下列命令來(lái)測(cè)試是否能夠connect apple提供的不經(jīng)加密(即不需使用任何證書(shū)?。┑腁PNS server

telnetgateway.sandbox.push.apple.com2195

如果你看到下列輸出,則表示你的電腦可以connect APNS. 如果出現(xiàn)error,那么check你的firewall是否允許outgoing connections on port 2195。
Trying17.172.233.65...
Connectedtogateway.sandbox.push-apple.com.akadns.net.
Escapecharacteris'^]'.
Press Ctrl+C to close the connection.

3)執(zhí)行下列命令來(lái)測(cè)試是否能夠connect apple提供的經(jīng)加密(需使用2) and 3)生成的2個(gè)pem file?。┑腁PNS "sandbox“ server for development.

openssls_client-connectgateway.sandbox.push.apple.com:2195-certPushDemoCert.pem-keyPushDemoKey.pem

執(zhí)行過(guò)程中會(huì)要你輸入PushDemoKey.pem生成時(shí)設(shè)置的密碼。如果connect server成功,就會(huì)等待你輸入字串,你可以輸入任意字串,然后回車(chē),就會(huì)disconnectserver。如果連接不成功,則openssl會(huì)顯示錯(cuò)誤信息。
注意:實(shí)際上有2個(gè)APNS servers: the “sandbox” server (用于testing) the live server(用于production mode)。我們這里測(cè)試的是sandbox server。live apns server的操作類(lèi)似。

8:創(chuàng)建provider server side (php version)
1). DownloadSimplePush PHP codeto your mac machine and then unzip it.
2). 去掉SimplePushfolder里的pk.pem (它沒(méi)用),把step 11.4生成的"PushDemoCK.pem" copy toSimplePushfolder
3). 修改simplepush.phpfile下面幾行:

// Put your device token here (without spaces):
//device token來(lái)自Step 10的第4點(diǎn),在output console獲取,注意:要去掉前后的尖括號(hào),和中間的所有空格
$deviceToken= '43fcc3cff12965bc45bf842bf9166fa60e8240c575d0aeb0bf395fb7ff86b465';
// Put your private key's passphrase here:
//該值是 3)生成PushDemoKey.pem時(shí)設(shè)置的密碼
$passphrase= '123456';
// Put your alert message here:
$message= 'My first push notification!';
//.....stream_context_set_option($ctx, 'ssl', 'local_cert', 'PushDemoCK.pem');

4). 在terminal window里,go to the simplepush folder,然后執(zhí)行下列命令,你的iPhone應(yīng)該會(huì)收到一條push message。

phpsimplepush.php

注意:如果你的app在iphone里是正在運(yùn)行,而且app是在front end時(shí),當(dāng)它收到push message時(shí)是不會(huì)出現(xiàn)在iPhone頂部的notification area的!

參考文章:http://mmz06.blog.163.com/blog/static/121416962011111710934946/

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。     

相關(guān)文章

最新評(píng)論