0

I have make a code that send a push notification (for distribution) at many device that have save on db, i have make the php:

// Put your alert message here:
$message = "send push";
// Put your private key's passphrase here:
$passphrase = 'phrase';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'apns.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

if (!$fp)
    exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS'. "<br>" ;
ob_flush();
flush();

// Create the payload body
$body['aps'] = array(
'alert' =&gt; $message,
'sound' =&gt; 'default'
);

// Encode the payload as JSON
$payload = json_encode($body);
if (mysql_num_rows($results)!=0) {
    while($row = mysql_fetch_array($results))
    {
        $deviceToken= $row['deviceToken'];

        if(isset($deviceToken)&&(strcmp($deviceToken,"(null)")!=0)){
            echo "<br>".$deviceToken."<br>";
            // Build the binary notification
            $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
            $result = fwrite($fp, $msg, strlen($msg));
            if (!$result)
                echo 'Message not delivered';
            else
                echo 'Message successfully delivered' ;
            ob_flush();
            flush();
        }
    }
    // Close the connection to the server
    fclose($fp);
}

If i have on db, only deviceToken correct or with value (null), the message is send but if i have one deviceToken wrong i receive the message 'Message successfully delivered' that have send all message but not work. where i make the mistake? Thanks.

Nate
  • 31,017
  • 13
  • 83
  • 207
Doom
  • 1,258
  • 3
  • 15
  • 24

2 Answers2

0

You does nothing wrong, this is an apple quality control. I met this phenomenon, and - if I remember well - if the structure of the device token was invalid, no push notification was delivered...

So, if you have a valid token, which currently does not belong to a device (e.g the device has removed your app), your other push notification will be delivered, but if a token seems to have an absolutely incorrect structure, no push notification will be delivered.

So, this phenomenon will not be dangerous for your production system, because your device will register a valid token to your provider's DB.

Maybe more details are between the comment of this tutorial: http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12

Balazs Nemeth
  • 2,333
  • 19
  • 29
0

This cannot be done. There isn't a sure fire way to tell if a push notification was sent successfully or not. There are alternatives, but they are not on the fly and not as reliable.

  1. Make sure the app is installed on the users device.
  2. Add logic to your application to post to your database when a push notification is received. That way you know the push was good. The downside to this is that push notifications are not always instant so there could be delay.

A user has explained this and given some links in another post. How to find the Apple Push Notification Delivered to user or not from our server?

Community
  • 1
  • 1
Bot
  • 11,868
  • 11
  • 75
  • 131