iOS的推送能夠用下圖簡單的歸納:php
(1). 登陸 iPhone Developer Connection Portal (連接) 而後點擊 App IDs
(2). 建立一個 Apple ID ,如:com.chenjungang.apnstest 注意:通配符 ID 不能用於推送通知服務。
(3). 點擊 Apple ID 旁的「Configure」,根據「嚮導」 的步驟生成一個簽名上傳,而後下載生成的許可證。
(4). 雙擊 .cer 文件將你的 aps_development.cer 導入 Keychain 中。
(5). 在 Mac 上打開「鑰匙串訪問」,而後在「登陸」中選擇 "密鑰"分類,找到咱們建立的證書,而後右擊「Apple Development IOS Push Services: com.tadpole.TestAPNs」 > 導出 「Apple Development IOS Push Services: com.chenjungang.apnstest」。保存爲 cert.p12 文件。
(6). 經過終端命令將這個 cert.p12 文件轉換爲 PEM 格式,打開終端, cd 進入證書所在目錄,執行以下命令:(Java 直接給 .p12 文件,Php 給 pem 文件)node
openssl pkcs12 -in cert.p12 -out apple_push_notification_production.pem -nodes -clcerts
執行完上面命令會在當前目錄下生成一個名爲apple_push_notification_production.pem文件,這個文件就是咱們須要獲得php鏈接APNS 的文件,將apple_push_notification_production.pem和push.php放入同一目錄上傳到服務器,push.php的代碼以下:json
1 <?php 2 $deviceToken = 'ad853c20 3a9b874e 00d0cde2 44a73752 999bb250 9fa36129 0525d5d0 a6ba3e4c'; 3 4 // Put your private key's passphrase here: 5 $passphrase = '123456'; 6 7 // Put your alert message here: 8 $message = 'This is some push message!'; 9 $ctx = stream_context_create(); 10 stream_context_set_option($ctx, 'ssl', 'local_cert', 'apple_push_notification_production.pem'); 11 stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); 12 13 // Open a connection to the APNS server 14 //這個爲正式的發佈地址 15 //$fp = stream_socket_client(「ssl://gateway.push.apple.com:2195「, $err, $errstr, 60, //STREAM_CLIENT_CONNECT, $ctx); 16 //這個是沙盒測試地址,發佈到appstore後記得修改哦 17 $fp = stream_socket_client( 18 'ssl://gateway.sandbox.push.apple.com:2195', $err, 19 $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); 20 if (!$fp) 21 exit("Failed to connect: $err $errstr" . PHP_EOL); 22 echo 'Connected to APNS' . PHP_EOL; 23 24 // Create the payload body 25 $body['aps'] = array( 26 'alert' => $message, 27 'sound' => 'default' 28 ); 29 30 // Encode the payload as JSON 31 $payload = json_encode($body); 32 33 // Build the binary notification 34 $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload; 35 36 // Send it to the server 37 $result = fwrite($fp, $msg, strlen($msg)); 38 39 if (!$result) 40 echo 'Message not delivered' . PHP_EOL; 41 else 42 echo 'Message successfully delivered' . PHP_EOL; 43 44 // Close the connection to the server 45 fclose($fp); 46 47 ?>
1 #define IS_IOS8_AND_UP ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) 2 3 -(void)settingPushMessage:(UIApplication*)application 4 { 5 6 if (IS_IOS8_AND_UP) { 7 [application registerForRemoteNotifications]; 8 UIUserNotificationType types = UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert; 9 [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:types categories:nil]]; 10 }else{ 11 //消息推送支持的類型 12 UIRemoteNotificationType types = 13 (UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound); 14 //註冊消息推送 15 [[UIApplication sharedApplication]registerForRemoteNotificationTypes:types]; 16 } 17 18 } 19 20 - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 21 { 22 //獲取token 23 NSString* token = [NSString stringWithFormat:@"%@",deviceToken]; 24 token = [token stringByReplacingOccurrencesOfString:@"<" withString:@""]; 25 token = [token stringByReplacingOccurrencesOfString:@">" withString:@""]; 26 [[NSUserDefaults standardUserDefaults] setObject:token forKey:@"APVNToken"]; 27 28 NSLog(@"token====%@",token); 29 } 30 - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error 31 { 32 //這裏是獲取token失敗 33 NSString *error_str = [NSString stringWithFormat: @"%@", error]; 34 NSLog(@"Failed to get token, error:%@", error_str.description); 35 } 36 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 37 { 38 //在此處理接收到的消息。 39 NSLog(@"Receive remote notification : %@",userInfo); 40 }
1 { 2 aps = { 3 alert = "hello, everyone"; 4 badge = 2; 5 sound = default; 6 }; 7 }
1 { 2 action = { 3 type = 4; 4 }; 5 aps = { 6 alert = "hello, everyone"; 7 badge = 4; 8 }; 9 }