PHP - APNs 消息已交付但在 iOS 设备上未收到
Posted
技术标签:
【中文标题】PHP - APNs 消息已交付但在 iOS 设备上未收到【英文标题】:PHP - APNs messages are delivered but not received on iOS device 【发布时间】:2015-12-25 11:15:08 【问题描述】:我正在使用以下代码向 ios 设备发送推送通知。我在 php 端成功发送消息,但在 iOS 设备中没有收到推送。
我已经检查了替换 .pem 文件,还测试了 sendbox 和生产。它不能同时工作。请提供调试此问题的方法。
function iosTest($tToken)
// Provide the Host Information.
//$tHost = 'gateway.sandbox.push.apple.com';
$tHost = 'gateway.push.apple.com';
$tPort = 2195;
//echo "hi";
// Provide the Certificate and Key Data.
$tCert = $_SERVER['DOCUMENT_ROOT'].'/N****d.pem';
// Provide the Private Key Passphrase (alternatively you can keep this secrete
// and enter the key manually on the terminal -> remove relevant line from code).
// Replace XXXXX with your Passphrase
$tPassphrase = 'harsh';
// Provide the Device Identifier (Ensure that the Identifier does not have spaces in it).
// The message that is to appear on the dialog.
$tAlert = 'Testing..';
// The Badge Number for the Application Icon (integer >=0).
$tBadge = 1;
// Audible Notification Option.
$tSound = 'default';
// The content that is returned by the LiveCode "pushNotificationReceived" message.
$tPayload = 'APNS Message Handled by LiveCode';
// Create the message content that is to be sent to the device.
$tBody['aps'] = array (
'alert' => $tAlert,
'badge' => $tBadge,
'sound' => $tSound,
);
//$tBody ['payload'] = $tPayload;
// Encode the body to JSON.
$tBody = json_encode ($tBody);
echo $tBody;
// Create the Socket Stream.
$tContext = stream_context_create ();
stream_context_set_option ($tContext, 'ssl', 'local_cert', $tCert);
// Remove this line if you would like to enter the Private Key Passphrase manually.
stream_context_set_option ($tContext, 'ssl', 'passphrase', $tPassphrase);
// Open the Connection to the APNS Server.
$tSocket = stream_socket_client ('ssl://'.$tHost.':'.$tPort, $error, $errstr, 30, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $tContext);
// Check if we were able to open a socket.
if (!$tSocket)
exit ("APNS Connection Failed: $error $errstr" . PHP_EOL);
// Build the Binary Notification.
$tMsg = chr (0) . chr (0) . chr (32) . pack ('H*', $tToken) . pack ('n', strlen ($tBody)) . $tBody;
// Send the Notification to the Server.
$tResult = fwrite ($tSocket, $tMsg, strlen ($tMsg));
if ($tResult)
echo 'Delivered Message to APNS' . PHP_EOL;
else
echo 'Could not Deliver Message to APNS' . PHP_EOL;
// Close the Connection to the Server.
fclose ($tSocket);
//send_feedback_request();
提前致谢。
【问题讨论】:
【参考方案1】:请使用下面的代码,因为它在我这边工作
<?php
// Put your device token here (without spaces):
$deviceToken = '17b2f31afd5c9736ad48ffca8d1936046431c6fc1f3f3ac661bcbfbf97ca60cc';
// Put your private key's passphrase here:
$passphrase = '';
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'HireRightNew.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' => 'Breaking News',
'sound' => 'default',
'link_url' => $url,
'category' => 'NEWS_CATEGORY',
);
// 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);
?>
【讨论】:
以上是关于PHP - APNs 消息已交付但在 iOS 设备上未收到的主要内容,如果未能解决你的问题,请参考以下文章