APNS不发送响应也不发送推送通知

时间:2012-11-02 21:01:43

标签: php stream push apple-push-notifications

Apple's docs说:

  

“如果您发送通知并且APN发现通知格式错误或无法识别,则会在断开连接之前返回错误响应数据包。(如果没有错误,则APN不会返回任何内容。)图5-3描述错误响应包的格式。“

这让我相信APNS不会发回信息的唯一原因是我发送的内容是否正确。然而,当我试图为他们的响应而fread时,我得到一个0长度的字符串,当解压缩变为null时,我认为这意味着什么都没有写回给我。

我的流由stream_socket_client()打开,并且没有返回false或抛出异常。我知道我的fwrite也成功地将154个字节写入该流。为什么Apple没有回复?

以下是连接APNS的代码:

function openConnection() {
    $streamContext = stream_context_create();
    stream_context_set_option($streamContext, 'ssl', 'local_cert', $this->APNS_CERT);
    stream_context_set_option($streamContext, 'ssl', 'passphrase', $this->Password);
    $apns = stream_socket_client('ssl://' . $this -> APNS_HOST . ':' . $this -> APNS_PORT, $error, $errorString, 60, STREAM_CLIENT_CONNECT, $streamContext);
    return $apns;
}

然后,在调用openConnection之后的下一个方法中:

//open the connection to use with apple.
$apns = $this->openConnection();

$alert = $message['alert'];
$badge = $message['badge'];
$deviceToken = $message['deviceToken'];

$payload['aps'] = array(
    'alert' => $message['alert'],
    'badge' => $message['badge'],
    'sound' => $message['sound']
);

if ($message['extraPayload'] != null) {
    $payload['acme'] = $message['extraPayload'];
}

$encodedString = json_encode($payload);

//create message
$apnsMessage = chr(1) . pack("N", $message['identifier']) . pack("N", $message['expire']) . pack("n", 32) . pack('H*', str_replace(' ', '', $message['deviceToken'])) . pack("n",strlen($encodedString)) . $encodedString;

$write = fwrite($apns, $apnsMessage);
echo $write;
//the echo was just to see if it wrote.

if (!$apns) {
    socket_close($apns);
    fclose($apns);

    echo "connection to APNS was lost.";
}


//look for changes. $null=null because some bug doesn't just let you pass null.
$null = null;
$changedStreams = stream_select($streamArray, $null, $null, 0, 1000000);

//check if it is actually false
if ($changedStreams === false) {
    //close stream when done.
    socket_close($apns);
    fclose($apns);

    echo "No response from APNs";
} elseif ($changedStreams > 0) {
//then check if what they sent back is an error and grab the error packet
$responseBinary = fread($apns, 6);
var_dump($responseBinary);

//check that it's the right thing
if ($responseBinary != false || strlen($responseBinary) == 6) {
    //convert it from it's binary stream state and print.
    $response = unpack('Ccommand/Cstatus_code/Nidentifier', $responseBinary);
    var_dump($response);

    //close stream when done.
    socket_close($apns);
    fclose($apns);
    }
} else {
    echo "Apple failed to respond, message was not sent.";
}

最后的var_dumpNULL

编辑:

原来这是一个错误的凭据冲突。它是通过创建一个新的pem文件来解决的。

1 个答案:

答案 0 :(得分:1)

从该页面开始:

您应该定期与反馈Web服务器连接,并获取那些反复报告失败传递尝试的设备的当前列表。然后,您应该停止向与这些应用程序关联的设备发送通知。有关详细信息,请参阅“反馈服务”。

反馈服务

通过类似于用于发送推送通知的二进制接口来访问反馈服务。您可以通过feedback.push.apple.com,端口2196访问生产反馈服务;您可以通过feedback.sandbox.push.apple.com,端口2196访问沙箱反馈服务。与推送通知的二进制接口一样,您必须使用TLS(或SSL)建立安全的通信通道。这些连接所需的SSL证书与为发送通知而配置的SSL证书相同。要建立可信的提供者身份,您应该在连接时使用对等身份验证将此证书提供给APN。