查询APNs反馈服务器的PHP技术 [英] PHP technique to query the APNs Feedback Server
问题描述
有人可以澄清APN(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
),
)
这篇关于查询APNs反馈服务器的PHP技术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!