$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 {
                $data->noBadge = 1;
            }
            $data->aps->badge = count($unreads);
            $data->tag = $message['tag'];
            d_echo("sending message: " . json_encode($data));
            $apns_server->send_push_notification($data, $device['device_token']);
            APNS_DB::record_notification_attempt(TRUE, $message['id']);
        } else {
            APNS_DB::record_notification_attempt(FALSE, $message['id']);
        }
        // backend needs to keep track of which messages are not yet read
        APNS_DB::save_unread_notifications($device['device_id'], $unreads);
    }
    APNS_DB::close_notifications($messages);
}
$apns_server->close();
$daemon->stop();
function get_module_name($tag)
{
    $parts = split(":", $tag);
    return $parts[0];
}