简体   繁体   中英

Get info from Apple push notification feedback

I want to get feedback from apple push notification with curl(PHP or Linux). I find this code for sending push notification

<?php
$url = 'https://feedback.push.apple.com:2196';
$cert = 'Cert.pem';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSLCERT, $cert);
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, "passphrase");
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"device_tokens": ["XXXX"], "aps": {"alert": "test message one!"}}');
$curl_scraped_page = curl_exec($ch);
?>

Feedback Service

Apple provide a feedback service which you are supposed to occasionally poll. This will provide a list of deviceTokens that were previously but are no longer valid, such as if the user has uninstalled your iPhone application . You can then remove the deviceToken from your database so you do not communicate with an invalid device.

I need a PHP script to get this list from Apple feedback service.

Thanks

You could use the following code:

<?php

    $apnsCert = 'Your_Certificate_File.pem'; //Put your Certificate_PATH/Certificate_File.pem Here

    $streamContext = stream_context_create(); //Creates a stream context 

    stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);//Sets option for a stream | For more information please see this page http://php.net/manual/en/function.stream-context-set-option.php 

    stream_context_set_option($streamContext, 'ssl', 'verify_peer', false); //Sets option for a stream

    $apns = stream_socket_client('ssl://feedback.push.apple.com:2196', $error, 

    $errorString, 60, STREAM_CLIENT_CONNECT, $streamContext); //Open Internet or Unix domain socket connection

    echo 'error=' . $error . "<br />";  //Show error number

    echo 'errorString=' . $errorString . "<br />"; //Show error string

    $result = fread($apns, 38); // Binary-safe file read and store in $result

    $unpacked = unpack("N1timestamp/n1length/H*devtoken", $result);//Get token from apple APNS

    echo 'Token is' ;print_r($unpacked); // Show token and can be replace with database update commands

    fclose($apns);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM