将推送通知发送到许多设备令牌

时间:2011-10-27 13:37:42

标签: php ios xcode apple-push-notifications

我有这个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!但推送通知仅由一个设备接收!我尝试发送不同的消息,但结果没有改变:(

1 个答案:

答案 0 :(得分: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中,我拥有所有令牌。我希望它有所帮助