/**
* Push a notification to Apple's notifications-server
*
* @param mixed $alert The message/dictionary to send.
* @param int $badge The number for the badge.
* @param string $sound The sound that should be played.
* @param array $extraDictionaries Extra dictionaries.
*/
public static function pushToAppleApp($alert, $badge = null, $sound = null, array $extraDictionaries = null)
{
// get ForkAPI-keys
$publicKey = self::get('fork.settings')->get('Core', 'fork_api_public_key', '');
$privateKey = self::get('fork.settings')->get('Core', 'fork_api_private_key', '');
// no keys, so stop here
if ($publicKey == '' || $privateKey == '') {
return;
}
// get all apple-device tokens
$deviceTokens = (array) self::getContainer()->get('database')->getColumn('SELECT s.value
FROM users AS i
INNER JOIN users_settings AS s
WHERE i.active = ? AND i.deleted = ? AND s.name = ? AND s.value != ?', array('Y', 'N', 'apple_device_token', 'N;'));
// no devices, so stop here
if (empty($deviceTokens)) {
return;
}
// init var
$tokens = array();
// loop devices
foreach ($deviceTokens as $row) {
// unserialize
$row = unserialize($row);
// loop and add
foreach ($row as $item) {
$tokens[] = $item;
}
}
// no tokens, so stop here
if (empty($tokens)) {
return;
}
// require the class
require_once PATH_LIBRARY . '/external/fork_api.php';
// create instance
$forkAPI = new \ForkAPI($publicKey, $privateKey);
try {
// push
$response = $forkAPI->applePush($tokens, $alert, $badge, $sound, $extraDictionaries);
if (!empty($response)) {
// get db
$db = self::getContainer()->get('database');
// loop the failed keys and remove them
foreach ($response as $deviceToken) {
// get setting wherein the token is available
$row = $db->getRecord('SELECT i.*
FROM users_settings AS i
WHERE i.name = ? AND i.value LIKE ?', array('apple_device_token', '%' . $deviceToken . '%'));
// any rows?
if (!empty($row)) {
// reset data
$data = unserialize($row['value']);
// loop keys
foreach ($data as $key => $token) {
// match and unset if needed.
if ($token == $deviceToken) {
unset($data[$key]);
}
}
// no more tokens left?
if (empty($data)) {
$db->delete('users_settings', 'user_id = ? AND name = ?', array($row['user_id'], $row['name']));
} else {
$db->update('users_settings', array('value' => serialize($data)), 'user_id = ? AND name = ?', array($row['user_id'], $row['name']));
}
}
}
}
} catch (Exception $e) {
if (self::getContainer()->getParameter('kernel.debug')) {
throw $e;
}
}
}