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); ?>
相关文章
- PHP有干扰线噪点的验证码类 2012/06/06
- PHP短链接算法 2012/06/04
- php用curl smtp发送邮件 2012/06/04
- php隐藏图片真实地址的代码 2012/06/04
- 在windows下定时执行PHP的方法 2012/06/04
- PHP实现京东API的授权HASH算法 2012/06/03
- PHP正则验证邮箱的函数 2012/06/03
- PHP清空新浪微博的代码 2012/06/03
- PHP去除BOM头的代码 2012/06/02
- PHP把xml转换成数组的方法 2012/06/02