将推送通知发送到许多设备
这是我第一次发布堆栈溢出,所以如果这个问题非常明显,我很抱歉。
我正在为我的iOS应用程序设置推送通知,只用我自己的设备就可以成功完成。 我很快就会在很多设备上进行测试,但是我想知道我是否有正确的想法。
我会将所有设备标记保留在数据库中,并且在发送推送通知时,我只需遍历数据库中所有行(包含标记)并通过后续脚本发送它们,每次都替换设备标记并且每次都保留我自己的私钥。 它是否正确?
提前致谢!
// Put your device token here (without spaces):
$deviceToken = $device; // this will be run through a loop that
changes this variable for every new device
$passphrase = 'XXXXXXXXX'; //This is mine and remains always
the same despite which device... correct?
// Put your alert message here:
$message = 'Testing first 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;
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default',
'badge' => 1
);
// 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);
链接地址: http://www.djcxy.com/p/57423.html