From d7ef39fe9c1d02af6e55a240e8b7a0346b6c7f57 Mon Sep 17 00:00:00 2001 From: Rui Mendes Date: Thu, 14 May 2026 10:37:37 +0100 Subject: [PATCH] feat(local-notifications): add presentationOptions config support for iOS --- local-notifications/README.md | 17 +++++++------ .../LocalNotificationsHandler.swift | 24 ++++++++++++++++++- local-notifications/src/definitions.ts | 20 +++++++++++++++- 3 files changed, 52 insertions(+), 9 deletions(-) diff --git a/local-notifications/README.md b/local-notifications/README.md index 366475c2b..c88bde1a3 100644 --- a/local-notifications/README.md +++ b/local-notifications/README.md @@ -36,13 +36,14 @@ For more information about the behavior changes of your app related to the priva -On Android, the Local Notifications can be configured with the following options: +The Local Notifications can be configured with the following options: -| Prop | Type | Description | Since | -| --------------- | ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----- | -| **`smallIcon`** | string | Set the default status bar icon for notifications. Icons should be placed in your app's `res/drawable` folder. The value for this option should be the drawable resource ID, which is the filename without an extension. Only available for Android. | 1.0.0 | -| **`iconColor`** | string | Set the default color of status bar icons for notifications. Only available for Android. | 1.0.0 | -| **`sound`** | string | Set the default notification sound for notifications. On Android 8+ it sets the default channel sound and can't be changed unless the app is uninstalled. If the audio file is not found, it will result in the default system sound being played on Android 7.x and no sound on Android 8+. Only available for Android. | 1.0.0 | +| Prop | Type | Description | Since | +| ------------------------- | -------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----- | +| **`smallIcon`** | string | Set the default status bar icon for notifications. Icons should be placed in your app's `res/drawable` folder. The value for this option should be the drawable resource ID, which is the filename without an extension. Only available for Android. | 1.0.0 | +| **`iconColor`** | string | Set the default color of status bar icons for notifications. Only available for Android. | 1.0.0 | +| **`sound`** | string | Set the default notification sound for notifications. On Android 8+ it sets the default channel sound and can't be changed unless the app is uninstalled. If the audio file is not found, it will result in the default system sound being played on Android 7.x and no sound on Android 8+. Only available for Android. | 1.0.0 | +| **`presentationOptions`** | LocalNotificationPresentationOption[] | This is an array of strings you can combine. Possible values in the array are: - `badge`: badge count on the app icon is updated (default value) - `sound`: the device will ring/vibrate when the notification is received - `banner`: the notification is displayed as a banner - `list`: the notification is displayed in the notification center An empty array can be provided if none of the options are desired. Only available for iOS. | 8.2.0 | ### Examples @@ -54,7 +55,8 @@ In `capacitor.config.json`: "LocalNotifications": { "smallIcon": "ic_stat_icon_config_sample", "iconColor": "#488AFF", - "sound": "beep.wav" + "sound": "beep.wav", + "presentationOptions": ["badge", "sound", "banner", "list"] } } } @@ -73,6 +75,7 @@ const config: CapacitorConfig = { smallIcon: "ic_stat_icon_config_sample", iconColor: "#488AFF", sound: "beep.wav", + presentationOptions: ["badge", "sound", "banner", "list"], }, }, }; diff --git a/local-notifications/ios/Sources/LocalNotificationsPlugin/LocalNotificationsHandler.swift b/local-notifications/ios/Sources/LocalNotificationsPlugin/LocalNotificationsHandler.swift index e0bf257f2..efe2bae6a 100644 --- a/local-notifications/ios/Sources/LocalNotificationsPlugin/LocalNotificationsHandler.swift +++ b/local-notifications/ios/Sources/LocalNotificationsPlugin/LocalNotificationsHandler.swift @@ -35,10 +35,32 @@ public class LocalNotificationsHandler: NSObject, NotificationHandlerProtocol { } } + if let optionsArray = self.plugin?.getConfig().getArray("presentationOptions") as? [String] { + var presentationOptions = UNNotificationPresentationOptions.init() + + optionsArray.forEach { option in + switch option { + case "banner": + presentationOptions.insert(.banner) + case "list": + presentationOptions.insert(.list) + case "badge": + presentationOptions.insert(.badge) + case "sound": + presentationOptions.insert(.sound) + default: + print("Unrecognized presentation option: \(option)") + } + } + + return presentationOptions + } + return [ .badge, .sound, - .alert + .banner, + .list ] } diff --git a/local-notifications/src/definitions.ts b/local-notifications/src/definitions.ts index 7e94d5bbb..5af46e74e 100644 --- a/local-notifications/src/definitions.ts +++ b/local-notifications/src/definitions.ts @@ -2,10 +2,12 @@ import type { PermissionState, PluginListenerHandle } from '@capacitor/core'; +export type LocalNotificationPresentationOption = 'badge' | 'sound' | 'banner' | 'list'; + declare module '@capacitor/cli' { export interface PluginsConfig { /** - * On Android, the Local Notifications can be configured with the following options: + * The Local Notifications can be configured with the following options: */ LocalNotifications?: { /** @@ -47,6 +49,22 @@ declare module '@capacitor/cli' { * @example "beep.wav" */ sound?: string; + + /** + * This is an array of strings you can combine. Possible values in the array are: + * - `badge`: badge count on the app icon is updated (default value) + * - `sound`: the device will ring/vibrate when the notification is received + * - `banner`: the notification is displayed as a banner + * - `list`: the notification is displayed in the notification center + * + * An empty array can be provided if none of the options are desired. + * + * Only available for iOS. + * + * @since 8.2.0 + * @example ["badge", "sound", "banner", "list"] + */ + presentationOptions?: LocalNotificationPresentationOption[]; }; } }