標籤:
結合網上各個資料,再簡單整理的一份。
一、APNS推送說明
1.你的IOS應用需要去註冊APNS訊息推送功能。
2.當蘋果APNS推送服收到來自你應用的註冊訊息就會返回一串device token給你(很重要)
3.將應用收到的device Token傳給你本地的Push伺服器。
4.當你需要為應用推送訊息的時候,你本地的推送伺服器會將訊息,以及Device Token打包發送到蘋果的APNS服
5.APNS再將訊息推送給目的iphone
二、推送的準備工作
推送準備的主要就是1、推送認證 2、匯出的密鑰檔案 3、下載的描述檔案
1、APP ID建立
先建立APP ID,名字為PustTest, Bundle ID 為com.公司名.testpush
直接點擊下一步,完成建立。
2、產生認證
認證選擇Push推送認證,下一步選擇剛剛建立的APP ID
添加從鑰匙串匯出的請求檔案
下一步->下載推送認證。
3、描述檔案建立下載
描述檔案選擇Development, 如果這裡選擇選擇下面的生產,則項目可能會閃退。下一步也是選擇剛剛建立的APP ID,然後選擇裝置一步步,最後下載描述檔案。
4、密鑰匯出
雙擊下載的認證檔案,會在鑰匙串登陸-我的認證裡產生一個新的認證,右擊匯出認證,名字設定為 push.p12
匯出的密碼要記得,推送要用,我設定位aaa123;
5,整理
將密鑰,認證,和描述檔案,放到案頭的一個檔案夾裡,檔案夾名字設定為 "push";
6,檔案轉換pem
推送該準備的檔案都準備好了,然後開始做推送,將密鑰和認證轉換成pem檔案。開啟電腦裡的終端,產生pem檔案需要用到終端命令。
首先用終端命令開啟案頭的push檔案夾“ cd ~/desktop/push ” 斷行符號;
把認證檔案產生pem檔案命令“ openssl x509 -in aps_development.cer -inform der -out PushCert.pem ”
把密鑰p12檔案產生pem檔案命令“ openssl pkcs12 -nocerts -out PushKey.pem -in push.p12 ”,斷行符號後需要輸入三次密碼,也就是匯出時候的密碼aaa123
然後把兩個pem檔案合并成一個pem檔案,命令:“ cat PushCert.pem PushKey.pem > ck.pem ”,這樣案頭的push檔案夾就有6個檔案了
測試推送認證能否正常運行,輸入終端命令“ telnet gateway.sandbox.push.apple.com 2195 ”,如果運行如下,則一切正常。
串連蘋果伺服器APNS,終端命令“ openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert PushCert.pem -key PushKey.pem ”
斷行符號後輸入密碼 aaa123,返回一堆資料,最後如下。表示沒問題。
7、項目配置
建立項目,Bundle ID 設定和APP ID裡一樣testpush,Code Signing選擇描述檔案和開發帳號,
AppDelegate.m裡設定註冊,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) { //IOS8 //建立UIUserNotificationSettings,並設定訊息的顯示類類型 UIUserNotificationSettings *notiSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIRemoteNotificationTypeSound) categories:nil]; [application registerUserNotificationSettings:notiSettings]; [application registerForRemoteNotifications]; } else{ // ios7 [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound |UIRemoteNotificationTypeAlert)]; } self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; // self.mainView = [[ViewController alloc] initWithNibName:@"MainViewController" bundle:nil]; self.window.rootViewController = [ViewController alloc]; return YES;}
其他回調方法
// 處理推送訊息- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{ NSLog(@"userinfo:%@",userInfo); NSLog(@"收到推送訊息:%@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]);}//註冊失敗回調- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *) error{ NSLog(@"Registfail%@",error);}//返回裝置表示device token-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{ NSLog(@"裝置標示%@",deviceToken);//這裡的Token就是我們裝置要告訴服務端的Token碼}
運行後得到標示device token “f465312ab96e2dfd35829d1c6f898a019b01141615745f5b41a4db4a312870a0”
8、PHP本機伺服器
測試通知效果需要一個php檔案,裡面填入擷取的標示,密鑰匯出的密碼,和合并檔案 ck.pem
<?php $deviceToken = ‘f465312ab96e2dfd35829d1c6f898a019b01141615745f5b41a4db4a312870a0‘;//擷取請求參數中的deviceToken $passphrase = ‘aaa123‘; //mimi $message = ‘這是一條推送訊息‘;//擷取請求參數中的想要推送的資訊///列印 查看 echo $deviceToken; echo $message; // Put your alert message here: 設定你申請好並轉換好的認證和密碼 $ctx = stream_context_create(); stream_context_set_option($ctx, ‘ssl‘, ‘local_cert‘, ‘ck.pem‘);//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); $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); ?>
php檔案下載:http://pan.baidu.com/s/1bi3Lym
9、推送命令
將配置好的php檔案同樣放到案頭的push檔案夾裡。運行終端命令,給標示手機推送一個通知 “這是一條推送訊息”
先用終端命令ck開啟push檔案夾,再調用運行push.php檔案,運行命令:“ php push.php “
手機端收到通知訊息。
註:本文主要內容都是參考網上其他部落格內容,稍微整理。
iOS 通知推送APNS