简体   繁体   中英

Only first push notification is received PHP

I have a little PHP script that sends Push notifications to multiple devices. The content is different from device to device. Somehow only the first Request is received, so only the first device in the queue is receiving the push notification. What's the error?

$sql = "SELECT * FROM pushdevices";
$query = mysql_query($sql);
$apnsHost = 'gateway.push.apple.com';
$apnsPort = 2195;
$apnsCert = ...;

$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);

$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);
while($result = mysql_fetch_assoc($query)) {
    [...]
        $deviceToken = $result["deviceToken"]; 
            $alertBody = '...';
        $payload['aps'] = array('alert' => $alertBody, 'badge' => $badge, 'sound' => 'default');
        $payload = json_encode($payload);

        if ($apns)
        {
          $apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;
          $is = fwrite($apns, $apnsMessage);

          echo "sent: $deviceToken<br />";
        }
        else
        {
          echo "Fehler!";
          var_dump($error);
          var_dump($errorString);
        }
    }
}
 fclose($apns);
?>

In the output it says sent to every device, but only the first is receiving. If I take the return value of $is (fwrite) it's 140 the first time and then 155. Maybe that's an error code?

Thanks for answers!

I found the problem, I just had to reset the $payload -variable at the end of the while. Now it's working!

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