詳解IOS開發(fā)之實現(xiàn)App消息推送(最新)
好久沒有寫過博客啦,今天就由本菜鳥給大家做一個簡單的IOSApp消息推送教程吧!一切從0開始,包括XCode6, IOS8, 以及蘋果開發(fā)者中心最新如何注冊應(yīng)用,申請證書以及下載配置概要文件,相信很多剛開始接觸iOS的人會很想了解一下。(ps:網(wǎng)上看了一下雖然有很多講述推送的好教程,我也是看著一步步學(xué)會的,但是這些教程的時間都是去年或者更早時期的,對引導(dǎo)新手來說不是很合適)
第一部分
首先第一步當(dāng)然是介紹一下蘋果的推送機(jī)制(APNS)咯(ps:其實每一篇教程都有),先來看一張?zhí)O果官方對其推送做出解釋的概要圖。
Provider是給你手機(jī)應(yīng)用發(fā)出推送消息的服務(wù)器,而APNS(Apple Push Notification Service)則是蘋果消息推送服務(wù)器。你本地的服務(wù)器當(dāng)需要給應(yīng)用推送一條消息的時候,先要將消息發(fā)出到蘋果推送服務(wù)器,然后再由蘋果推送服務(wù)器將消息發(fā)到安裝了該應(yīng)用的手機(jī)。
接下來再看一張解釋圖:
根據(jù)上圖的邏輯我來給大家解釋一下:
1.你的IOS應(yīng)用需要去注冊APNS消息推送功能。
2.當(dāng)蘋果APNS推送服收到來自你應(yīng)用的注冊消息就會返回一串device token給你(很重要)
3.將應(yīng)用收到的device Token傳給你本地的Push服務(wù)器。
4.當(dāng)你需要為應(yīng)用推送消息的時候,你本地的推送服務(wù)器會將消息,以及Device Token打包發(fā)送到蘋果的APNS服
5.APNS再將消息推送給目的iphone
第二部分
1.從證書頒發(fā)機(jī)構(gòu)頒發(fā)證書
打開你mac的鑰匙串訪問: 然后點擊鑰匙串訪問
隨后它會彈出一個窗口用戶電子郵件信息就填寫你蘋果開發(fā)者賬號的名稱即可(應(yīng)該是一個郵件名稱),點擊保存到磁盤的選
項,點擊繼續(xù),顯示如下:
點擊存儲,文件名為:CertificateSigningRequest.certSigningRequest 隨后將他放在一個文件夾中我們?nèi)∶鹥ush吧!
第三部分
訪問蘋果開發(fā)者網(wǎng)址:https://developer.apple.com/
選中MemberCenter選項,進(jìn)入登陸頁面,用你的蘋果開發(fā)者賬號登陸,過一會網(wǎng)頁就會自動跳轉(zhuǎn)到下圖。
點擊紅色所選部分內(nèi)容進(jìn)行下一步的操作。
選擇Certificates選項,設(shè)置證書,如圖所示先解釋一下:
Development選項的作用顧名思義就是用來作為開發(fā)使用的證書,Production選項則是用來發(fā)布產(chǎn)品使用的,名稱很陌生是不是,之前的開發(fā)者網(wǎng)頁是沒有這一選項的,可能是蘋果把他修改了,用這個名稱更加能讓人理解吧(字面上解釋就是產(chǎn)品么)。兩個選項生成證書的步驟是一樣的,現(xiàn)在我們使用開發(fā)者的選項進(jìn)行證書的制作,步驟如下:選擇Development選項:
點擊上面的加號選項,
選擇APNS選項(開發(fā)么當(dāng)然是在沙盒環(huán)境下了,模擬真實情況),然后Continue
這個AppID我們在下一部分講如何生成,現(xiàn)在我用的是已經(jīng)生成好的一個應(yīng)用ID,繼續(xù)Continue
這邊就要選擇在鑰匙串訪問環(huán)節(jié)下載下來的CertificateSigningRequest.certSigningRequest文件了,選擇并生成:
點擊下載,得到aps_development .cer,保存到push文件中去。
第四部分
新建一個AppID,選擇網(wǎng)頁上的AppIDs,然后點擊右上角的 “加號”
App的取名只要按照蘋果要求的就可以了
然后BundleID是比較重要的,在提交審核以及測試(蘋果的TestFlight)和付費環(huán)節(jié)都需要用到,也只需按照蘋果要求來寫就好了。
接下來就是對你的應(yīng)用需要使用蘋果的哪些服務(wù)進(jìn)行選擇就行了,例如廣告,游戲中心,推送,付費等等情況。
最后選擇“Submit”選項,在下一個界面中選擇“done”選項,這樣我們設(shè)置AppID的步驟我們就完成了。
第五部分:生成Provisioning Profiles
這個配置概要文件分為兩種,一種是為開發(fā)使用的,還有一種則是為發(fā)布到appStore上面。
創(chuàng)建發(fā)布版的ProvisioningProfile與開發(fā)版的流程相同,點擊Development然后點擊右上角的加號
會進(jìn)入選擇何種配置概要文件的界面:
我們現(xiàn)在時測試,所以選擇“IOS App Development”的選項,在下面的Distribution發(fā)布選項中有兩個選擇,“App Store”以及
“Ad hoc”,你可以根據(jù)下面的描述選擇你發(fā)布所需的選項。點擊Continue進(jìn)入下一步。
選擇你上一步創(chuàng)建的AppID,點擊Continue 進(jìn)行下一步
選擇你的開發(fā)者賬號,Continue進(jìn)行下一步
在這一步上選擇你的設(shè)備(你只有在這一步上勾選了你的設(shè)備,你才能在設(shè)備上用這個簽名進(jìn)行調(diào)試)。關(guān)于如何將你的設(shè)備號添加進(jìn)去也是非常簡單的,選擇左側(cè)的"Devices",然后點擊右上角的加號,在隨后出來的頁面上添加你設(shè)備的UUID(在XCode中可以查看到)以及name( 可以隨便取,自己看的懂就行)然后Register一下,照著流程走到最后一步就完成了。
好咱們繼續(xù)回到上面的Provisioning Profile配置環(huán)節(jié),當(dāng)你選好了你的設(shè)備后點擊“Continue”進(jìn)入下一頁,
輸入一個文件名(最好是起的能看懂是干嘛的,當(dāng)然也可以隨便起),點擊“Generate”進(jìn)入下一個頁面,在這個頁面中就會有一個下載按鈕讓你下載這個文件,我們把它下載下來放在Push文件夾中。
第六部分
好了,前期的準(zhǔn)備工作都已經(jīng)做完了,現(xiàn)在讓我們開始推送吧!(吼吼)
首先雙擊我們生成的 “aps_development .cer” 文件,進(jìn)入鑰匙串訪問,找到我們的專用秘鑰(根據(jù)在第二部分中從證書機(jī)構(gòu)頒發(fā)證書操作中填寫的常用名)
我在第二部分填寫的是“silicon”,由于換了一臺mac之前安裝的沒有了,之前沒有截圖,所以隨便找了個圖給大家看一下,憑大家的聰明才智應(yīng)該不難理解吧。
然后右擊導(dǎo)出,會彈出如下所示的圖。
將他存儲到push文件夾中,命名為“push.p12”,在這一步中導(dǎo)出會讓你輸入密碼并驗證,你可以自定義一個密碼,例如abc123
現(xiàn)在push文件夾中應(yīng)該有幾個文件“aps_development .cer” ,"push.p12",“CertificateSigningRequest.certSigningRequest”以及剛才下下來的配置概要文件。
接下來我們打開終端將他們生成.pem文件
1.把aps_development .cer文件生成.pcm文件,cd到push文件夾下
2.把push.p12文件生成為.pem文件
上邊輸入的密碼則是你導(dǎo)出證書所設(shè)的密碼,即abc123.接著還會讓你輸入.pem文件的密碼,還是使用abc123好了,防止混淆。這樣我們在push文件夾中就又得到了兩個文件,PushChatCert.pem和PushChatKey.pem。
3.把PushChatCert.pem和PushChatKey.pem合并為一個pem文件,
在push文件夾中又多了一個ck.pem文件,以上我們把需要使用的文件都準(zhǔn)備好了
接下來就要測試一下啦,是不是很激動~
為了測試證書工作的狀況,我們可以使用“telnet gateway.sandbox.push.apple.com 2195”來檢測一下,如果顯示下圖則表示成功了。
然后,我們使用我們生成的證書和私鑰來設(shè)置一個安全的鏈接去鏈接蘋果服務(wù)器
在終端輸入如下命令:openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert PushChatCert.pem -key PushChatKey.pem
需要輸入密碼(abc123 我們剛才所設(shè)置的)。
然后他會返回一系列的數(shù)據(jù),這里我就粘貼一部分啦:
CONNECTED(00000003) depth=1 /C=US/O=Entrust, Inc./OU=www.entrust.NET/rpa is incorporated by reference/OU=(c) 2009 Entrust, Inc./CN=Entrust Certification Authority - L1C verify error:num=20:unable to get local issuer certificate verify return:0 --- Certificate chain 。。。。。(省略) 。。。。。(省略) 。。。。。(省略) Start Time: 1416389389 Timeout : 300 (sec) Verify return code: 0 (ok) ---
測試就到這里啦。。。
第七部分
1.建立推送項目
// // AppDelegate.m // TestPushNotifiy // // Created by silicon on 14-10-30. // Copyright (c) 2014年 silicon. All rights reserved. // #import "AppDelegate.h" @implementation AppDelegate @synthesize mainView = _mainView; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) { //IOS8 //創(chuàng)建UIUserNotificationSettings,并設(shè)置消息的顯示類類型 UIUserNotificationSettings *notiSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIRemoteNotificationTypeSound) categories:nil]; [application registerUserNotificationSettings:notiSettings]; } else{ // ios7 [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound |UIRemoteNotificationTypeAlert)]; } self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; self.mainView = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil]; self.window.rootViewController = self.mainView; return YES; } - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)pToken{ NSLog(@"---Token--%@", pToken); } - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{ NSLog(@"userInfo == %@",userInfo); NSString *message = [[userInfo objectForKey:@"aps"]objectForKey:@"alert"]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil nil]; [alert show]; } - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{ NSLog(@"Regist fail%@",error); } - (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. } - (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - (void)applicationWillEnterForeground:(UIApplication *)application { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } - (void)applicationDidBecomeActive:(UIApplication *)application { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } @end
在appdelegate.m中加入以上代碼,
if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) { //IOS8 //創(chuàng)建UIUserNotificationSettings,并設(shè)置消息的顯示類類型 UIUserNotificationSettings *notiSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIRemoteNotificationTypeSound) categories:nil]; [application registerUserNotificationSettings:notiSettings]; } else{ // ios7 [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound |UIRemoteNotificationTypeAlert)]; }
由于ios8的推送跟ios7及以下的不一樣,所以需要加判斷來注冊消息推送。
函數(shù):
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)pToken{ NSLog(@"---Token--%@", pToken); }
會接收來自蘋果服務(wù)器給你返回的deviceToken,然后你需要將它添加到你本地的推送服務(wù)器上。(很重要,決定你的設(shè)備能不能接收到推送消息)。
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{ NSLog(@"userInfo == %@",userInfo); NSString *message = [[userInfo objectForKey:@"aps"]objectForKey:@"alert"]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil nil]; [alert show]; }
這個函數(shù)則是當(dāng)設(shè)備接收到來自蘋果推送服務(wù)器的消息時觸發(fā)的,用來顯示推送消息。
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{ NSLog(@"Regist fail%@",error); }
當(dāng)注冊失敗時,觸發(fā)此函數(shù)。
2.PHP服務(wù)端
將simplepush.php這個推送腳本也放在push文件夾中
<?php // ??????????deviceToken??????????????? $deviceToken = 'c95f661371b085e2517b4c12cc76293522775e5fd9bb1dea17dd80fe85583b41'; // Put your private key's passphrase here: $passphrase = 'abc123'; // Put your alert message here: $message = 'My first push test!'; //////////////////////////////////////////////////////////////////////////////// $ctx = stream_context_create(); stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem'); stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); // Open a connection to the APNS server //?????????? //$fp = stream_socket_client(?ssl://gateway.push.apple.com:2195?, $err, $errstr, 60, //STREAM_CLIENT_CONNECT, $ctx); //?????????????appstore?????? $fp = stream_socket_client( 'ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); if (!$fp) exit("Failed to connect: $err $errstr" . PHP_EOL); echo 'Connected to APNS' . PHP_EOL; // Create the payload body $body['aps'] = array( 'alert' => $message, 'sound' => 'default' ); // Encode the payload as JSON $payload = json_encode($body); // Build the binary notification $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload; // Send it to the server $result = fwrite($fp, $msg, strlen($msg)); if (!$result) echo 'Message not delivered' . PHP_EOL; else echo 'Message successfully delivered' . PHP_EOL; // Close the connection to the server fclose($fp); ?>
deviceToken填寫你接收到的token,passPhrase則填寫你的ck.pem設(shè)置的密碼。
此刻就是見證奇跡的時候了,使用終端進(jìn)入到push文件夾,在終端輸入 php simplepush.php
若顯示以上提示則表示推送成功了。附上一張成功圖。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
實例講解iOS音樂播放器DOUAudioStreamer用法
本篇文章給大家通過實例講解了iOS音樂播放器DOUAudioStreamer用法以及分享了實例代碼,一起學(xué)習(xí)參考下吧。2017-12-12Objective-C中block循環(huán)引用問題詳解
這篇文章主要給大家介紹了關(guān)于Objective-C中block循環(huán)引用問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Objective-C具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06