All Products
Search
Document Center

Add notification listener

Last Updated: Feb 24, 2021

You can call this operation to add native notification listening.

addNotifyListener API usage instruction

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

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. });
  39. });
  40. });
  41. </script>

API

  1. AlipayJSBridge.call('addNotifyListener', {
  2. name, keep
  3. }, fn)

Input parameters

Name Type Description Required Default value
name String Notification name Y “”
keep String Indicates whether to keep listening to this notification within the lifecycle of ViewController N “true”
fn Function Callback function N

Output parameters

result: {}: The parameter carried by the callback function, corresponding to an event message.

Error codes

Error code Description
4 Unauthorized call
12 Listen to the same notification multiple times on the same page

Precautions

  • If an event being listened to is sent by the HTML5, add the NEBULANOTIFY_ prefix.
  • Before configuring addNotifyListener, perform a remove operation to avoid errors caused by multiple add operations.
  • If keep is set to false, listening is automatically unregistered after the event is triggered once.
  • The data returned by Android contains only one layer of key/value pairs. If the value is Object during postNotification, Android performs a JSON.stringify operation on the value. We recommend that you perform try{ JSON.parse } during usage.