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 subscription = Airship.push.onPushReceived((event) => {
  console.log('Push received:', event.pushPayload)
  console.log('Is foreground:', event.isForeground)
})

// Later, to cancel the subscription
subscription.cancel()

This event fires when a push notification arrives. On iOS, it fires regardless of whether the app is in the foreground or background. On Android, this event only fires in the foreground.

Notification Response

Listen for when a user interacts with a notification:

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

// Later, to cancel the subscription
subscription.cancel()

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:

Airship.push.getActiveNotifications((notifications) => {
  console.log('Active notifications:', notifications)
})
 Note

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

Clear Notifications

Clear all notifications for the app:

Airship.push.clearNotifications()

Clear a specific notification by identifier:

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>.