The frontend can send notifications to the client through this API. The HTML5 adds a prefix NEBULANOTIFY_
to the transferred name tag and sends it as a notification name.
In versions earlier than Android 10.1.0, the data field of postNotification
cannot be left empty so that it can be accepted by addNotifyListener
. However, this restriction has been removed in 10.1.60. addNotifyListener
can accept an empty data record.
Android sends broadcasts through LocalBroadcastManager
. The broadcasts can be listened to in the format of NEBULANOTIFY_
+ name
.
postNotification API usage instruction
AlipayJSBridge.call('postNotification', {
name:'fortest',
data:{}
}, function (result) {
console.log(result);
});
Code sample
The following example shows the basic functions.
<h1>Click the following button to perform a test.</h1>
<p>In this example, multiple operations are called on the same page. This API supports communication among different apps.</p>
<a href="#" class="btn start">Start listening.</a>
<a href="#" class="btn stop">Stop listening.</a>
<a href="#" class="btn send">Send a notification.</a>
<script>
function callback(e){
alert(JSON.stringify(e));
};
function ready(callback) {
// Call JS Bridge if it has been injected.
if (window.AlipayJSBridge) {
callback && callback();
} else {
// Listen to the injection event if it has not been injected.
document.addEventListener('AlipayJSBridgeReady', callback, false);
}
}
ready(function(){
document.querySelector('.start').addEventListener('click', function(){
AlipayJSBridge.call('addNotifyListener', {
name:'NEBULANOTIFY_TEST_EVENT' // Events sent by the HTML5 must be listened to by adding the prefix `NEBULANOTIFY_`.
}, callback);
});
document.querySelector('.stop').addEventListener('click', function(){
AlipayJSBridge.call('removeNotifyListener', {
name:'NEBULANOTIFY_TEST_EVENT' // Events sent by the HTML5 must be listened to by adding the prefix `NEBULANOTIFY_`.
}, function(e){
alert(JSON.stringify(e));
});
});
document.querySelector('.send').addEventListener('click', function(){
AlipayJSBridge.call('postNotification', {
name:'TEST_EVENT' // Events sent by the HTML5 must be listened to by adding the prefix `NEBULANOTIFY_`.
data: {
hello: 'world'
}
}, function(e){
alert(JSON.stringify(e));
});
});
});
</script>
API
AlipayJSBridge.call('postNotification', {
name, data
}, fn)
Input parameters
Name | Type | Description | Required | Default value |
---|---|---|---|---|
name | String | Notification name | Y | “” |
data | object | Information notified to the client. Android will traverse all JSON data and convert the value of each item into string type for transmission. Ensure that the information is compatible with the data format. | N | - |
fn | function | Callback function | N | - |
Output parameters
result: {success}
: the parameter transferred in the callback function.
Name | Type | Description |
---|---|---|
success | bool | Whether the message is successfully sent |
Error code
Error code | Description |
---|---|
4 | Unauthorized call |