Skip to content

Commit 78bea4c

Browse files
feat(local-notifications): add presentationOptions config support for iOS (#2530)
1 parent 6b9b1a2 commit 78bea4c

3 files changed

Lines changed: 52 additions & 9 deletions

File tree

local-notifications/README.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@ For more information about the behavior changes of your app related to the priva
3636
<docgen-config>
3737
<!--Update the source file JSDoc comments and rerun docgen to update the docs below-->
3838

39-
On Android, the Local Notifications can be configured with the following options:
39+
The Local Notifications can be configured with the following options:
4040

41-
| Prop | Type | Description | Since |
42-
| --------------- | ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----- |
43-
| **`smallIcon`** | <code>string</code> | 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 |
44-
| **`iconColor`** | <code>string</code> | Set the default color of status bar icons for notifications. Only available for Android. | 1.0.0 |
45-
| **`sound`** | <code>string</code> | 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 |
41+
| Prop | Type | Description | Since |
42+
| ------------------------- | -------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----- |
43+
| **`smallIcon`** | <code>string</code> | 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 |
44+
| **`iconColor`** | <code>string</code> | Set the default color of status bar icons for notifications. Only available for Android. | 1.0.0 |
45+
| **`sound`** | <code>string</code> | 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 |
46+
| **`presentationOptions`** | <code>LocalNotificationPresentationOption[]</code> | 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 |
4647

4748
### Examples
4849

@@ -54,7 +55,8 @@ In `capacitor.config.json`:
5455
"LocalNotifications": {
5556
"smallIcon": "ic_stat_icon_config_sample",
5657
"iconColor": "#488AFF",
57-
"sound": "beep.wav"
58+
"sound": "beep.wav",
59+
"presentationOptions": ["badge", "sound", "banner", "list"]
5860
}
5961
}
6062
}
@@ -73,6 +75,7 @@ const config: CapacitorConfig = {
7375
smallIcon: "ic_stat_icon_config_sample",
7476
iconColor: "#488AFF",
7577
sound: "beep.wav",
78+
presentationOptions: ["badge", "sound", "banner", "list"],
7679
},
7780
},
7881
};

local-notifications/ios/Sources/LocalNotificationsPlugin/LocalNotificationsHandler.swift

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,32 @@ public class LocalNotificationsHandler: NSObject, NotificationHandlerProtocol {
3535
}
3636
}
3737

38+
if let optionsArray = self.plugin?.getConfig().getArray("presentationOptions") as? [String] {
39+
var presentationOptions = UNNotificationPresentationOptions.init()
40+
41+
optionsArray.forEach { option in
42+
switch option {
43+
case "banner":
44+
presentationOptions.insert(.banner)
45+
case "list":
46+
presentationOptions.insert(.list)
47+
case "badge":
48+
presentationOptions.insert(.badge)
49+
case "sound":
50+
presentationOptions.insert(.sound)
51+
default:
52+
print("Unrecognized presentation option: \(option)")
53+
}
54+
}
55+
56+
return presentationOptions
57+
}
58+
3859
return [
3960
.badge,
4061
.sound,
41-
.alert
62+
.banner,
63+
.list
4264
]
4365
}
4466

local-notifications/src/definitions.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
import type { PermissionState, PluginListenerHandle } from '@capacitor/core';
44

5+
export type LocalNotificationPresentationOption = 'badge' | 'sound' | 'banner' | 'list';
6+
57
declare module '@capacitor/cli' {
68
export interface PluginsConfig {
79
/**
8-
* On Android, the Local Notifications can be configured with the following options:
10+
* The Local Notifications can be configured with the following options:
911
*/
1012
LocalNotifications?: {
1113
/**
@@ -47,6 +49,22 @@ declare module '@capacitor/cli' {
4749
* @example "beep.wav"
4850
*/
4951
sound?: string;
52+
53+
/**
54+
* This is an array of strings you can combine. Possible values in the array are:
55+
* - `badge`: badge count on the app icon is updated (default value)
56+
* - `sound`: the device will ring/vibrate when the notification is received
57+
* - `banner`: the notification is displayed as a banner
58+
* - `list`: the notification is displayed in the notification center
59+
*
60+
* An empty array can be provided if none of the options are desired.
61+
*
62+
* Only available for iOS.
63+
*
64+
* @since 8.2.0
65+
* @example ["badge", "sound", "banner", "list"]
66+
*/
67+
presentationOptions?: LocalNotificationPresentationOption[];
5068
};
5169
}
5270
}

0 commit comments

Comments
 (0)