Notification Events
Listen for push notification events, handle user responses, and manage active notifications.
The Airship SDK provides event listeners for when a push is received or a notification is interacted with. Apps can use these events for custom push processing.
Notification Events
The SDK provides several event listeners for handling push notifications at different stages.
Push Received
Listen for when a push notification is received:
Airship.addListener(EventType.PushReceived, (event) => {
console.log('Push received:', event.pushPayload);
// Handle push received event
});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:
Airship.addListener(EventType.NotificationResponse, (response) => {
console.log('Notification tapped:', response);
console.log('Action ID:', response.actionId);
// Handle the notification response
if (response.actionId === 'custom_action') {
// Handle custom action
}
});This event fires when a user taps on a notification or a notification action button.
Registration Token Updates
Listen for when the push registration token is generated or updated:
Airship.addListener(EventType.PushTokenReceived, (event) => {
console.log('Push token:', event.pushToken);
// Send token to your backend if needed
});You can also retrieve the current token at any time:
const token = await Airship.push.getRegistrationToken();Notification Status Changes
Monitor changes to notification permission status:
Airship.addListener(EventType.PushNotificationStatusChangedStatus, (event) => {
console.log('Notification status changed:', event.status);
console.log('Is opted in:', event.status.isOptedIn);
});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);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);On Android, you can use this method to clear notifications outside of Airship. The identifier is in the format <tag>:<id>.
Silent Notifications
Silent notifications are push messages that do not present a notification to the user. These are typically used to briefly wake the app from a background state to perform processing tasks or fetch remote content.
We recommend that you thoroughly test your implementation to confirm that silent notifications do not generate any device notifications.
Platform Configuration
iOS: Set the content_available property to true in the iOS override object.
Android: All push messages are delivered in the background. By default, Airship will treat messages without an alert as silent.
Silent pushes do not have guaranteed delivery. Factors affecting delivery include battery life, WiFi connectivity, and the number of silent pushes sent within a recent time period. These metrics are determined solely by iOS/Android and APNs/FCM.
Silent push is best used for supplementing regular app behavior rather than providing critical functionality. For example, an app could use a silent push to pre-fetch new data ahead of time to reduce load times when the app is later launched by the user.
Handling Silent Notifications
Silent notifications will trigger the PushReceived event but will not display a notification to the user:
Airship.addListener(EventType.PushReceived, (event) => {
if (!event.pushPayload.alert) {
console.log('Silent push received');
// Perform background work
}
});Categories