rust_macios/user_notifications/
un_notification_content.rs

1use libc::c_double;
2use objc::{msg_send, sel, sel_impl};
3use rust_macios_objective_c_runtime_proc_macros::interface_impl;
4
5use crate::{
6    foundation::{NSArray, NSDictionary, NSError, NSNumber, NSString, UInt},
7    object,
8    objective_c_runtime::{
9        id,
10        traits::{FromId, PNSObject},
11    },
12    utils::to_optional,
13};
14
15use super::{UNNotificationAttachment, UNNotificationSound};
16
17#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
18#[repr(i64)]
19pub enum UNNotificationInterruptionLevel {
20    Passive,
21    Active,
22    TimeSensitive,
23    Critical,
24}
25
26object! {
27    /// The uneditable content of a notification.
28    unsafe pub struct UNNotificationContent;
29}
30
31#[interface_impl(NSObject)]
32impl UNNotificationContent {
33    /// The localized text that provides the notification’s primary description.
34    #[property]
35    pub fn title(&self) -> NSString {
36        unsafe { NSString::from_id(msg_send![self.m_self(), title]) }
37    }
38
39    /// The localized text that provides the notification’s secondary description.
40    #[property]
41    pub fn subtitle(&self) -> NSString {
42        unsafe { NSString::from_id(msg_send![self.m_self(), subtitle]) }
43    }
44
45    /// The localized text that provides the notification’s main content.
46    #[property]
47    pub fn body(&self) -> NSString {
48        unsafe { NSString::from_id(msg_send![self.m_self(), body]) }
49    }
50
51    /* Accessing supplementary content
52     */
53
54    /// The visual and audio attachments to display alongside the notification’s main content.
55    #[property]
56    pub fn attachments(&self) -> NSArray<UNNotificationAttachment> {
57        unsafe { NSArray::from_id(msg_send![self.m_self(), attachments]) }
58    }
59
60    /// The custom data to associate with the notification.
61    #[property]
62    pub fn user_info(&self) -> NSDictionary<id, id> {
63        unsafe { NSDictionary::from_id(msg_send![self.m_self(), userInfo]) }
64    }
65
66    /* Reading app configuration
67     */
68
69    /// The name of the image or storyboard to use when your app launches because of the notification.
70    #[property]
71    pub fn launch_image_name(&self) -> NSString {
72        unsafe { NSString::from_id(msg_send![self.m_self(), launchImageName]) }
73    }
74
75    /// The number that your app’s icon displays.
76    #[property]
77    pub fn badge(&self) -> Option<NSNumber> {
78        unsafe { to_optional(msg_send![self.m_self(), badge]) }
79    }
80
81    /// The value your app uses to determine which scene to display to handle the notification.
82    #[property]
83    pub fn target_content_identifier(&self) -> Option<NSString> {
84        unsafe { to_optional(msg_send![self.m_self(), targetContentIdentifier]) }
85    }
86
87    /* Reading system configuration
88     */
89
90    /// The sound that plays when the system delivers the notification.
91    #[property]
92    pub fn sound(&self) -> Option<UNNotificationSound> {
93        unsafe { to_optional(msg_send![self.m_self(), sound]) }
94    }
95
96    /// The notification’s importance and required delivery timing.
97    #[property]
98    pub fn interruption_level(&self) -> UNNotificationInterruptionLevel {
99        unsafe { msg_send![self.m_self(), interruptionLevel] }
100    }
101
102    ///The score the system uses to determine if the notification is the summary’s featured notification.
103    #[property]
104    pub fn relevance_score(&self) -> c_double {
105        unsafe { msg_send![self.m_self(), relevanceScore] }
106    }
107
108    /// The criteria the system evaluates to determine if it displays the notification in the current Focus.
109    #[property]
110    pub fn filter_criteria(&self) -> Option<NSString> {
111        unsafe { to_optional(msg_send![self.m_self(), filterCriteria]) }
112    }
113
114    /* Retrieving group information
115     */
116
117    /// The identifier that groups related notifications.
118    #[property]
119    pub fn thread_identifier(&self) -> NSString {
120        unsafe { NSString::from_id(msg_send![self.m_self(), threadIdentifier]) }
121    }
122
123    /// The identifier of the notification’s category.
124    #[property]
125    pub fn category_identifier(&self) -> NSString {
126        unsafe { NSString::from_id(msg_send![self.m_self(), categoryIdentifier]) }
127    }
128
129    /// The text the system adds to the notification summary to provide additional context.
130    #[property]
131    pub fn summary_argument(&self) -> NSString {
132        unsafe { NSString::from_id(msg_send![self.m_self(), summaryArgument]) }
133    }
134
135    /// The number the system adds to the notification summary when the notification represents multiple items.
136    #[property]
137    pub fn summary_argument_count(&self) -> UInt {
138        unsafe { msg_send![self.m_self(), summaryArgumentCount] }
139    }
140
141    /* Updating the notification’s content
142     */
143
144    /// Returns a copy of the notification that includes content from the specified provider.
145    #[method]
146    pub fn content_by_updating_with_provider(
147        &mut self,
148        provider: id,
149    ) -> Result<UNNotificationContent, NSError> {
150        unsafe {
151            let mut error = NSError::m_alloc();
152
153            let ptr = UNNotificationContent::from_id(
154                msg_send![self.m_self(), contentByUpdatingWithProvider: provider error: &mut error],
155            );
156
157            if error.m_self().is_null() {
158                Ok(ptr)
159            } else {
160                Err(error)
161            }
162        }
163    }
164}