Notification Events
Listen for push notification events, handle user responses, and manage active notifications.
The Airship SDK provides events for when a push is received or a notification is interacted with. Apps can use these events for custom push processing. Registering for an event is optional; the SDK will automatically launch the application without the need to set one.
Notification Events
Push Received
Listen for when a push notification is received. This event fires whether the app is in the foreground or background.
Airship.Shared.OnPushReceived += (PushMessage message) => {
// Handle push received event
};Push Opened
Listen for when a user taps a notification.
Airship.Shared.OnPushOpened += (PushMessage message) => {
// Handle notification open
};Registration Token Updates
Listen for when the push registration token is generated or updated.
Airship.Shared.OnPushTokenReceived += (string pushToken) => {
// Send the token to your backend if needed
};You can also retrieve the current token at any time.
string token = Airship.Shared.push.GetPushToken();Notification Status Changes
Monitor changes to the notification permission status.
Airship.Shared.OnNotificationStatusChanged += (PushNotificationStatus status) => {
Debug.Log("Is opted in: " + 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. GetActiveNotifications is asynchronous, so run it as a coroutine.
StartCoroutine(Airship.Shared.push.GetActiveNotifications((IEnumerable<PushMessage> notifications) => {
// Handle active notifications
}));On Android, this list only includes notifications sent through Airship.
Clear Notifications
Clear all notifications for the app:
Airship.Shared.push.ClearNotifications();Clear a specific notification by identifier:
Airship.Shared.push.ClearNotification(identifier);On Android, you can use this method to clear notifications outside of Airship. The identifier is in the format <id>:<tag>.
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
For iOS, set the content_available property to true in the iOS override object.
For Android, all push messages are delivered in the background, and by default Airship will treat messages without an alert as silent.
Pushes sent with the content_available property (iOS) or without an alert (Android) do not have guaranteed delivery. Factors affecting delivery include battery life, whether the device is connected to WiFi, and the number of silent pushes sent within a recent time period. These metrics are determined solely by iOS/Android and APNs/FCM. Therefore, this feature is best used for supplementing the regular behavior of the app rather than providing critical functionality. For instance, an app could use a silent push to pre-fetch new data ahead of time in order to reduce load times when the app is later launched by the user.
Handling Silent Notifications
Silent notifications trigger the OnPushReceived event but do not display a notification to the user.
Airship.Shared.OnPushReceived += (PushMessage message) => {
if (string.IsNullOrEmpty(message.Alert))
{
// Perform background work
}
};