rust_macios/user_notifications/
un_notification_settings.rs

1use objc::{msg_send, sel, sel_impl};
2
3use crate::{
4    object,
5    objective_c_runtime::{macros::interface_impl, traits::PNSObject},
6    utils::to_bool,
7};
8
9object! {
10    /// The object for managing notification-related settings and the authorization status of your app.
11    unsafe pub struct UNNotificationSettings;
12}
13
14/// Constants indicating whether the app is allowed to schedule notifications.
15#[repr(i64)]
16#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
17pub enum UNAuthorizationStatus {
18    /// The user hasn't yet made a choice about whether the app is allowed to schedule notifications.
19    NotDetermined = 0,
20    /// The app isn't authorized to schedule or receive notifications.
21    Denied,
22    /// The app is authorized to schedule or receive notifications.
23    Authorized,
24    /// The application is provisionally authorized to post noninterruptive user notifications.
25    #[cfg(target_os = "ios")]
26    Provisional,
27    /// The app is authorized to schedule or receive notifications for a limited amount of time.
28    #[cfg(target_os = "ios")]
29    Ephemeral,
30}
31
32/// Constants that indicate the current status of a notification setting.
33#[repr(i64)]
34#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
35pub enum UNNotificationSetting {
36    /// The setting is not available to your app.
37    NotSupported = 0,
38    /// The setting is disabled.
39    Disabled,
40    /// The setting is enabled.
41    Enabled,
42}
43
44/// Constants indicating the presentation styles for alerts.
45#[repr(i64)]
46#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
47pub enum UNAlertStyle {
48    /// No alert.
49    None = 0,
50    /// Banner alerts.
51    Banner,
52    /// Modal alerts.
53    Alert,
54}
55
56/// Constants indicating the style previewing a notification's content.
57#[repr(i64)]
58#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
59pub enum UNShowPreviewsSetting {
60    /// The notification's content is always shown, even when the device is locked.
61    Always,
62    /// The notification's content is shown only when the device is unlocked.
63    WhenAuthenticated,
64    /// The notification's content is never shown, even when the device is unlocked
65    Never,
66}
67
68#[interface_impl(NSObject)]
69impl UNNotificationSettings {
70    /* Getting the Authorization Status
71     */
72
73    /// The app's ability to schedule and receive local and remote notifications.
74    #[property]
75    pub fn authorization_status(&self) -> UNAuthorizationStatus {
76        unsafe { msg_send![self.m_self(), authorizationStatus] }
77    }
78
79    /* Getting Device-Specific Settings
80     */
81
82    /// The setting that indicates whether your app’s notifications appear in Notification Center.
83    #[property]
84    pub fn notification_center_setting(&self) -> UNNotificationSetting {
85        unsafe { msg_send![self.m_self(), notificationCenterSetting] }
86    }
87
88    /// The setting that indicates whether your app’s notifications appear on a device’s Lock screen.
89    #[property]
90    pub fn lock_screen_setting(&self) -> UNNotificationSetting {
91        unsafe { msg_send![self.m_self(), lockScreenSetting] }
92    }
93
94    /// The setting that indicates whether your app’s notifications appear in CarPlay.
95    #[property]
96    pub fn car_play_setting(&self) -> UNNotificationSetting {
97        unsafe { msg_send![self.m_self(), carPlaySetting] }
98    }
99
100    /// The authorization status for displaying alerts.
101    #[property]
102    pub fn alert_setting(&self) -> UNNotificationSetting {
103        unsafe { msg_send![self.m_self(), alertSetting] }
104    }
105
106    /// The setting that indicates whether badges appear on your app’s icon.
107    #[property]
108    pub fn badge_setting(&self) -> UNNotificationSetting {
109        unsafe { msg_send![self.m_self(), badgeSetting] }
110    }
111
112    /// The authorization status for playing sounds for incoming notifications.
113    #[property]
114    pub fn sound_setting(&self) -> UNNotificationSetting {
115        unsafe { msg_send![self.m_self(), soundSetting] }
116    }
117
118    /// The authorization status for playing sounds for critical alerts.
119    #[property]
120    pub fn critical_alert_setting(&self) -> UNNotificationSetting {
121        unsafe { msg_send![self.m_self(), criticalAlertSetting] }
122    }
123
124    /// The setting that indicates whether Siri can announce your app’s notifications.
125    #[property]
126    pub fn announcement_setting(&self) -> UNNotificationSetting {
127        unsafe { msg_send![self.m_self(), announcementSetting] }
128    }
129
130    /// The setting that indicates the system schedules the notification.
131    #[property]
132    pub fn scheduled_delivery_setting(&self) -> UNNotificationSetting {
133        unsafe { msg_send![self.m_self(), scheduledDeliverySetting] }
134    }
135
136    /// The setting that indicates the system treats the notification as time-sensitive.
137    #[property]
138    pub fn time_sensitive_setting(&self) -> UNNotificationSetting {
139        unsafe { msg_send![self.m_self(), timeSensitiveSetting] }
140    }
141
142    /* Getting Interface Settings
143     */
144
145    /// The setting that indicates whether the app shows a preview of the notification's content.
146    #[property]
147    pub fn show_previews_setting(&self) -> UNShowPreviewsSetting {
148        unsafe { msg_send![self.m_self(), showPreviewsSetting] }
149    }
150
151    /// A Boolean value indicating the system displays a button for in-app notification settings.
152    #[property]
153    pub fn provides_app_notification_settings(&self) -> bool {
154        unsafe { to_bool(msg_send![self.m_self(), providesAppNotificationSettings]) }
155    }
156
157    ///
158    #[property]
159    pub fn direct_messages_setting(&self) -> UNNotificationSetting {
160        unsafe { msg_send![self.m_self(), directMessagesSetting] }
161    }
162}