Как изменить PHP-файл для отправки на большее количество устройств Push-уведомления в цикле? - PullRequest
0 голосов
/ 02 февраля 2012

У меня есть этот php-файл, и когда я использую его для отправки на свое устройство, все в порядке, и я получаю уведомление без проблем, теперь у меня больше токена устройства, и я хочу изменить php-файл, чтобы сделать цикл для отправки навсе устройства

<?php

// Put your device token here (without spaces):

$deviceToken = '';

// Put your private key's passphrase here:

$passphrase = '';

// Put your alert message here:

$message = '';



////////////////////////////////////////////////////////////////////////////////

$ctx = stream_context_create();

stream_context_set_option($ctx, 'ssl', 'local_cert', '.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;


// Create the payload body

$body['aps'] = array(

    'alert' => $message,

    'sound' => 'default'

    );

// Encode the payload as JSON

$payload = json_encode($body);


// Build the binary notification

$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . 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;


// Close the connection to the server

fclose($fp);

1 Ответ

0 голосов
/ 02 февраля 2012

Из того, что я вижу, вам нужно было бы поместить всю нижнюю половину скрипта в цикл и пройтись по каждому устройству, хранящемуся в массиве. Возможно, есть лучший способ, в зависимости от того, как на самом деле работает используемая вами система, но нижеприведенное должно дать то, что вы ищете, если я не допустил ошибок, что весьма вероятно, поскольку я могу » проверить это.

<?php
$message = ''; //Put Message Here

$devices = Array();

$devices[0] = Array();
$devices[0]["deviceToken"] = ''; //Put First DeviceToken Here
$devices[0]["passphrase"] = ''; //Put First Passphrase Here

$devices[1] = Array();
$devices[1]["deviceToken"] = ''; //Put Second DeviceToken Here
$devices[1]["passphrase"] = ''; //Put Second Passphrase Here

//Copy and paste the above 3 lines as desired, adding 1 to the number $devices[<NUMBER>] for each additional device
//Make sure to put their specific information in each line.

//-------------------------------------------------------

foreach($devices as $device){
$ctx = stream_context_create();

stream_context_set_option($ctx, 'ssl', 'local_cert', '.pem');

stream_context_set_option($ctx, 'ssl', 'passphrase', $device["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;


// Create the payload body

$body['aps'] = array(

    'alert' => $message,

    'sound' => 'default'

    );

// Encode the payload as JSON

$payload = json_encode($body);


// Build the binary notification

$msg = chr(0) . pack('n', 32) . pack('H*', $devices["deviceToken"]) . 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;


// Close the connection to the server

fclose($fp);
}
?>
...