require_once dirname(__FILE__) . "/../../config/mobi_web_constants.php";
require_once "DaemonWrapper.php";
$daemon = new DaemonWrapper("apns_push");
$daemon->start($argv);
require_once 'apns_lib.php';
$apns_server = new ApplePushServersPool(APNS_CONNECTIONS_LIMIT);
// this is a daemon so loop forever
d_echo("push daemon activated");
while ($daemon->sleep(APNS_PUSH_REST_TIME)) {
    d_echo("waiting for messages to send...", False);
    db::ping();
    $messages = APNS_DB::get_unsent_notifications();
    while ($message = APNS_DB::fetch_notification($messages)) {
        $data = $message['payload'];
        $device_id = $message['device_id'];
        $device = APNS_DB::get_device($device_id);
        $module_name = get_module_name($message['tag']);
        // we only send to devices that have not been deactived
        // and we only send to enabled modules
        if ($device['active'] == 1 && APNS_DB::is_module_enabled($device_id, $module_name)) {
            // need to compute the number of unread messages
            // to be displayed on "badge" on the device
            $unreads = $device['unread_notifications'];
            // look for an old version of this message
            $old_position = array_search($message['tag'], $unreads);
            if ($old_position !== FALSE) {
                array_splice($unreads, $old_position, 1);
            }
            if (intval($message['has_badge'])) {
                $unreads[] = $message['tag'];
            } else {