2014-10-03 167 views
0

我想用一个php脚本从数据库发送多个PN。表中有3个设备令牌,但脚本只将它发送给第一个设备令牌。iOS发送多个推送通知

这里的脚本,有人想法。

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

$fp = stream_socket_client(
    'ssl://gateway.sandbox.push.apple.com:2195', $err, 
    $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); 

if (!$fp){ 
    exit("Failed to connect: $err $errstr" . PHP_EOL); 
} 

$ausgabe.= 'Connected to APNS' . PHP_EOL . '<hr>'; 

$body['aps'] = array(
    'alert' => $message, 
    'sound' => 'note.wav', 
    'badge' => 1 
); 

$payload = json_encode($body); 

$selectSQL = 'SELECT * from app_pushnotification'; 
$result = mysql_query($selectSQL); 
while($row = mysql_fetch_array($result)){ 
    $i+=1; 
    $ausgabe.= $i.' - '. $row['device_token'].' '; 
    $deviceToken = $row['device_token']; 

    $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload; 

    $result = fwrite($fp, $msg, strlen($msg)); 

    if (!$result){ 
     $ausgabe.='Message to not delivered' . PHP_EOL . '<hr>'; 
    } else { 
     $ausgabe.='Message to successfully delivered' . PHP_EOL . '<hr>'; 
    } 

} 

$ausgabe.='close connection'; 
fclose($fp); 

} 
echo $ausgabe; 

电贺凯文

+0

尝试每条消息使用一个连接。关闭/打开循环中的连接。 – kezi 2014-10-03 18:29:05

+0

@kdogisthebest这是一个坏主意。太慢了! – Qualcuno 2014-10-03 18:30:13

+0

Kevin,查询返回多少行?你确定错误不在数据库中,而是在APNS服务器上? – Qualcuno 2014-10-03 18:31:11

回答

2

你的错误是你在循环改变$result值。因此脚本无法读取下一行数组。

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

$fp = stream_socket_client(
    'ssl://gateway.sandbox.push.apple.com:2195', $err, 
    $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); 

if (!$fp){ 
    exit("Failed to connect: $err $errstr" . PHP_EOL); 
} 

$ausgabe.= 'Connected to APNS' . PHP_EOL . '<hr>'; 

$body['aps'] = array(
    'alert' => $message, 
    'sound' => 'note.wav', 
    'badge' => 1 
); 

$payload = json_encode($body); 

$selectSQL = 'SELECT * from app_pushnotification'; 
$result = mysql_query($selectSQL); 
while($row = mysql_fetch_array($result)){ 
    $i+=1; 
    $ausgabe.= $i.' - '. $row['device_token'].' '; 
    $deviceToken = $row['device_token']; 

    $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload; 

    $write_result = fwrite($fp, $msg, strlen($msg)); 

    if (!$write_result){ 
     $ausgabe.='Message to not delivered' . PHP_EOL . '<hr>'; 
    } else { 
     $ausgabe.='Message to successfully delivered' . PHP_EOL . '<hr>'; 
    } 

} 

$ausgabe.='close connection'; 
fclose($fp); 

} 
echo $ausgabe; 

注意,第二$result =现在$write_result =