PHP 技术来查询 APNs 反馈服务器 [英] PHP technique to query the APNs Feedback Server

查看:27
本文介绍了PHP 技术来查询 APNs 反馈服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就您查询的方式而言,有人可以澄清 APNs(Apple 推送通知)想要什么吗?

Can someone clarify what the APNs (Apple Push Notification) wants as far as how you query it?

文档说它在建立连接后立即开始发送.这是否意味着我不会对其执行 fread() ?

The docs say it starts sending as soon as the connection is made. Does this mean that I don't do an fread() on it?

这是我当前尝试阅读的代码.我没有将 fread() 放入循环中,因为我不知道什么响应表示没有更多的记录可读取",而且我不想在我的服务器上出现无限循环.

Here's my current code to try and read it. I did NOT put the fread() in a loop as I do not know what response indicates "no more records to read" and I didn't want an infinite loop on my server.

<?php
$apnsCert = 'HOHRO-prod.pem';

$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
stream_context_set_option($streamContext, 'ssl', 'verify_peer', false);

$apns = stream_socket_client('ssl://feedback.push.apple.com:2196', $error, $errorString, 60, STREAM_CLIENT_CONNECT, $streamContext);

echo 'error=' . $error;
echo 'errorString=' . $errorString;


$result = fread($apns, 38);
echo 'result=' . $result;


fclose($apns);
?>

到目前为止,我得到的只是一个空回复.没有错误,所以它正在连接.

So far all I am getting is a null reply. There are no errors so it is connecting.

我不知道 null 回复是否意味着没有数据,或者我的 fread() 是错误的做法.

I don't know if the null reply means no data is there, or my fread() is the wrong way to do it.

谢谢

推荐答案

当我第一次尝试连接时,有一个大问题让我很困惑:APNS 反馈服务器只返回自上次过期"以来的设备令牌反馈请求.这意味着大多数情况下您会得到一个 NULL 响应,除非您已经在与大量的应用用户打交道.

Here's a big gotcha which confused me when I first tried connecting: the APNS feedback servers only return the device tokens that have "expired" since your last feedback request. Which means most of the time you'll get a NULL response unless you're already dealing with a high volume of users of your app.

因此,请确保将过期的设备令牌存储到磁盘或数据库中,因为在您的反馈查询之后,它们将永远消失.至少可以说,这让测试变得很痛苦!

So make sure you store the expired device tokens to disk or db, because after your feedback query they're gone for good. This makes testing a pain to say the least!

这是一个从 APNS 反馈服务器获取设备令牌的完整函数(非常感谢上面的答案帮助我把它们放在一起):

Here's a complete function to fetch the device tokens from the APNS feedback servers (many thanks to the answers above for helping me put it all together):

function send_feedback_request() {
    //connect to the APNS feedback servers
    //make sure you're using the right dev/production server & cert combo!
    $stream_context = stream_context_create();
    stream_context_set_option($stream_context, 'ssl', 'local_cert', '/path/to/my/cert.pem');
    $apns = stream_socket_client('ssl://feedback.push.apple.com:2196', $errcode, $errstr, 60, STREAM_CLIENT_CONNECT, $stream_context);
    if(!$apns) {
        echo "ERROR $errcode: $errstr\n";
        return;
    }


    $feedback_tokens = array();
    //and read the data on the connection:
    while(!feof($apns)) {
        $data = fread($apns, 38);
        if(strlen($data)) {
            $feedback_tokens[] = unpack("N1timestamp/n1length/H*devtoken", $data);
        }
    }
    fclose($apns);
    return $feedback_tokens;
}

如果一切顺利,该函数的返回值将如下所示(通过 print_r()):

If all is well, the return values from this function will look something like this (via print_r()):

Array
(
    Array
    (
        [timestamp] => 1266604759
        [length] => 32
        [devtoken] => abc1234..............etcetc
    ),
    Array
    (
        [timestamp] => 1266604922
        [length] => 32
        [devtoken] => def56789..............etcetc
    ),
)

这篇关于PHP 技术来查询 APNs 反馈服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆