推送通知在 Iphone 中不起作用
Posted
技术标签:
【中文标题】推送通知在 Iphone 中不起作用【英文标题】:Push Notification not working in Iphone 【发布时间】:2015-04-18 13:09:58 【问题描述】:我用过这个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():无法连接到 ssl://gateway.sandbox.push.apple.com:2195(连接被拒绝)在 /home/sunstate/public_html/sunstate_api/gcm_message.php 在第 55 行 连接失败:111 连接被拒绝
【问题讨论】:
您好,您是在测试开发还是发布? 另见“verify error:num=20” when connecting to gateway.sandbox.push.apple.com。您应该确保三件事:(1)TLS 1.0 或更高版本; (2) 服务器名称指示; (3) Entrust.net 证书颁发机构 (2048) 根。 【参考方案1】:通过硬编码您的设备令牌尝试像下面的代码。
<?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,并要求他们打开与被提供商阻止的通知相关的端口。
【讨论】:
以上是关于推送通知在 Iphone 中不起作用的主要内容,如果未能解决你的问题,请参考以下文章