歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> iOS 實現推送消息

iOS 實現推送消息

日期:2017/3/1 9:44:20   编辑:Linux編程

iOS消息推送的工作機制可以簡單的用下圖來概括:


Provider是指某個iPhone軟件的Push服務器,APNS是Apple Push Notification Service的縮寫,是蘋果的服務器。

iOS 在 Flash 中集成消息推送服務 http://www.linuxidc.com/Linux/2014-05/101874.htm

上圖可以分為三個階段:

第一階段:應用程序把要發送的消息、目的iPhone的標識打包,發給APNS。

第二階段:APNS在自身的已注冊Push服務的iPhone列表中,查找有相應標識的iPhone,並把消息發送到iPhone。

第三階段:iPhone把發來的消息傳遞給相應的應用程序,並且按照設定彈出Push通知。

從上圖我們可以看到:

1、應用程序注冊消息推送。

2、iOS從APNS Server獲取device token,應用程序接收device token。

3、應用程序將device token發送給PUSH服務端程序。

4、服務端程序向APNS服務發送消息。

5、APNS服務將消息發送給iPhone應用程序。

步驟一、

創建需要的證書 & 使用PHP編寫服務器端推送代碼

1. 登錄 iPhone Developer Connection Portal(http://developer.apple.com/iphone/manage/overview/index.action ) 然後點擊 App IDs
2. 創建一個 Apple ID ,如: com.tadpole.TestAPNs 注意:通配符 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.tadpole.TestAPNs”。保存為 cert.p12 文件。
6. 通過終端命令將這個cert.p12文件轉換為PEM格式,打開終端,cd 進入證書所在目錄,執行如下命令:

$ 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的代碼如下:

<?php

// 這裡是我們上面得到的deviceToken,直接復制過來(記得去掉空格)

$deviceToken = '740f4707bebcf74f 9b7c25d4 8e3358945f6aa01da5ddb387462c7eaf 61bb78ad';

// Put your private key's passphrase here:

$passphrase = 'abc123456';

// Put your alert message here:

$message = 'My first push test!';

////////////////////////////////////////////////////////////////////////////////

$ctx = stream_context_create();

stream_context_set_option($ctx, 'ssl', 'local_cert', 'apple_push_notification_production.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);

?>

更多詳情見請繼續閱讀下一頁的精彩內容: http://www.linuxidc.com/Linux/2014-05/101876p2.htm

Copyright © Linux教程網 All Rights Reserved