Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How get feedback from APNs when sending push notification

Now I can send push Token from device that has installed a pass already, but I don't know how the feedback work in this point. From apple docs, Apple Push Notification service (APNs) provides feedback to server to tell if pushToken is valid or not. How to get this feedback ? I try this code, but a lot errors. This is the code:

<?php
$cert = '/Applications/MAMP/htdocs/passesWebserver/certificates.pem';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', $cert);
stream_context_set_option($ctx, 'ssl', 'verify_peer', false);

$fp = stream_socket_client('ssl://feedback.sandbox.push.apple.com:2196', $error,            $errorString, 60, STREAM_CLIENT_CONNECT, $ctx);
// production server is ssl://feedback.push.apple.com:2196

if (!$fp) {
error_log("Failed to connect feedback server: $err $errstr",0);
return;
}
else {
   error_log("Connection to feedback server OK",0);
}
    error_log("APNS feedback results",0);
    while ($devcon = fread($fp, 38))
    {
   $arr = unpack("H*", $devcon); 
   $rawhex = trim(implode("", $arr));
   $feedbackTime = hexdec(substr($rawhex, 0, 8)); 
   $feedbackDate = date('Y-m-d H:i', $feedbackTime); 
   $feedbackLen = hexdec(substr($rawhex, 8, 4)); 
   $feedbackDeviceToken = substr($rawhex, 12, 64);
   error_log ("TIMESTAMP:" . $feedbackDate, 0);
      error_log ( "DEVICE ID:" . $feedbackDeviceToken,0);
    }
fclose($fp);
?>
like image 299
malinchhan Avatar asked Apr 11 '13 08:04

malinchhan


People also ask

How do I check my APN push notifications?

Sending a test Android push notificationClick on Settings and open Mobile Apps. Click on the Android App and make sure the Firebase API key has been configured. Click on Test Push and enter the device token for your test device. Add a test payload and send the test.

What is APNs feedback service?

The APNs system includes a feedback service that can be queried to check for devices that have unregistered and no longer need to be notified. The device tokens maintainer: A Web Services server program maintaining the database of device tokens, with application user information.

Are Apple push notifications guaranteed?

The system makes every attempt to deliver local and remote notifications in a timely manner, but delivery isn't guaranteed. The PushKit framework offers a more timely delivery mechanism for specific types of notifications, such as those VoIP and watchOS complications use.

How do push notifications actually work?

A push service receives a network request, validates it and delivers a push message to the appropriate browser. If the browser is offline, the message is queued until the the browser comes online. Each browser can use any push service they want, it's something developers have no control over.


1 Answers

This should work. You don't need to run this with every push request. Depending on how frequently you update, and the number of devices, you could set a daily or weekly cron job.

$cert_file = '/path/to/combined/cert.pem';
$cert_pw = 'top secret';

$stream_context = stream_context_create();
stream_context_set_option($stream_context, 'ssl', 'local_cert', $cert_file);
if (strlen($cert_pw))
    stream_context_set_option($stream_context, 'ssl', 'passphrase', $cert_pw);

$apns_connection = stream_socket_client('feedback.push.apple.com:2196', $error_code, $error_message, 60, STREAM_CLIENT_CONNECT, $stream_context);

if($apns_connection === false) {
    apns_close_connection($apns_connection);

    error_log ("APNS Feedback Request Error: $error_code - $error_message", 0);
}

$feedback_tokens = array();

while(!feof($apns_connection)) {
    $data = fread($apns_connection, 38);
    if(strlen($data)) {
        $feedback_tokens[] = unpack("N1timestamp/n1length/H*devtoken", $data);
    }
}
fclose($apns_connection);


if (count($feedback_tokens))
    foreach ($feedback_tokens as $k => $token) {
         // code to delete record from database
    }
like image 186
PassKit Avatar answered Oct 11 '22 14:10

PassKit