All Products
Search
Document Center

SuperApp:Communication

Last Updated:Oct 25, 2024

This topic describes the JavaScript API of WVStandardEventCenter. You can refer to this topic when you create HTML5 apps or Miniapps. You can use the JavaScript API of WVStandardEventCenter to send events from HTML5 apps or MiniApps to Native.

WVStandardEventCenter.postNotificationToNative

Sends events from HTML5 apps or MiniApps to Native. The event name can be changed based on your business requirements.

iOS event listeners

Uses the Notification Center to listen for events.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myEventListener) name:eventName object:nil]

- (void)myEventListener:(NSNotification *)notification {
        // Specify to obtain the event parameters from notification.userInfo. 
}

Android event listeners

@import android.taobao.windvane.service;
@import android.taobao.windvane.standardmodal

// Implement a listener. 
public class JsEventListener implements WVEventListener {
        @Override
        public WVEventResult onEvent(int id, WVEventContext ctx, Object... obj) {
                // All events are sent to the listener. WVEventId.H5TONATIVE_EVENT indicates the events sent by HTML5 apps or MiniApps. 
                if (id == WVEventId.H5TONATIVE_EVENT) {
                        if (obj[0] instanceof String) {
                                String params = (String)obj[0];
                                // params is a JSON string that contains two parameters – event and param. You need to deserialize the string. 
                        }
                }
                return null;
        }
}

// Register a listener. 
WVEventService.getInstance().addEventListener(new JsEventListener());

Input parameters

  • [string]event: the name of the event to be sent.

  • [object]param: (optional) the parameter of the event to be sent.

Callback parameters

No callback parameters exist. If the phone beeps, the success callback is invoked. Otherwise, the failure callback is invoked.

var params = {
        event: 'eventName',
        param: {
                // The data of the event to be sent. 
        }
};
window.WindVane.call('WVStandardEventCenter','postNotificationToNative', params, function(e) {
        alert('success');
}, function(e) {
        alert('failure: ' + JSON.stringify(e));
});