Sample php file for send push notification to IOS devices

3:44 PM


I've write a php file for send push notification to IOS devices


$deviceToken = "2efacf3609f7e6c59a27d0135cdce7a5e4acfe653e0b73181cc4664c3ef2bb34";
$passphrase = '123456';
$ctx = stream_context_create();

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

$fp = stream_socket_client(
                           //'ssl://gateway.sandbox.push.apple.com:2195', $err, // for development
                           'ssl://gateway.push.apple.com:2195', $err, // for production
                           $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

if (!$fp) {
    exit("Failed to connect: $err $errstr" . PHP_EOL);
    }
  
  echo 'connecting to APNS' . PHP_EOL;
   
    // payload
 $body['aps'] = array(
                 'alert' => "This is a Message",
                 'title' => "This is a Title"
                 );
 
 
// convert payload to json
$payload = json_encode($body);

$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
$result = fwrite($fp, $msg, strlen($msg));

if (!$result) {
   echo 'Message not delivered' . PHP_EOL;
} else
    echo 'Message successfully delivered' . PHP_EOL;
}

fclose($fp);

You Might Also Like

0 comments