Send Push Notification To IOS App Using PHP

- First Create a PEM file with your XCODE.
- Create this file (GCM.php)

<?php

class GCM {
//put your code here
// constructor
function __construct() {
} /**
* Sending Push Notification
*/
function send_gcm_notification_ios($subject,$deviceToken,$flag) {

$passphrase = ‘YOUR_PEM_FILE_PASSWORD'; $message = $subject; $flag=$flag; $ctx = stream_context_create(); stream_context_set_option($ctx, 'ssl', 'local_cert', ‘YOUR_PEM_FILE_NAME.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

// Open a connection to the APNS server
$fp = stream_socket_client(
//for production // 'ssl://gateway.push.apple.com:2195', $err,


//for test
'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
$body2['aps'] = array(
'alert' => $message,
'sound' => 'default',
);
// Encode the payload as JSON $payload = json_encode($body2); // 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)); }}
?>


- At you PHP file you want to send Push Notification, Just call function we created as below:

$subject=“YOUR_MESSAGE
”;
$deviceToken = “DEVICE_ID_MESSAGE_WILL_SENT";
$flag="message";
$gcm = new GCM();
$result
= $gcm->send_gcm_notification_ios($subject, $deviceToken);

- This will call function we created first then send push notification to device ID given.

Comments

Popular Posts