JSAPI

Since 9.5.1

postNotification

You can use this API to send notification events from Javascript to native code. A prefix NEBULANOTIFY_ is required if the events are emitted from H5 pages.

It is allowed to call postNotification with empty data, and it is implemented by using LocalBroadcastManager in Android platform.

Usage

AlipayJSBridge.call('postNotification', {
  name:'fortest',
  data:{}
}, function (result) {
  console.log(result);
});

Example

Basic Function

<h1>Please click on the buttons below</h1>
<p>Although the following test is conducted in one page, you can use this API to listen to events happens in other pages</p>

<a href="#" class="btn start">Start Listening</a>
<a href="#" class="btn stop">Stop Listening</a>
<a href="#" class="btn send">Notify</a>
<script>
function callback(e){
  alert(JSON.stringify(e));
};

function ready(callback) {
  // Invoke directly if JSBridge is already injected
  if (window.AlipayJSBridge) {
    callback && callback();
  } else {
    // Otherwise listen to `AlipayJSBridgeReady` event
    document.addEventListener('AlipayJSBridgeReady', callback, false);
  }
}
ready(function(){
  document.querySelector('.start').addEventListener('click', function(){
    AlipayJSBridge.call('addNotifyListener', {
      name:'NEBULANOTIFY_TEST_EVENT' 
      // H5 event name must be prefixed with NEBULANOTIFY_
    }, callback);
  });

  document.querySelector('.stop').addEventListener('click', function(){
    AlipayJSBridge.call('removeNotifyListener', {
      name:'NEBULANOTIFY_TEST_EVENT'       // H5 event name must be prefixed with NEBULANOTIFY_
    }, function(e){
      alert(JSON.stringify(e));
    });
  });

  document.querySelector('.send').addEventListener('click', function(){
    AlipayJSBridge.call('postNotification', {
      name:'NEBULANOTIFY_TEST_EVENT',       // H5 event name must be prefixed with NEBULANOTIFY_
      data: {
        hello: 'world'
      }
    });
  });
});
</script>

API

AlipayJSBridge.call('postNotification', {
  name, data
}, fn)

Input Parameters

NameTypeDescriptionMandatory
namestringName of eventY
dataobjectEvent data to be sent to native code, all properties in this json object will be converted to String format in Android platform, please pay attention to the data format when deserialise the data object in native codeN
fnfunctionCallback functionN

Output Parameters

Parameter result:{} that passed to the callback function

NameTypeDescription
successboolTrue if post notification is successful

Errors

ErrorDescription
4No permission