Skip to content

Instantly share code, notes, and snippets.

@tanmay27vats
Last active May 28, 2018 04:41
Show Gist options
  • Save tanmay27vats/601c203ad63cca30b8268317c5c416c6 to your computer and use it in GitHub Desktop.
Save tanmay27vats/601c203ad63cca30b8268317c5c416c6 to your computer and use it in GitHub Desktop.
Apple Push Notification service (APNs) - iOS Push Notification in PHP
<?php
// Provide the Host Information.
$vHost = 'gateway.sandbox.push.apple.com';
//$vHost = 'gateway.push.apple.com';
$vPort = 2195;
// Provide the Certificate and Key Data.
$vCert = 'Certificates.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
$vPassphrase = 'XXXXX';
// 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.
$vToken = 'F8B7E8CCA8BS34C7C621D73E702C1E6BFE02EAFCD51338F21FA5DC68A';
// The message that is to appear on the dialog. Message could be an Array (detailed array) or just a simple sentence.
// Message size should be Max 4KBs (as on 20th Jun, 2017).
$vAlert = 'You have new iOS (APNS) notification.';
// ============ OR =================
$vAlert = array(
"title" => "Game Request",
"body" => "Tanmay Vats wants to play poker.",
"action-loc-key" => "PLAY"
);
// The Badge Number for the Application Icon (integer >=0).
$vBadge = 1;
// Audible Notification Option.
$vSound = 'default';
// Create the message content that is to be sent to the device.
$vBody['aps'] = array (
'alert' => $vAlert,
'badge' => $vBadge,
'sound' => $vSound,
);
$vBody ['custom-key'] = "custom-value";
// Encode the body to JSON.
$vBody = json_encode ($vBody);
// Create the Socket Stream.
$vContext = stream_context_create ();
stream_context_set_option ($vContext, 'ssl', 'local_cert', $vCert);
// Remove this line if you would like to enter the Private Key Passphrase manually.
stream_context_set_option ($vContext, 'ssl', 'passphrase', $vPassphrase);
// Open the Connection to the APNS Server.
$vSocket = stream_socket_client ('ssl://'.$vHost.':'.$vPort, $error, $errstr, 30, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $vContext);
// Check if we were able to open a socket.
if (!$vSocket)
{
exit ("APNS Connection Failed: $error $errstr" . PHP_EOL);
}
// Build the Binary Notification.
$vMsg = chr (0) . chr (0) . chr (32) . pack ('H*', $vToken) . pack ('n', strlen ($vBody)) . $vBody;
// Send the Notification to the Server.
$vResult = fwrite ($vSocket, $vMsg, strlen ($vMsg));
if ($vResult)
{
echo 'Delivered Message to APNS' . PHP_EOL;
}
else
{
echo 'Could not Deliver Message to APNS' . PHP_EOL;
}
// Close the Connection to the Server.
fclose ($vSocket);
// For more details, please check on https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment