2012-02-02 3 views
0

私はこのphpファイルを持っています。私はそれをデバイスに送信するために使用したとき、それはOKです と私は問題なしで通知を受け取ります 今私はデバイストークン以上を持っています。すべてのデバイスへ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); 
+0

あなたは何をしようとしていますか?どのような問題がありますか? –

+0

問題コード私はコードを変更して同じコード内の他のデバイストークンに送信することができません。複数のデバイスを追加してプッシュを送信したい – AbouHaRga

答えて

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); 
} 
?> 
+0

ありがとうございますが、すべてを作ってプッシュを送信した後に問題があります(APNSメッセージに正常に配信されたAPNSメッセージに接続しましたが正常に配信されました)が表示されますが、私は自分のiPhoneでプッシュを取得しませんでした。 – AbouHaRga

+0

あなたのアイフォーン用の情報を間違いなく入れましたか? もしあなたがそうしたのであれば、最も簡単にデバッグを開始する場所は、foreachループの開始時に 'print_r($ device)'でしょう。これが何を出力するか見てください - 配列が正しく設定されていないと思います。 – Hecksa

+0

まだ問題はありますかAPNSメッセージに正常に配信されましたが、私のiPhoneで通知が届きませんでした – AbouHaRga

関連する問題