阿里云跨平台 DevOps向Native发送事件 WVStandardEventCenter

WVStandardEventCenter.postNotificationToNative

H5 向 Native 发送事件,具体的事件名称可以是与 Native 业务方商议一致的任意名称。

iOS 事件监听用法

使用标准的消息中心监听事件:

  1. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myEventListener) name:eventName object:nil]
  2. - (void)myEventListener🙁NSNotification *)notification {
  3. // 事件参数可以从 notification.userInfo 中获取。
  4. }

Android 事件监听用法

  1. @import android.taobao.windvane.service;
  2. @import android.taobao.windvane.standardmodal
  3. // 首先,需要自己实现监听用的 Listener。
  4. public class JsEventListener implements WVEventListener {
  5. @Override
  6. public WVEventResult onEvent(int id, WVEventContext ctx, Object... obj) {
  7. // 所有事件均派发到这里,WVEventId.H5TONATIVE_EVENT 表示 H5 发送过来的事件。
  8. if (id == WVEventId.H5TONATIVE_EVENT) {
  9. if (obj[0] instanceof String) {
  10. String params = (String)obj[0];
  11. // 这里的 params 是包含 event 和 param 的 JSON String,需要自行反序列化。
  12. }
  13. }
  14. return null;
  15. }
  16. }
  17. // 然后,注册监听器。
  18. WVEventService.getInstance().addEventListener(new JsEventListener());

输入参数

  • [string] event – 要发送的事件名称。
  • [object] param – [可选]要发送的事件参数。

回调参数

无回调参数,如果成功蜂鸣,则进入 success 回调,否则进入 failure 回调。

  1. var params = {
  2. event: 'eventName',
  3. param: {
  4. // 事件要传递的数据。
  5. }
  6. };
  7. window.WindVane.call('WVStandardEventCenter','postNotificationToNative', params, function(e) {
  8. alert('success');
  9. }, function(e) {
  10. alert('failure: ' + JSON.stringify(e));
  11. });

原创文章,作者:网友投稿,如若转载,请注明出处:https://www.cloudads.cn/archives/33676.html

发表评论

登录后才能评论