Todos os produtos
Search
Central de documentação

Mobile Platform as a Service:Configure a preset ad space

Última atualização: Jun 28, 2026

Obtém informações de espaços de anúncio para uma página H5 desenvolvida no contêiner H5 do mPaaS. É possível obter um único espaço de anúncio ou vários em lote. Para interceptar as informações do espaço e do anúncio antes da exibição, use um ActionExecutor.

Antes de começar

Se você ainda não conhece o componente de entrega inteligente, configure os espaços de anúncio H5 diretamente pelo console do componente (lado do servidor). Para mais informações, consulte Crie um espaço de anúncio.

Obter um único espaço de anúncio

Chame getCdpSpaceInfo na página H5 para obter as informações do espaço de anúncio:

AlipayJSBridge.call('getCdpSpaceInfo', {
  spaceCode: 'space-code1',
  extInfo: {
    tradeNo: '123'
  },
  immediately: false,
  multiCallback: true
}, function (result) {
  console.log(result);
});

Parâmetros:

  • spaceCode: código do espaço de anúncio. Obtenha este código no backend.

  • extInfo: informações estendidas no formato chave-valor.

  • immediately: parâmetro booleano opcional. Defina como true para buscar dados diretamente do servidor, ignorando o cache. O valor padrão é false.

  • multiCallback: defina como true para receber dois callbacks sequenciais por chamada. Defina como false para receber apenas um callback.

Nota

Quando multiCallback é false ou omitido, a lógica do serviço recebe um único callback de resultado. Se houver um mecanismo de cache ativo, o anúncio pode não aparecer na primeira visita do usuário, surgindo apenas na segunda. Com multiCallback definido como true, a entrega inteligente aciona dois callbacks: o primeiro retorna os dados em cache (se disponíveis) e o segundo traz o resultado da Remote Procedure Call (RPC).

Obter espaços de anúncio em lote

Chame getCdpSpaceInfos na página H5 para obter múltiplos espaços de anúncio em uma única solicitação:

AlipayJSBridge.call('getCdpSpaceInfos', {
  spaceCodes: ['space-code1', 'space-code2'],
  extInfo: {
    tradeNo: '123'
  },
  immediately: false,
  multiCallback: true
}, function (result) {
  console.log(result);
});
/**
 * Cdp ad service interface
 *
 */
public abstract class CdpAdvertisementService extends ExternalService {

    /**
     * Initializes all ad information.
     *
     * @param extInfo  Extended information.
     * @param callBack The callback.
     */
    public abstract void initialized(Map<String, String> extInfo, IAdGetSpaceInfoCallBack callBack);

    /**
     * Queries an ad space by its ID. The query result is returned asynchronously through the onSuccess(SpaceInfo spaceInfo) callback.
     * If no local cache exists, the result of the RPC query is returned in one callback.
     * If a local cache exists and has not expired, the local query result is returned in one callback.
     * If a local cache exists and has expired, the local query result is returned in one callback, and the updated result is returned in another callback after the RPC is complete.
     * The onFail() interface is called only if the RPC fails.
     *
     * @param spaceCode The ad space ID. You need to request it from the delivery platform.
     * @param callback  The callback interface.
     */
    public abstract void getSpaceInfoByCode(String spaceCode, IAdGetSingleSpaceInfoCallBack callback);

    /**
     * Queries an ad space by its ID. The query result is returned asynchronously through the onSuccess(SpaceInfo spaceInfo) callback.
     * If no local cache exists, the result of the RPC query is returned in one callback.
     * If a local cache exists and has not expired, the local query result is returned in one callback.
     * If a local cache exists and has expired, the local query result is returned in one callback, and the updated result is returned in another callback after the RPC is complete.
     * The onFail() interface is called only if the RPC fails.
     *
     * @param spaceCode   The ad space ID. You need to request it from the delivery platform.
     * @param extInfo     Extended parameters.
     * @param immediately Returns only the RPC result.
     * @param callback    The callback interface.
     */
    public abstract void getSpaceInfoByCode(String spaceCode, Map<String, String> extInfo, boolean immediately, final IAdGetSingleSpaceInfoCallBack callback);

    /**
     * Queries ad spaces in batches by a list of ad space IDs. The query result is returned asynchronously through the onSuccess(List<SpaceInfo> adSpaceInfo) callback.
     * If no local cache exists, the result of the RPC query is returned in one callback.
     * If a local cache exists and has not expired, the local query result is returned in one callback.
     * If a local cache exists and has expired, the local query result is returned in one callback, and the updated result is returned in another callback after the RPC is complete.
     * The onFail(List<String> adSpaceCodes) interface is called only if the RPC fails.
     *
     * @param spaceCodeList A list of ad space IDs. You need to request them from the delivery platform.
     * @param extInfo       Extended parameters.
     * @param immediately   Returns only the RPC result.
     * @param callback      The callback interface.
     */
    public abstract void batchGetSpaceInfoByCode(List<String> spaceCodeList, Map<String, String> extInfo, boolean immediately, final IAdGetSpaceInfoCallBack callback);

    /**
     * TODO Reserved H5 interface.
     * Queries and displays an ad. Currently, this is only called by AdH5Plugin.
     *
     * @param activity   The current page.
     * @param parentView The parent view.
     * @param url        The URL.
     * @param h5Param    The parameter.
     */
    public abstract void checkAndShowAdInH5(final Activity activity, ViewGroup parentView, String url, String h5Param);

    /**
     * Removes the ad view for a specified spaceCode.
     *
     * @param activity  The page from which to remove the ad.
     * @param spaceCode The ad ID.
     */
    public abstract void removeAdvertisement(Activity activity, String spaceCode);

    /**
     * Gets the action executor. Returns null if not set.
     *
     * @return The action executor.
     */
    public abstract ActionExecutor getActionExecutor();

    /**
     * Sets the action executor.
     *
     * @param executor The action executor.
     */
    public abstract void setActionExecutor(ActionExecutor executor);

    /**
     * Sets the user ID.
     *
     * @param userId The user ID.
     */
    public abstract void setUserId(String userId);

    /**
     * Gets the user ID.
     *
     * @return The user ID.
     */
    public abstract String getUserId();

    /**
     * The callback class for getting ad spaces.
     */
    public interface IAdGetSpaceInfoCallBack {
        /**
         * Successfully retrieved ad space information.
         *
         * @param adSpaceInfo A list of ad spaces.
         */
        void onSuccess(List<SpaceInfo> adSpaceInfo);

        /**
         * Failed to retrieve ad space information.
         *
         * @param adSpaceCodes A list of requested ad space codes.
         */
        void onFail(List<String> adSpaceCodes);
    }

    /**
     * The callback class for getting a single ad space.
     */
    public interface IAdGetSingleSpaceInfoCallBack {
        /**
         * Successfully retrieved ad space information.
         *
         * @param spaceInfo The ad space information.
         */
        void onSuccess(SpaceInfo spaceInfo);

        /**
         * Failed.
         */
        void onFail();
    }
}

ActionExecutor

O ActionExecutor intercepta as informações do espaço e do anúncio antes da exibição. Retorne true em interceptAction para suprimir o espaço de anúncio e seu conteúdo. Retorne false para permitir que o cliente os visualize normalmente.

/**
 * Action handler
 *
 */
public interface ActionExecutor {

    /**
     * Specifies whether to intercept the action.
     * @param spaceInfo The ad space information.
     * @param spaceObjectInfo The ad information.
     * @param url The action URL.
     * @return Returns true to intercept the action, or false to not intercept it.
     */
    boolean interceptAction(final SpaceInfo spaceInfo, final SpaceObjectInfo spaceObjectInfo, final String url);

    /**
     * Executes the action.
     *
     * @param spaceInfo The ad space information.
     * @param spaceObjectInfo The ad information.
     * @param url The action URL.
     * @return Returns 1 for success. Other values indicate an error.
     */
    int executeAction(final SpaceInfo spaceInfo, final SpaceObjectInfo spaceObjectInfo, final String url);
}