Wednesday 15 March 2017

How to Send iOS push notification from PHP

How to Send iOS push notification from PHP?

PHP Code
//device token
$deviceToken = "a58c9e7406c4b591eda8f192027d00ef82cdba361821f693e7b8ac9cb3afbc61";

//Message will send to ios device
$message = 'this is custom message';

//certificate file
$apnsCert = 'pushcert.pem';
//certificate password
$passphrase = 1234;

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', $apnsCert);
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$fp = stream_socket_client('ssl://', $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);

// Create the payload body
$body['aps'] = array(
    'badge' => +1,
    'alert' => $message,
    'sound' => 'default'

$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;
    echo 'Message successfully delivered amar' . $message . PHP_EOL;

// Close the connection to the server

Question: What is full form of APNS?
Apple Push Notification Service

Question: What is device token in ios?
Device token is an identifier for the Apple Push Notification System for iOS devices.

Question: What is apnsHost for Development and LIVE?
Development Server

Production Server

Question: From where we can test the APN Host?

Question: What port need to enable for APNs notification?