APNS在处于非活动状态后无法传递消息


APNS fails to deliver message after inactivity

我已经用PHP为APNS创建了一个客户端-服务器解决方案。

"服务器"是一个持续运行的脚本,它等待连接并将内容复制到APNS。这意味着它始终保持与APNS的连接处于打开状态。我使用fwrite和流上下文。这是接受传入连接的部分:

if ($client = @stream_socket_accept($this->socket, -1)) {
   $this->writeLine("Received connection.");
   $this->inputBuffer = stream_get_contents($client);
}

它在while循环中运行,以便下一次迭代将inputBuffer的内容复制到APNS,如下所示:

$writtenBytes = @fwrite($this->connectionAPNS, $this->inputBuffer, strlen($this->inputBuffer));

然后我检查写为:的字节

if ($writtenBytes > 0) {
$this->writeLine("Copied $writtenBytes bytes to APNS.");
   return true;
}

现在,当连接闲置了一段时间后,问题就出现了。第一个推送通知将返回写入APNS的字节数,例如"Copied 157 bytes to APNS。",表示fwrite有效,但消息未送达。

然后,下一个通知返回写入的0个字节,自动重新连接(我的脚本的一部分)并写入失败的字节,然后它就工作了。

如果连接实际上无法写入或APNS不接受写入的字节,为什么要返回写入的字节?

我使用增强的通知格式,并检查返回的错误,这些错误似乎都没有。

我使用这段代码,这是我在博客上发现的,在向APNS:写入后,我将连接句柄传递给函数

function checkAppleErrorResponse($fp)
{
    $apple_error_response = fread($fp, 6);
    if ($apple_error_response) {
        error_response = unpack('Ccommand/Cstatus_code/Nidentifier', $apple_error_response);
        if ($error_response['status_code'] == '0') {
            $error_response['status_code'] = '0-No errors encountered';
        } else {
            // Here is some fancy if/else error handling, but I never end in this else , so I excluded it.
        }
     }
}

保持连接APNS打开的"客户端"部分如下所示,每当writtenBytes返回0(错误)或脚本首次加载时都会调用:

function connectToAPNS()
    {
        $ctx = stream_context_create();
        stream_context_set_option($ctx, 'ssl', 'local_cert', $this->certpath);
        stream_context_set_option($ctx, 'ssl', 'passphrase', $this->passphrase);

        // Open a connection to the APNS server
        $error = "";
        $errno = 0;
        $this->connectionAPNS = stream_socket_client(
            'ssl://gateway.push.apple.com:2195',
            $errno,
            $error,
            60,
            STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT,
            $ctx
        );
        if ($this->connectionAPNS) {
            stream_set_blocking($this->connectionAPNS, 0);
            $this->writeLine("Connected to APNS!");
            return true;
        } 
    }

您的应用程序是发布版还是调试版?在每种情况下,您都必须与不同的web服务通信并使用不同的证书。

在沙箱/调试模式下,与通信ssl://gateway.sandbox.push.apple.com:2195

在生产/关系模式下,与通信ssl://gateway.push.apple.com:2195

如果您使用错误的服务进行通信,则会自动删除通知。