All Products
Search
Document Center

Send custom message to custom View

Last Updated: Jun 02, 2021

The function of sending custom message to custom View is only supported in mPaaS 10.1.68.29 and later. When the current baseline version is lower than 10.1.68.29, please refer to mPaaS 10.1.68 upgrade guide to upgrade the baseline version to 10.1.68.29.

Create custom View context

'mpaas-map' is the value of id in the mini program tab, fill in your id here.

 
  1. this.context = my.createMpaasCustomComponentContext('mpaas-map');

Send message to the client by context

actiontype is the name of the message event received by the client and data is the extension parameter.

 
  1. this.context.postMessage({
  2. actionType: 'setColor',
  3. data: {
  4. "color": "#FF00FF00"
  5. }
  6. });

Client receives message and processes native View

In the onReceivedMessage method, the first parameter is the actionType passed in by the mini program, the second parameter is the extension parameter passed in, and the third parameter is bridge (can be ignored).

 
  1. public class MyTestEmbedView extends MPBaseEmbedView {
  2. ···
  3. @Override
  4. public void onReceivedMessage(String actionType, JSONObject data, H5BridgeContext bridgeContext) {
  5. LoggerFactory.getTraceLogger().debug(TAG, "onReceivedMessage, actionType: " + actionType + ", data: " + data.toString());
  6. if("setColor".equals(actionType)){
  7. mRealView.setColor(Color.parseColor(data.getString("color")));
  8. }
  9. }
  10. ···
  11. }