2011-10-27 6 views
7

ho questo script PHP:Invia Push Notification a molti dispositivo gettoni

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

risultato per tutti i Token - Connessione OK! Ma la notifica push è ricevuta da un solo dispositivo! Ho provato l'invio di messaggi diversi, ma il risultato non cambia :(

+0

wow !!! ideas! ?? –

+1

Solo un'idea (potenzialmente stupida) - il gateway APN supporta più PN in una singola connessione? Forse dovresti provare a spostare il apertura/chiusura del flusso all'interno del ciclo – mkilmanas

+0

no, non funziona. ma ho trovato la classe in codice google, in questa classe implementare metodo questo ID univoco per i messaggi, ma su ID univoci nella documentazione di Apple nulla su questo id ... –

risposta

3

del mio codice. I test con 4 dispositivi.

Mi dispiace per i commenti che sono in spagnolo.

<?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'; 
} 
?> 

Nella mia active_users DB Ho tutti i token. Spero che aiuti

+0

a Poco tardi ... ma questo funziona per me – spankmaster79

+0

Questo codice non è diverso da quello nella domanda (per quanto riguarda la connessione e le fwrites) – jptsetung