[英]Push Notification not working in Iphone
我用过这个php代码
$query = "SELECT * FROM iphone_register";
$result_iphone = mysql_query($query);
$fetres = mysql_num_rows($result_iphone);
while ($rows = mysql_fetch_object($result_iphone)) {
$deviceToken = $rows->device_id;
$passphrase = '******';
$message = 'New Push Notification!';
$badge = 1;
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$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);
}
$body['aps'] = array(
'alert' => 'new notification is arrived',
'body'=> $message,
'badge' => $badge,
'sound' => 'newMessage.wav'
);
$payload = json_encode($body);
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
$result = fwrite($fp, $msg, strlen($msg));
}
if (!$result) {
echo 'Error, notification not sent' . PHP_EOL;
} else {
echo 'notification sent!' . PHP_EOL;
}
fclose($fp);
当我从服务器向设备发送推送通知时,出现以下错误。
我尝试更改ck.pem
文件以获取解决方案。
但它不起作用。 因此,我认为ck.pem(Certificate)
文件没有问题。
警告:stream_socket_client():无法连接到第55行的/home/sunstate/public_html/sunstate_api/gcm_message.php中的ssl://gateway.sandbox.push.apple.com:2195(连接被拒绝):无法连接:111拒绝连接
通过对设备令牌进行硬编码,尝试像下面的代码所示。
<?php
$deviceToken = 'yourdevicetoken'; // masked for security reason
// Passphrase for the private key (ck.pem file)
$pass = 'xxxxx';
// Get the parameters from http get or from command line
$message = $_GET['message'] or $message = $argv[1] or $message = 'Message received from BRL server';
$badge = (int)$_GET['badge'] or $badge = (int)$argv[2];
$sound = $_GET['sound'] or $sound = $argv[3];
//$deviceToken = $_GET['deviceToken'];
// Construct the notification payload
$body = array();
$body['aps'] = array('alert' => $message);
if ($badge)
$body['aps']['badge'] = $badge;
if ($sound)
$body['aps']['sound'] = $sound;
/* End of Configurable Items */
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck_dev.pem');
// assume the private key passphase was removed.
stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr,60,STREAM_CLIENT_CONNECT, $ctx);
if (!$fp) {
print "Failed to connect $err $errstr\n";
return;
}
else {
print "Connection OK\n";
}
$payload = json_encode($body);
$msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;
print "sending message :" . $payload . "\n";
fwrite($fp, $msg);
fclose($fp);
?>
将此代码复制到.php文件中,然后将其上传到您的服务器,并通过添加实际的设备令牌从浏览器运行此文件。
另外,如果您正在使用任何托管服务器,则需要在该服务器上安装SSL,并要求它们打开与提供商所阻止的通知相关的端口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.