0

私はこの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);
4

1 に答える 1

0

私が見る限り、スクリプトの下半分全体をループに入れて、配列に格納された各デバイスを処理する必要があります。使用しているシステムが実際に動作する方法によっては、より良い方法があるかもしれませんが、私が間違いを犯していなければ、以下はあなたが探しているものを達成するはずです。テストしないでください。

<?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);
}
?>
于 2012-02-02T13:39:35.557 に答える