Saturday, 11 February 2017

IOS Push Notification Setup

<?php

//domain.com/ios_push/simplepush.php?tokenId=51285a98c8dbdf0324a8b3892b476f9138b3bdf7d3919725885e37cecaa34956&msg=hellofrompush
// Put your device token here (without spaces):
//$deviceToken = '51285a98c8dbdf0324a8b3892b476f9138b3bdf7d3919725885e37cecaa34956';
$deviceToken = $_GET['tokenId'];

// Put your private key's passphrase here:
$passphrase = 'abc123';

// Put your alert message here:
//$message = 'Hi Ravi From domain';
$message = urldecode($_GET['msg']);

////////////////////////////////////////////////////////////////////////////////

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'file_Push_Dist_Feb_11_17.pem');
//stream_context_set_option($ctx, 'ssl', 'local_cert', 'file_DistKey_Aug_25.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,
'ssl://gateway.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);

No comments:

Post a Comment