firebae_cm/notification/
notification.rs

1use crate::Color;
2
3/// Represents the priority of the notification.
4/// See <https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#notificationpriority>.
5#[derive(serde::Serialize, Debug, Clone)]
6#[serde(rename_all = "snake_case")]
7pub enum NotificationPriority {
8    PriorityUnspecified,
9    PriorityMin,
10    PriorityLow,
11    PriorityDefault,
12    PriorityHigh,
13    PriorityMax,
14}
15
16/// Represents the visibility of the notification.
17/// See <https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#visibility>
18#[derive(serde::Serialize, Debug, Clone)]
19#[serde(rename_all = "snake_case")]
20pub enum Visibility {
21    VisibilityUnspecified,
22    Private,
23    Public,
24    Secret,
25}
26
27/// Represents the notification light settings of the notification.
28/// See <https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#lightsettings>.
29/// Durations are in seconds.
30#[derive(serde::Serialize, Debug, Default, Clone)]
31pub struct LightSettings {
32    #[serde(skip_serializing_if = "Option::is_none")]
33    color: Option<Color>,
34    #[serde(skip_serializing_if = "Option::is_none")]
35    light_on_duration: Option<String>,
36    #[serde(skip_serializing_if = "Option::is_none")]
37    light_off_duration: Option<String>,
38}
39
40impl LightSettings {
41    pub fn new() -> Self {
42        Default::default()
43    }
44
45    pub fn color(&mut self, color: Color) -> &mut Self {
46        self.color = Some(color);
47        self
48    }
49
50    pub fn light_on_duration(&mut self, seconds: usize) -> &mut Self {
51        self.light_on_duration = Some(format!("{}s", seconds));
52        self
53    }
54
55    pub fn light_off_duration(&mut self, seconds: usize) -> &mut Self {
56        self.light_off_duration = Some(format!("{}s", seconds));
57        self
58    }
59}
60
61/// Represents a basic template for notifications, which is equal across all platforms.
62/// See <https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#notification>.
63/// Use the `Notification::new` function, or initialize the struct yourself:
64/// ```rust
65/// let notification = Notification {
66///     title: Some("Hello, ").to_string(),
67///     body: Some("world!").to_string(),
68///     image: None,
69/// };
70/// ```
71#[derive(serde::Serialize, Debug, Clone, Default)]
72pub struct Notification {
73    #[serde(skip_serializing_if = "Option::is_none")]
74    pub title: Option<String>,
75    #[serde(skip_serializing_if = "Option::is_none")]
76    pub body: Option<String>,
77    #[serde(skip_serializing_if = "Option::is_none")]
78    pub image: Option<String>,
79}
80
81impl Notification {
82    /// Creates an empty Notification.
83    pub fn new() -> Self {
84        Default::default()
85    }
86
87    /// Easily create a notification with any type that implements `Into<String>` (such as `&str`).
88    /// ```rust
89    /// let notification = Notification::new().with_title("Hello!");
90    /// ```
91    pub fn with_title(mut self, title: impl Into<String>) -> Self {
92        self.title = Some(title.into());
93        self
94    }
95
96    /// Easily create a notification with any type that implements `Into<String>` (such as `&str`).
97    /// ```rust
98    /// let notification = Notification::new().with_title("Hello, ").with_body("world!");
99    /// ```
100    pub fn with_body(mut self, body: impl Into<String>) -> Self {
101        self.body = Some(body.into());
102        self
103    }
104
105    /// Easily create a notification with any type that implements `Into<String>` (such as `&str`).
106    /// ```rust
107    /// let notification = Notification::new().with_image("/static/img.png");
108    /// ```
109    pub fn with_image(mut self, image: impl Into<String>) -> Self {
110        self.image = Some(image.into());
111        self
112    }
113}