iT邦幫忙

DAY 30
2

APP 開發好好玩系列 第 27

iOS 推播伺服器實作方式

iOS 推播伺服器實作方式
在申請完憑證之後,只要把憑證放到伺服器之上,就可以利用伺服器來產生 json 檔,然後利用 php 的 open 方法,開啓遠端的網址,就可以把推播訊息送到 Apple 的 APNS 然後就可以進行推播的動作了.
程式碼如下

<?php
$payload['aps'] = array('alert'=>'', 'badge'=>0,'sound'=>'default');
$payload['aps']['alert'] = $_POST['alert'];
$output = json_encode($payload);

$apnsHost = 'ssl://gateway.sandbox.push.apple.com:2195';

$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', '0930.pem');

$apns = stream_socket_client($apnsHost, $error, $errorString, 60, STREAM_CLIENT_
CONNECT, $streamContext);
$token = 'xxxxxxa2109e852237b761695d05a1333c1f7a9110d1d5ce43e8aa2a28xxxxxx';
// $token = $_POST['token'];
$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', $token) . chr(0) . chr(str
len($output)) . $output;
$result = fwrite($apns, $apnsMessage);
echo 'Message sent: The response is:' . $result;
fclose($apns);

?>

上一篇
手機 APP 推播方案
系列文
APP 開發好好玩27
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

我要留言

立即登入留言