php实现IOS消息推送的代码


PHP #ios #消息推送2012-06-08 11:08

实现的代码如下:

<?php
// Comment these lines in production mode
ini_set('display_errors','on');
error_reporting(E_ALL);


// Apns config

// true - use apns in production mode
// false - use apns in dev mode
define("PRODUCTION_MODE",false);

$serverId = 1;
$serverName = 'my-server-domain.com';

if(PRODUCTION_MODE) {
$apnsHost = 'gateway.sandbox.push.apple.com';
} else {
$apnsHost = 'gateway.push.apple.com';
}

$apnsPort = 2195;
if(PRODUCTION_MODE) {
// Use a development push certificate 
$apnsCert = $_SERVER['DOCUMENT_ROOT'].'/apns/apns-dominos-development.pem';
} else {
// Use a production push certificate 
$apnsCert = $_SERVER['DOCUMENT_ROOT'].'/apns/apns-dominos-production.pem';
}


// --- Sending push notification ---

// Insert your device token here 
$device_token = "<dc6127d8 dc6127d8 dc6127d8 dc6127d8 dc6127d8 dc6127d8 dc6127d8    dc6127d8>"; // Some Device Token


// Notification content

$payload = array();

//Basic message
$payload['aps'] = array(
'alert' => 'testing 1,2,3..', 
'badge' => 1, 
'sound' => 'default',
);
$payload['server'] = array(
'serverId' => $serverId,
 'name' => $serverName
);
// Add some custom data to notification
$payload['data'] = array(
'foo' => "bar"
);
$payload = json_encode($payload);

$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
stream_context_set_option($streamContext, 'ssl', 'passphrase', "");


$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error,      $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);


$deviceToken = str_replace(" ","",substr($device_token,1,-1));
echo $deviceToken;
$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '',      $deviceToken)) . chr(0) . chr(mb_strlen($payload)) . $payload;
fwrite($apns, $apnsMessage);


//socket_close($apns);
fclose($apns);

?>


相关文章

粤ICP备11097351号-1