public static function get_user_devices($appid, $userid = 0) {
        global $USER;

        $params = self::validate_parameters(
            self::get_user_devices_parameters(),
            array(
                'appid' => $appid,
                'userid' => $userid,
            )
        );

        $context = context_system::instance();
        self::validate_context($context);

        if (empty($params['userid'])) {
            $user = $USER;
        } else {
            $user = core_user::get_user($params['userid'], '*', MUST_EXIST);
            core_user::require_active_user($user);
            // Allow only admins to retrieve other users devices.
            if ($user->id != $USER->id) {
                require_capability('moodle/site:config', $context);
            }
        }

        $warnings = array();
        $devices = array();
        // Check if mobile notifications are enabled.
        if (!self::is_system_configured()) {
            $warnings[] = array(
                'item' => 'user',
                'itemid' => $user->id,
                'warningcode' => 'systemnotconfigured',
                'message' => 'Mobile notifications are not configured'
            );
        } else {
            // We catch exceptions here because get_user_devices may try to connect to Airnotifier.
            try {
                $manager = new message_airnotifier_manager();
                $devices = $manager->get_user_devices($appid, $user->id);
            } catch (Exception $e) {
                $warnings[] = array(
                    'item' => 'user',
                    'itemid' => $user->id,
                    'warningcode' => 'errorgettingdevices',
                    'message' => $e->getMessage()
                );
            }
        }

        return array(
            'devices' => $devices,
            'warnings' => $warnings
        );
    }