Notification Events

How to handle push notification events, respond to user interactions, and manage active notifications.

The Airship SDK provides event listeners for when a push is received or a notification is interacted with.

Push Received

Listen for when a push notification is received:

const handle = await Airship.push.onPushReceived(event => {
  console.log('Push received:', event.pushPayload)
})

This event fires when a push notification arrives, regardless of whether the app is in the foreground or background.

Notification Response

Listen for when a user interacts with a notification:

const handle = await Airship.push.onNotificationResponse(event => {
  console.log('Notification tapped:', event)
  console.log('Action ID:', event.actionId)
  
  if (event.actionId === 'custom_action') {
    // Handle custom action
  }
})

This event fires when a user taps on a notification or a notification action button.

Managing Active Notifications

You can retrieve and clear notifications that are currently displayed in the notification center.

Get Active Notifications

Retrieve the list of currently displayed notifications:

const notifications = await Airship.push.getActiveNotifications()
console.log('Active notifications:', notifications)
 Note

On Android, this list only includes notifications sent through Airship.

Clear Notifications

Clear all notifications for the app:

await Airship.push.clearNotifications()

Clear a specific notification by identifier:

await Airship.push.clearNotification(identifier)
 Note

On Android, you can use this method to clear notifications outside of Airship. The identifier is in the format <tag>:<id>.