Skip to content

Instantly share code, notes, and snippets.

@mako34
Last active May 3, 2020 13:04
Show Gist options
  • Save mako34/8551656 to your computer and use it in GitHub Desktop.
Save mako34/8551656 to your computer and use it in GitHub Desktop.
PHP test for iOS push notification
<?php
// Provide the Host Information.
$tHost = 'gateway.sandbox.push.apple.com';
$tPort = 2195;
// Provide the Certificate and Key Data.
$tCert = 'key.pem';
// Provide the Private Key Passphrase (alternatively you can keep this secrete
// and enter the key manually on the terminal -> remove relevant line from code).
// Replace XXXXX with your Passphrase
$tPassphrase = 'myPass*';
// Provide the Device Identifier (Ensure that the Identifier does not have spaces in it).
// Replace this token with the token of the iOS device that is to receive the notification.
$tToken = '0a32cbcc8464ec05ac3389424343ebca1cd567939b2f54892cd1dcb134';
// The message that is to appear on the dialog.
$tAlert = 'You have a LiveCode APNS Message';
// The Badge Number for the Application Icon (integer >=0).
$tBadge = 8;
// Audible Notification Option.
$tSound = 'default';
// The content that is returned by the LiveCode "pushNotificationReceived" message.
$tPayload = 'APNS Message Handled by LiveCode';
// Create the message content that is to be sent to the device.
$tBody['aps'] = array (
'alert' => $tAlert,
'badge' => $tBadge,
'sound' => $tSound,
);
$tBody ['payload'] = $tPayload;
// Encode the body to JSON.
$tBody = json_encode ($tBody);
// Create the Socket Stream.
$tContext = stream_context_create ();
stream_context_set_option ($tContext, 'ssl', 'local_cert', $tCert);
// Remove this line if you would like to enter the Private Key Passphrase manually.
stream_context_set_option ($tContext, 'ssl', 'passphrase', $tPassphrase);
// Open the Connection to the APNS Server.
$tSocket = stream_socket_client ('ssl://'.$tHost.':'.$tPort, $error, $errstr, 30, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $tContext);
// Check if we were able to open a socket.
if (!$tSocket)
exit ("APNS Connection Failed: $error $errstr" . PHP_EOL);
// Build the Binary Notification.
$tMsg = chr (0) . chr (0) . chr (32) . pack ('H*', $tToken) . pack ('n', strlen ($tBody)) . $tBody;
// Send the Notification to the Server.
$tResult = fwrite ($tSocket, $tMsg, strlen ($tMsg));
if ($tResult)
echo 'Delivered Message to APNS' . PHP_EOL;
else
echo 'Could not Deliver Message to APNS' . PHP_EOL;
// Close the Connection to the Server.
fclose ($tSocket);
?>
@shrutiupari
Copy link

Warning: stream_socket_client(): unable to connect to ssl://gateway.push.apple.com:2195 (Unable to find the socket transport &quot;ssl&quot; - did you forget to enable it when you configured PHP?) in E:\wamp\www\yourbillz\PHP_push_iOS.php

I'm getting the above error

@DonaldLaborde
Copy link

DonaldLaborde commented Mar 1, 2018

This PHP test for iOS push notification worked perfectly till march 2017 where Apple made a change in connecting to APNS server. Does someone know how to modify this to make it run again ? Any idea would be appreciate. Thanks in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment