0

每次我用 php 发送大量 APN 时,我都会发送大约 8-10 个,然后其余的都失败了。当我第二次尝试时,每次都会成功发送其余通知。它从来不是同一个设备,因为我将发送的设备设置为标记,因此他们不会收到双重通知,而且我什至永久禁用了它失败的设备,因为最初我认为这些设备是问题所在。它们都是生产设备,而不是测试设备,因为它们也是分开的。我无数次得到相同的结果,但不知道为什么会这样。

代码:

$date = date("Ymd");
$englishOnly = "en";
$dateHolder = date('Ymd');
$serverTimeInt = date('Gi');


// Put your private key's passphrase here:
$passphrase = 'mypw';

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.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' . PHP_EOL . "<br>";

$doubleFix = array();
$clearArray = array();
$pass = true;
?>
<?php
$messageId = 1;
$alertMessage = "My Message";
?>
<?php
$sqlpush = "SELECT * FROM devices WHERE ios=1";
$sqlresult = mysql_query($sqlpush);
while($row = mysql_fetch_array($sqlresult))
{
$deviceToken = $row['deviceid'];
$badges = $row['badges'];
$messages = explode(',',$row['messages']);
$pass = true;
$messagesUpdate = $row['messages'] . ',' . $messageId;


foreach($messages as $messageCheck)
{
if($messageCheck == $messageId)
{
$pass = false;
}
}

if($pass == true)
{
$sql2 = "UPDATE devices SET badges=badges+1 WHERE deviceid='$deviceToken'";
mysql_query($sql2)
or die('Query failed. ' . mysql_error());

$message = $alertMessage;

$body['aps'] = array(
'alert' => $message,
'sound' => 'default',
'badge' => $badges+1
);

// Encode the payload as JSON
$payload = json_encode($body);
echo $payload;
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;

// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));

if (!$result)
echo 'Message not delivered' . PHP_EOL . "<br>";
else
echo 'Message successfully delivered to ' . $deviceToken . PHP_EOL . "<br>";

}
}
$doubleFix = $clearArray;

fclose($fp);
?>
<?php
$finish = microtime(TRUE);
$totaltime = $finish - $start;
echo "This script took ".$totaltime." seconds to run";
?>

本质上它连接到 apn 服务,循环通过我所有的设备,然后关闭。我正在发送几千条通知。

其他人有这个问题,或者知道为什么会这样吗?

4

0 回答 0