0

my swift 2 app goes online a few seconds ago. i can send single push notifications to one device.

but if i send one notification to two devices, the devices get no notifications. the log "Message successfully delivered" will be shown.

here my php file:

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'xxx.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', 'xxx');

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

    for ($y = 0; $y < count($deviceTokens); $y++)
      {
        $body['aps'] = array('silentPush' => 0, 'alert' => 'Test', 'badge' => $badgeNumber, 'sound' => 'default');          
        $payload = json_encode($body);
        $msg = chr(0) . pack('n', 32) . pack('H*', $deviceTokens[$y]) . pack('n', strlen($payload)) . $payload;
        $result = fwrite($fp, $msg, strlen($msg));

        if (!$result) 
            {
                echo 'Message not delivered' . PHP_EOL.'<br />';
            } 
        else 
            {
                echo 'Message successfully delivered' . PHP_EOL.'<br />';
            }
    }

Where is my mistake?

Stack108
  • 915
  • 2
  • 14
  • 35
  • Have you tried debugging that for() to make sure it is actually looping through the tokens and sending a unique payload to each one of them? – Dan H Apr 18 '16 at 11:31
  • i checked it. the loops goes 2 times around with the same alert text, but with different tokens. but i found this a few seconds ago: 'One note: say you have 10 device tokens, but the 7th is invalid (i.e. the user uninstalled app, for example)... when you send out the notifications each will go out successfully UNTIL that 7th one. the response / result may show as successful, but since its invalid as a token, apns will drop the connection to the server and tokens 8 through 10 wont be sent at all..' one of this token is invalid. can this be my problem? – Stack108 Apr 18 '16 at 11:42
  • Then it means that when your loop starts, it sends a payload to the first device, which is invalid and it immediately drop the APNS connection. Then the 2nd device won't receive the payload because the connection has been dropped. I would do a print_r($result) to check what you're getting in return for valid and invalid pushes. I am sure the $result has some sort of error message you can verify to make sure the payload was actually sent. Then if you get an error on the $result, you can simply re-stablish the APNS connection and continue with the next iteration. – Dan H Apr 18 '16 at 11:51
  • this is what i get if i print_r $result: `{"aps":{"silentPush":0,"alert":"Test","badge":1,"sound":"default"}}`but how can i check if a token was valid or invalid? – Stack108 Apr 18 '16 at 12:57
  • Have a look at this: http://stackoverflow.com/questions/10058768/php-apple-enhanced-push-notification-read-error-response – Dan H Apr 18 '16 at 13:00

0 Answers0