2011-10-27 162 views
7

我有這個PHP腳本:發送推送通知到許多設備令牌

$q = mysql_query("SELECT `token` FROM `tokens`"); 

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


while($token = mysql_fetch_array($q)) 
{ 
    $deviceToken = $token[0]; 
    echo $deviceToken.": "; 
    $payload['aps'] = array('alert' => $message, 'sound' => 'Completed.wav'); 
    $payload = json_encode($payload); 

    if (!$fp) 
    { 
     echo "Failed to connect {$err} {$errstrn}<br />"; 
    } 
    else 
    { 
     echo "Connection OK<br />"; 
    } 

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

    if (fwrite($fp, $msg, strlen($msg)) === FALSE) 
    { 
     echo "can't write to socket!<br />"; 
    } 
} 

fclose($fp); 

結果所有令牌 - 連接OK!但推送通知僅由一個設備接收!我試着發送不同的信息,但結果並沒有改變:(

+0

哇!想法!?? –

+1

只是一個(可能是愚蠢的)想法 - APN網關是否支持多個PN在單個連接?也許你應該嘗試移動打開/關閉循環內的流 – mkilmanas

+0

不可以,它不起作用。但我發現類在谷歌代碼中,在這個類實現方法這個唯一的ID爲消息,但關於唯一的ID在蘋果文件沒有關於此ID ... –

回答

3

試試我的代碼。我有4對設備進行測試。

比較遺憾的是在西班牙的意見。

<?php 
include("../conectar.php"); 

if (isset($_GET['msj']) and isset($_GET['cupon']) and $_GET['msj'] != '' and $_GET['cupon'] != '') { 

    $msj = $_GET['msj']; 
    $cupon = $_GET['cupon']; 

    echo '<textarea name="textarea" id="textarea" cols="70" rows="10">'; 

    // Le mandamos el msj a todos los tokens registradosdevice_token 
    $sql = "SELECT * FROM active_users"; 
    $resultSql = mysql_query($sql) or die (mysql_error()); 

    echo "Se enviaran: ".mysql_num_rows($resultSql)." notificaciones" . PHP_EOL; 
    ob_flush(); 
    flush(); 

    $i = 0; 
    while($row = mysql_fetch_array($resultSql)) { 

     $deviceToken[$i] = $row['token']; 
     $i++; 
    } 

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

     // Put your alert message here: 
     $message = $msj; 

     ////////////////////////////////////////////////////////////////////////////////  
     $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.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); 

     echo 'Connected to APNS' . PHP_EOL; 
     ob_flush(); 
     flush(); 

     // Create the payload body 
     $body['aps'] = array(
      'alert' => $message, 
      'sound' => 'default', 
      'badge' => '+1' 
     ); 
     $body['tags'] = array(
      'cupon' => $cupon 
     ); 

     // Encode the payload as JSON 
     $payload = json_encode($body); 

     for($i = 0; $i<count($deviceToken); $i++) { 

      // Build the binary notification 
      $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken[$i]) . pack('n', strlen($payload)) . $payload; 

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

      if (!$result) 
       echo 'Message not delivered' . PHP_EOL; 
      else 
       echo 'Message successfully delivered' . PHP_EOL; 
      ob_flush(); 
      flush(); 

     } 

     // Close the connection to the server 
     fclose($fp); 

    echo '</textarea>'; 
}else{ 
    echo 'error #2'; 
} 
?> 

在我active_users DB我有所有的令牌,我希望它有幫助

+0

a有點晚...但這對我很有用 – spankmaster79

+0

這段代碼與問題中的代碼沒有區別(關於連接和fwrites) – jptsetung

相關問題