Skip to content

Instantly share code, notes, and snippets.

@trushang-ts
Created January 12, 2021 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trushang-ts/d10031cc4ae61e9738f6dfa3c8a91951 to your computer and use it in GitHub Desktop.
Save trushang-ts/d10031cc4ae61e9738f6dfa3c8a91951 to your computer and use it in GitHub Desktop.
Send APNs Testing Script
<form action="" method=post>
<table>
<tr>
<td colspan="2"><h3>Test Push-Notification</h3></td>
</tr>
<tr>
<td>Message</td>
<td><input type="text" name="notification" value="Hello.." required="required"></td>
</tr>
<tr>
<td>Device token</td>
<td><input type="text" name="devicetoken" required="required"></td><br>
</tr>
<tr>
<td align="right" colspan="3"><input type="submit" name="check" value="Send Push-Notification"></td>
</tr>
</table>
</form>
<?php
if (isset($_POST['check'])) {
if (!empty($_POST['notification'])) {
//$deviceToken = '65001dece0a196b3dc2d5a79e1260ac64c2cb5ff9f7ddc749f51f15eb8bc4b88';
$deviceToken = $_POST['devicetoken'];
$passphrase = 'admin';
$message = $_POST['notification'];
$ctx = stream_context_create();
$filename = 'EP_DEV_CK.pem'; //Upload on root with 400 permission
stream_context_set_option($ctx, 'ssl', 'local_cert', $filename);
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);
//$fp = stream_socket_client('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;
$body['aps'] = array(
'alert' => $message,
'sound' => 'default',
"content-available" => 1
);
$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);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment