Skip to content

Instantly share code, notes, and snippets.

@hnq90
Last active November 11, 2017 22:22
Show Gist options
  • Save hnq90/26011663b0d59d9f7c38 to your computer and use it in GitHub Desktop.
Save hnq90/26011663b0d59d9f7c38 to your computer and use it in GitHub Desktop.
APNS-CakePHP
<?php
###
# Using APNS-PHP Library <https://github.com/duccio/ApnsPHP>
###
App::uses("String", "Utility");
App::uses("Security", "Utility");
App::import("Vendor", "ApnsPHP/Autoload");
require __DIR__ . '/PushNotificationInterface.php';
class IOSPushNotification implements PushNotificationInterface
{
/**
* App Certificate Path
*
* @var string
*/
private const APP_CERT_PATH = "/cert/app_bundle_cert.pem";
/**
* Root Certificate Authority Cert Path
*
* @var string
*/
private const ENTRUST_CERT_PATH = "/cert/entrust.cert";
/**
* Environment
*
* @var int
*/
private $env;
/**
* App Certificate
*
* @var string
*/
private $app_cert_path;
/**
* Entrust Certificate
*
* @var string
*/
private $entrust_cert_path;
/**
* APNS Identifier
*
* @var string
*/
private $identifier = "APNS";
/**
* Timeout
*
* @var int
*/
private $expiry = 30;
/**
* Sound of notification
*
* @var string
*/
private $sound = "default";
/**
* Override construct function to setup APNS
*
* @author HuyNQ <huynq@rikkeisoft.com>
* @version 1.0
*/
public function __construct() {
// Check env of project and set env
if(empty($this->env)) {
$this->env = (Configure::read("debug") > 0)
? ApnsPHP_Abstract::ENVIRONMENT_SANDBOX
: ApnsPHP_Abstract::ENVIRONMENT_PRODUCTION;
}
// Get app cert and entrust cert
$this->app_cert_path = WWW_ROOT . self::APP_CERT_PATH;
$this->entrust_cert_path = WWW_ROOT . self::ENTRUST_CERT_PATH;
}
/**
* @param string $device_token Device Token
* @param string $text Content of notification
* @param array $options Options of push notification
*
* @return bool Push notification successfully or failed
* @throws ApnsPHP_Exception
* @throws ApnsPHP_Message_Exception
* @throws ApnsPHP_Push_Exception
* @throws Exception
*
* @author HuyNQ <huynq@rikkeisoft.com>
* @version 1.0
*/
public function pushOnce($device_token, $text, $options = []) {
try {
// Instantiate a new ApnsPHP_Push object
$push = new ApnsPHP_Push($this->env, $this->app_cert_path);
$push->setProviderCertificatePassphrase("Passphrase");
// Set the Root Certificate Authority to verify the Apple remote peer
$push->setRootCertificationAuthority($this->entrust_cert_path);
// Connect to the Apple Push Notification Service
$push->connect();
// Instantiate a new Message with a single recipient
$message = new ApnsPHP_Message($device_token);
// Set the alert message to display to the user.
$message->setText($text);
// Set a custom identifier. To get back this identifier use the getCustomIdentifier() method
// over a ApnsPHP_Message object retrieved with the getErrors() message.
$message->setCustomIdentifier(isset($options["identifier"])
? $options["identifier"] : $this->identifier);
// Set badge icon to "1"
$message->setBadge(1);
// Play the sound
$message->setSound(isset($options["sound"])
? $options["sound"] : $this->sound);
// Set a custom property
// $message->setCustomProperty('acme2', array('bang', 'whiz'));
// Set another custom property
// $message->setCustomProperty('acme3', array('bing', 'bong'));
// This message will expire in N seconds if not successful delivered.
$message->setExpiry(isset($options["expiry"])
? $options["expiry"] : $this->expiry);
// Add the message to the message queue
$push->add($message);
// Send message in the message queue
$push->send();
// Disconnect from the Apple Push Notification Service
$push->disconnect();
// Check error
$error = $push->getErrors();
if(empty($error)) {
return true;
}
return false;
} catch (ApnsPHP_Exception $e) {
return false;
}
}
/**
* Push notification to many devices at once
*
* @param array $lists List of device token and content
* @param array $options Options of push notification
*
* @return bool Push notification successfully or failed
* @throws ApnsPHP_Exception
* @throws ApnsPHP_Message_Exception
* @throws ApnsPHP_Push_Exception
* @throws Exception
*
* @author HuyNQ <huynq@rikkeisoft.com>
* @version 1.0
*/
public function pushMany($lists, $options = []) {
try {
// Instantiate a new ApnsPHP_Push object
$push = new ApnsPHP_Push($this->env, $this->app_cert_path);
// Set passphrase of pem file
$push->setProviderCertificatePassphrase("Passphrase");
// Set the Root Certificate Authority to verify the Apple remote peer
$push->setRootCertificationAuthority($this->entrust_cert_path);
// Connect to the Apple Push Notification Service
$push->connect();
foreach ($lists as $device_token => $text) {
// Instantiate a new Message with a single recipient
$message = new ApnsPHP_Message($device_token);
// Set the alert message to display to the user.
$message->setText($text);
// Set a custom identifier. To get back this identifier use the getCustomIdentifier() method
// over a ApnsPHP_Message object retrieved with the getErrors() message.
$message->setCustomIdentifier(isset($options["identifier"])
? $options["identifier"] : $this->identifier);
// Set badge icon to "1"
$message->setBadge(1);
// Play the sound
$message->setSound(isset($options["sound"])
? $options["sound"] : $this->sound);
// Set a custom property
// $message->setCustomProperty('acme2', array('bang', 'whiz'));
// Set another custom property
// $message->setCustomProperty('acme3', array('bing', 'bong'));
// This message will expire in N seconds if not successful delivered.
$message->setExpiry(isset($options["expiry"])
? $options["expiry"] : $this->expiry);
// Add the message to the message queue
$push->add($message);
}
// Send message in the message queue
$push->send();
// Disconnect from the Apple Push Notification Service
$push->disconnect();
// Check error
$error = $push->getErrors();
if (empty($error)) {
return true;
}
return false;
} catch (ApnsPHP_Exception $e) {
return false;
}
}
/**
* Get list feedback from Apple server
*
* @return array Array of Device Token from Apple Server
* @throws ApnsPHP_Exception
* @throws Exception
*
* @author HuyNQ <huynq@rikkeisoft.com>
* @version 1.0
*/
public function getFeedback() {
try {
// Instantiate a new ApnsPHP_Feedback object
$feedback = new ApnsPHP_Feedback($this->env, $this->app_cert_path);
$feedback->setProviderCertificatePassphrase("Passphrase");
// Connect to the Apple Push Notification Feedback Service
$feedback->connect();
// Receives feedback tuples from Apple Push Notification Service feedback.
$error_devices = $feedback->receive();
// Disconnect from the Apple Push Notification Feedback Service
$feedback->disconnect();
// Return list of device token
return $error_devices;
} catch (ApnsPHP_Exception $e) {
return [];
}
}
}
@fxdyx
Copy link

fxdyx commented Jan 26, 2016

private const ??

@mdunham
Copy link

mdunham commented Nov 11, 2017

Can I see the PushNotificationInterface.php file?

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