All Products
Search
Document Center

Distribute a notification

Last Updated: Feb 26, 2021

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

  1. AlipayJSBridge.call('postNotification', {
  2. name:'fortest',
  3. data:{}
  4. }, function (result) {
  5. console.log(result);
  6. });

Code sample

The following example shows the basic functions.

  1. <h1>Click the following button to perform a test.</h1>
  2. <p>In this example, multiple operations are called on the same page. This API supports communication among different apps.</p>
  3. <a href="#" class="btn start">Start listening.</a>
  4. <a href="#" class="btn stop">Stop listening.</a>
  5. <a href="#" class="btn send">Send a notification.</a>
  6. <script>
  7. function callback(e){
  8. alert(JSON.stringify(e));
  9. };
  10. function ready(callback) {
  11. // Call JS Bridge if it has been injected.
  12. if (window.AlipayJSBridge) {
  13. callback && callback();
  14. } else {
  15. // Listen to the injection event if it has not been injected.
  16. document.addEventListener('AlipayJSBridgeReady', callback, false);
  17. }
  18. }
  19. ready(function(){
  20. document.querySelector('.start').addEventListener('click', function(){
  21. AlipayJSBridge.call('addNotifyListener', {
  22. name:'NEBULANOTIFY_TEST_EVENT' // Events sent by the HTML5 must be listened to by adding the prefix `NEBULANOTIFY_`.
  23. }, callback);
  24. });
  25. document.querySelector('.stop').addEventListener('click', function(){
  26. AlipayJSBridge.call('removeNotifyListener', {
  27. name:'NEBULANOTIFY_TEST_EVENT' // Events sent by the HTML5 must be listened to by adding the prefix `NEBULANOTIFY_`.
  28. }, function(e){
  29. alert(JSON.stringify(e));
  30. });
  31. });
  32. document.querySelector('.send').addEventListener('click', function(){
  33. AlipayJSBridge.call('postNotification', {
  34. name:'TEST_EVENT' // Events sent by the HTML5 must be listened to by adding the prefix `NEBULANOTIFY_`.
  35. data: {
  36. hello: 'world'
  37. }
  38. }, function(e){
  39. alert(JSON.stringify(e));
  40. });
  41. });
  42. });
  43. </script>

API

  1. AlipayJSBridge.call('postNotification', {
  2. name, data
  3. }, 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