当我使用推送通知时,我有一个问题,它在我使用开发时有效,但是当我想将它用于实际时,它将无效。
ck用于生产。
当我使用它时,结果是:Message successfully delivered
但我的手机没有收到消息
<?php
pushNotification('wow2','DeviceToken');
function pushNotification($theMessage, $theDeviceToken)
{
// Put your device token here (without spaces):
$deviceToken = $theDeviceToken;
// Put your private key's passphrase here:
$passphrase = 'Code';
// Put your alert message here:
$message = $theMessage;
////////////////////////////////////////////////////////////////////////////////
$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.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
//exit("Failed to connect: $err $errstr" . PHP_EOL);
exit("" . PHP_EOL);
//echo 'Connected to APNS\n' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);
// 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;
fclose($fp);
}
?>
有人能看出什么问题吗? :)
答案 0 :(得分:1)
APNS
- 您的设备令牌不匹配
- 你的.pem不是有效的,原因是.p12和.cer文件有时会被破坏,所以删除.p12文件和.cer并重新创建一次。
- 不是一个值得回答的答案,但偶然会发生 - 检查您的设备通知是否为
醇>ON/OFF
答案 1 :(得分:0)
我认为这是您的设备令牌的问题。如果服务器中的设备令牌无效,则不会收到通知。从服务器删除无效设备令牌后,它就会起作用。
同样的事情发生在我身上,花了很长时间才找到解决方案。
答案 2 :(得分:0)
尝试使用设备移动令牌的硬编码设备令牌。
之后尝试使用变量或echo设备令牌。
//将您的设备令牌放在这里(不含空格):
$deviceToken = '0f744707bebcf74f9b7c25d48e3358945f6aa01da5dd43487462c7eaf61bbad78';