欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

ios消息推送

程序员文章站 2022-05-21 17:27:47
...
<?php
/*
include this content
define('DEV_APP_TOKEN','test token');
define('PRODUCT_APP_TOKEN','product token');
define('DEV_CERT_PASSPHRASE','DEV_CERT_PASSPHRASE');
define('PRODUCT_CERT_PASSPHRASE','PRODUCT_CERT_PASSPHRASE');
define('DEV_SSL_URL','ssl://gateway.sandbox.push.apple.com:2195');
define('PRODUCT_SSL_URL','ssl://gateway.push.apple.com:2195');
define('API_KEY','you key');

*/

include 'config.php';


$token = empty($_POST['dev_token']) ? '' : $_POST['dev_token']; //要推送的设备序列号
$message = empty($_POST['message']) ? '' : $_POST['message'];//内容
$type = empty($_POST['type']) ? 0 : intval($_POST['type']);//消息类型
$url = empty($_POST['url']) ? '' : $_POST['url'];//url
$badge = empty($_POST['badge']) ? 1 : intval($_POST['badge']); //未读消息数


if($token == '' || empty($token)){
	echo '设备token不能为空';
	die;
}
// Put your device token here (without spaces):
$deviceToken = $token;

// Put your private key's passphrase here:
$passphrase = PRODUCT_CERT_PASSPHRASE;



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

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

// Open a connection to the APNS server
$fp = stream_socket_client(
    PRODUCT_SSL_URL, $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 = array('aps' => array('alert' => $message,
						     'sound' => 'default',
						     'type' => $type,
						     'url' => $url,
                             'badge' => $badge
						      )
    ); 
    

// 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);
    
?>
相关标签: ios消息推送