Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send Push Notification to many device tokens [closed]

i have this PHP script:

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

result for all token - Connection OK! But the Push Notification is received by only one device! I tried sending different messages but the result did not change :(

like image 689
Alexander Zakatnov Avatar asked Oct 27 '11 13:10

Alexander Zakatnov


1 Answers

try my code. I test it with 4 devices.

Sorry about the comments that are in spanish.

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

In my active_users DB I have all the tokens. I hope it help

like image 52
Eduardo Iglesias Avatar answered Oct 19 '22 23:10

Eduardo Iglesias