Customize Notifications
Configure iOS notification options, badges, foreground presentation, and silent notifications.
Customize how notifications appear and behave on iOS and Android platforms.
iOS Notification Options
By default, the Airship SDK will request Alert, Badge, and Sound notification options for remote notifications. You can customize these options by setting them before enabling user notifications:
Airship.push.iOS.setNotificationOptions([
IOSNotificationOption.alert,
IOSNotificationOption.badge,
IOSNotificationOption.sound,
]);
// Then enable user notifications
await Airship.push.setUserNotificationsEnabled(true);Available Options
IOSNotificationOption.alert- Display alertsIOSNotificationOption.badge- Update the app badgeIOSNotificationOption.sound- Play soundsIOSNotificationOption.carPlay- Display notifications in CarPlayIOSNotificationOption.criticalAlert- Display critical alerts (requires special entitlement)IOSNotificationOption.providesAppNotificationSettings- Indicates the app has custom notification settingsIOSNotificationOption.provisional- Enables provisional authorization
Provisional Authorization
Apps can request provisional authorization along with the usual notification options. When requesting provisional authorization, apps do not need to prompt the user for permission initially, and notifications will be delivered in a non-interruptive manner to the Notification Center until the user explicitly chooses to keep delivering messages either prominently or quietly.
Airship.push.iOS.setNotificationOptions([
IOSNotificationOption.alert,
IOSNotificationOption.badge,
IOSNotificationOption.sound,
IOSNotificationOption.provisional,
]);
// Enable notifications (no prompt will be shown)
await Airship.push.setUserNotificationsEnabled(true);Provisional notifications appear in Notification Center but not as banners or with sounds. Users can then choose to keep or turn off notifications from Notification Center.
iOS Foreground Presentation Options
When a push is received in the foreground on iOS, how the notification is displayed to the user is controlled by foreground presentation options. By default, the SDK will not set any options so the notification will be silenced.
Airship.push.iOS.setForegroundPresentationOptions([
IOSForegroundPresentationOption.banner,
IOSForegroundPresentationOption.list,
IOSForegroundPresentationOption.sound,
]);Available Presentation Options
IOSForegroundPresentationOption.sound- Play the sound associated with the notificationIOSForegroundPresentationOption.badge- Apply the notification’s badge value to the app’s iconIOSForegroundPresentationOption.list- Show the notification in Notification Center (and as a banner on iOS 13 and older)IOSForegroundPresentationOption.banner- Present the notification as a banner (and in Notification Center on iOS 13 and older)
If no foreground presentation options are set, notifications received while the app is in the foreground will be silently received without displaying any UI to the user.
iOS Badge Management
The badge on iOS presents a counter on top of the application icon. You can control this directly through Airship:
// Set badge number
await Airship.push.iOS.setBadge(20);
// Get current badge number
int currentBadge = await Airship.push.iOS.badge;
print('Current badge: $currentBadge');
// Reset badge
await Airship.push.iOS.resetBadge();Auto-Badge
Auto-badge automatically increments the badge when a notification is received and decrements it when the notification is cleared:
// Enable auto-badge
await Airship.push.iOS.setAutoBadgeEnabled(true);
// Check if auto-badge is enabled
bool isEnabled = await Airship.push.iOS.isAutoBadgeEnabled();When using auto-badge, only modify the badge value through Airship methods to ensure the value stays in sync.
iOS Authorized Notification Settings
Check which notification settings the user has authorized:
// Get authorized notification settings
List<IOSAuthorizedNotificationSetting> settings =
await Airship.push.iOS.authorizedNotificationSettings;
print('Authorized settings: $settings');
// Get authorization status
IOSAuthorizedNotificationStatus status =
await Airship.push.iOS.authorizedNotificationStatus;
print('Authorization status: $status');Listen for changes to authorized settings:
Airship.push.iOS.onAuthorizedSettingsChanged.listen((event) {
print('Authorized settings changed: ${event.authorizedSettings}');
});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-Specific Behavior
- Android: All push messages are delivered in the background. By default, Airship treats messages without an
alertas silent. - iOS: Set the
content_availableproperty totruein the iOS override object when sending the push.
Example Silent Notification Payload
{
"audience": "all",
"notification": {
"ios": {
"content_available": true,
"extra": {
"update_type": "content_refresh"
}
},
"android": {
"extra": {
"update_type": "content_refresh"
}
}
},
"device_types": ["ios", "android"]
}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.
Clear Notifications
Clear notifications programmatically:
// Clear all notifications
await Airship.push.clearNotifications();
// Clear a specific notification by ID
await Airship.push.clearNotification('notification_id');
// Get active notifications
List<PushPayload> activeNotifications = await Airship.push.activeNotifications;
print('Active notifications: ${activeNotifications.length}');On Android, this only clears notifications sent through Airship. On iOS, it clears all notifications for the app.
Categories