在过去的3天里,我一直在尝试使用我的php代码和apns。
到目前为止,我能够发送通知(我只有一台设备,因此我不知道这是否适用于多台设备)。
当我尝试测试为2个设备发送一个通知时,第一个是设备令牌无效(由我发明),第二个是我的设备令牌,我无法将通知发送到我的设备...从我的读取,当通知不可读或错误时,apns关闭连接并发送长度为6个字节的错误,其中第二个字节(status_code)是错误类型。如果没有发生错误,我仍然无法理解apns是否发送status_code 0(没有遇到错误)或者它是否发送任何错误。
到目前为止,我无法找到这个status_code,尽管在互联网上发现了以下几个代码。
CODE
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', '.\certif\ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Create the payload body
//file_put_contents('logs/debug_not.log', 'body'. "\n", FILE_APPEND);
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);
// Encode the payload as JSON
//file_put_contents('logs/debug_not.log', 'json body'. "\n", FILE_APPEND);
$payload = json_encode($body);
$open = true;
foreach ($devices as $device) {
//file_put_contents('logs/debug_not.log', 'inicio ciclo'. "\n", FILE_APPEND);
if($open == true){
//file_put_contents('logs/debug_not.log', 'abrir ligacao'. "\n", FILE_APPEND);
// 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){
//file_put_contents('logs/debug_not.log', 'erro abrir ligacao'. "\n", FILE_APPEND);
throw new \Exception;
}
}
// Build the binary notification
//file_put_contents('logs/debug_not.log', 'criar payload'. "\n", FILE_APPEND);
$msg = chr(0) . pack('n', 32) . pack('H*', str_replace(' ', '', $device['token'])) . pack('n', strlen($payload)) . $payload;
// Send it to the server
//file_put_contents('logs/debug_not.log', 'enviar payload'. "\n", FILE_APPEND);
$result = fwrite($fp, $msg, strlen($msg));
stream_set_blocking ($fp, 0);
$errorResponse = @fread($apns, 6);
//if i var_dump($errorResponse) it gives me null/false all the time, no matter what
//this is my workaround for invalid device tokens-- its not working or at least is not send the valid tokens
if(!$result || !$fp)
{
//file_put_contents('logs/debug_not.log', 'erro na not '. chr($data["status_code"]). "\n", FILE_APPEND);
fclose($fp);
$open = true;
} else{
//file_put_contents('logs/debug_not.log', 'suc na not'. "\n", FILE_APPEND);
$open = false;
}
//file_put_contents('logs/debug_not.log', 'fim ciclo'. "\n", FILE_APPEND);
}
// Close the connection to the server
if($fp){
//file_put_contents('logs/debug_not.log', 'fechar connection'. "\n", FILE_APPEND);
fclose($fp);
}
我的连接上的fread始终为null / false,即使我收到通知或我正在尝试使用错误的令牌。
我在错误后连接的逻辑似乎无效。
可以请别人帮帮我???我不打算使用第三类或代码,希望有一个简单的基本工作用于设备令牌数组,即使有一些apns给出错误。谢谢大家。
答案 0 :(得分:6)
您已将联系设为$fp = stream_socket_client ......
并且你正在尝试$errorResponse = @fread($apns, 6);
,它永远不会给你任何输出,因为没有定义$ apns。
相反,你应该这样做:
$errorResponse = @fread($fp, 6);
并且您的代码可以正常运行
答案 1 :(得分:1)
如果您收到错误(最有可能是因为您的应用程序的Apple服务器上不存在您发明的设备令牌),则会在关闭连接之前返回错误消息。如果消息成功,则没有响应,但连接仍保持打开状态。
以下是处理此问题的方法:
跟踪您上次成功的讯息。您可以通过向自己的设备发送消息来跟踪一系列消息,以防止任何误报。
您可能想查看EasyAPNS的源代码,这是一个开源的PHP实现。此外,您可以阅读Apple文档中的详细信息:Provider Communication with Apple Push Notification Service