PHP technique to query the APNs Feedback Server

前端 未结 5 1040
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-27 11:15

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

The docs say it starts sending as soon as the connection is made. Does

5条回答
  •  难免孤独
    2020-11-27 12:02

    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!

    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;
    }
    

    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
        ),
    )
    

提交回复
热议问题