Hello Developers,
If you want to send push notification from php to iPhone application then create a new file with iphone_push.php and save it in your api folder. Paste following code in file :
<?php
function pushnofitication($deviceToken, $message)
{
// Put your device token here (without spaces):
$deviceToken = $deviceToken;
// Put your private key's passphrase here:
$passphrase = '12345';
// Put your alert message here:
$message = $message;
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'apns_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);
// Create the payload body
$body['aps'] = array(
'alert' =>
array (
"body" => $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);
}
?>
For this you need a .pem file. Get this file from your iphone developer and save in your folder with name apns_cert.pem. While create pem file firstly they create .cert file. After create .cert file they need to enter a passphrase to create .pem file. so also you need to change passphrase.
The Above code is for sandbox mode when you app is live then replace .pem file and change url with live so your push notification is also work with live app.
When you want to send push notification include this file in your file and call function :
pushnofitication($deviceToken, $message)
$deviceToken : device token in unique id of iphone so when you register user get this token and save it in database. When you send push notification get this token from database and pass in this function.
$message : Message which you want to show in notification.
Thanks & Regard
Pulkit Jalani
http://www.webtecso.com
If you want to send push notification from php to iPhone application then create a new file with iphone_push.php and save it in your api folder. Paste following code in file :
<?php
function pushnofitication($deviceToken, $message)
{
// Put your device token here (without spaces):
$deviceToken = $deviceToken;
// Put your private key's passphrase here:
$passphrase = '12345';
// Put your alert message here:
$message = $message;
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'apns_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);
// Create the payload body
$body['aps'] = array(
'alert' =>
array (
"body" => $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);
}
?>
For this you need a .pem file. Get this file from your iphone developer and save in your folder with name apns_cert.pem. While create pem file firstly they create .cert file. After create .cert file they need to enter a passphrase to create .pem file. so also you need to change passphrase.
The Above code is for sandbox mode when you app is live then replace .pem file and change url with live so your push notification is also work with live app.
When you want to send push notification include this file in your file and call function :
pushnofitication($deviceToken, $message)
$deviceToken : device token in unique id of iphone so when you register user get this token and save it in database. When you send push notification get this token from database and pass in this function.
$message : Message which you want to show in notification.
Thanks & Regard
Pulkit Jalani
http://www.webtecso.com
Comments
Post a Comment