0

i have this php file and when i use it to send to my device it is ok and i receive the notification without any problem now i have more than Device Token and i want to modify the php file to make loop to send to all Devices

<?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);
  • What are you trying to do? What problems do you have? –  Feb 02 '12 at 10:25
  • the problem i can't modify the code to send to more device token in the same code i want to add more than one device to send push – AbouHaRga Feb 02 '12 at 13:21

1 Answers1

0

From what I can see, you'd need to put the entire bottom half of the script in a loop and go through each device, stored in an array. There may be a better way, depending on the way that the system you're using actually works, but the below should achieve what you're looking for, if I haven't made any mistakes, which is very likely since I can't test it.

<?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);
}
?>
Hecksa
  • 2,762
  • 22
  • 34
  • Thanks a lot but there is a problem after i make every thing and send the push i receive this message ( Connected to APNS Message successfully delivered Connected to APNS Message successfully delivered ) but i didn't get the push in my iphone – AbouHaRga Feb 02 '12 at 16:31
  • Did you definitely put the info in correctly for your iphone? If you're sure you did, the easiest starting place to debug would be `print_r($device)` right at the start of the foreach loop. See what this outputs - I expect that the array isn't being set right. – Hecksa Feb 02 '12 at 20:08
  • Still i have the problem it show me Connected to APNS Message successfully delivered but i didn't get any notification on my iPhone – AbouHaRga Feb 05 '12 at 06:10
  • You added the `print_r($device)` in and you got exactly the same output? If so, there's probably a problem with the arrays themselves. – Hecksa Feb 05 '12 at 19:22
  • no i didn't get the same message but i get this error Parse error: syntax error, unexpected T_FOREACH in .php on line 19 – AbouHaRga Feb 06 '12 at 05:45
  • You need a semicolon at the end of the line - `print_r($device)` – Hecksa Feb 06 '12 at 09:26
  • i get this message Connected to APNS Message successfully delivered Connected to APNS Message successfully delivered but i didn't get the push in my iphone – AbouHaRga Feb 06 '12 at 13:31
  • Have you checked that the information you put in the variables at the top is correct? If so, it's the array declarations - you can take a look at http://php.net/manual/en/language.types.array.php to see how to correctly declare and use these types in php. – Hecksa Feb 06 '12 at 13:48