xcode 向许多设备令牌发送推送通知

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7916686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:15:11  来源:igfitidea点击:

Send Push Notification to many device tokens

phpiosxcodeapple-push-notifications

提问by Alexander Zakatnov

i have this PHP script:

我有这个 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);

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 :(

所有令牌的结果 - 连接正常!但是推送通知只能被一台设备接收!我尝试发送不同的消息,但结果没有改变:(

回答by Eduardo Iglesias

try my code. I test it with 4 devices.

试试我的代码。我用 4 个设备对其进行了测试。

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

在我的 active_users 数据库中,我拥有所有令牌。我希望它有帮助