php写的可以在本机发送iOS push程序

用于客户端,本机发push消息,把下面这段代码拷贝到文件以.php后缀名,和证书放到同一目录;ck.pem需要自己生成。具体步骤详见这里



<?php

// Put your device token here (without spaces):
$deviceToken =    'f5c57414ad2c3da9da2ba37aad811bbd98c7ec7c7541934bc8a33d5f9420ac65';//ios6的touch


// Put your private key's passphrase here:
$passphrase = '123456';//证书的密码

// Put your alert message here:
$message = 'My first push notification!';

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

$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(
	'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;

$alert = array(
				'body'=>’push测试2’,
				'action-loc-key'=>'ok');
// Create the payload body
$pushInfo['aps'] = array(
	'alert' => $alert,
	'sound' => '1',
	// 'content-available' => 1//,//for 静默下载。
	'badge' => 1
	);
$pushInfo['usrdefined'] = array('ptype'=>'6',
								'pushid'=>'4865',
								//自定义字段
								'appleid'=>’123456’
);

// Encode the payload as JSON
$payload = json_encode($pushInfo);

// 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);
● 填写token的时候,把空格去掉。
● cd到文件的目录,键入命令 php xxx.php,如果收到此消息说明发送成功
Connected to APNS
Message successfully delivered

你可能感兴趣的:(push,本地发送)