firebae_cm/settings/
android.rs

1use serde_json::Value;
2
3use crate::{AndroidFcmOptions, AndroidNotification, IntoFirebaseMap};
4
5/// Represents the Android message priority.
6/// See <https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#androidmessagepriority>.
7#[derive(serde::Serialize, Debug, Clone)]
8#[serde(rename_all = "lowercase")]
9pub enum AndroidMessagePriority {
10    Normal,
11    High,
12}
13
14/// Represents all settings for an Android message.
15/// See <https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#AndroidConfig> for a complete list.
16#[derive(serde::Serialize, Debug, Default, Clone)]
17pub struct AndroidConfig {
18    #[serde(skip_serializing_if = "Option::is_none")]
19    collapse_key: Option<String>,
20    #[serde(skip_serializing_if = "Option::is_none")]
21    priority: Option<AndroidMessagePriority>,
22    #[serde(skip_serializing_if = "Option::is_none")]
23    ttl: Option<String>,
24    #[serde(skip_serializing_if = "Option::is_none")]
25    restricted_package_name: Option<String>,
26    #[serde(skip_serializing_if = "Option::is_none")]
27    data: Option<Value>,
28    #[serde(skip_serializing_if = "Option::is_none")]
29    notification: Option<AndroidNotification>,
30    #[serde(skip_serializing_if = "Option::is_none")]
31    fcm_options: Option<AndroidFcmOptions>,
32    #[serde(skip_serializing_if = "Option::is_none")]
33    direct_boot_ok: Option<bool>,
34}
35
36impl AndroidConfig {
37    pub fn new() -> Self {
38        Default::default()
39    }
40
41    pub fn collapse_key(&mut self, collapse_key: impl Into<String>) -> &mut Self {
42        self.collapse_key = Some(collapse_key.into());
43        self
44    }
45
46    pub fn priority(&mut self, priority: AndroidMessagePriority) -> &mut Self {
47        self.priority = Some(priority);
48        self
49    }
50
51    pub fn ttl(&mut self, seconds: usize) -> &mut Self {
52        self.ttl = Some(format!("{}s", seconds));
53        self
54    }
55
56    pub fn restricted_package_name(
57        &mut self,
58        restricted_package_name: impl Into<String>,
59    ) -> &mut Self {
60        self.restricted_package_name = Some(restricted_package_name.into());
61        self
62    }
63
64    /// Sets the data of the message. Accepts any type that implements IntoFirebaseMap, which will construct the required Map<String, String>.
65    /// For ease, you can use the [crate::AsFirebaseMap] derive macro on your structs:
66    /// ```rust
67    /// use firebae_cm::{AsFirebaseMap, AndroidConfig};
68    /// 
69    /// #[derive(AsFirebaseMap)]
70    /// struct Data {
71    ///     field1: String,
72    ///     field2: i32, // Note that this will become a String in Firebase
73    /// }
74    /// 
75    /// fn main() {
76    ///     let data = Data {
77    ///         field1: "Hello, world!".to_string(),
78    ///         field2: 5481,
79    ///     };
80    /// 
81    ///     let mut config = AndroidConfig::new();
82    ///     config.data(data).expect("Data not parsable");    
83    /// }
84    /// ```
85    pub fn data(&mut self, data: impl IntoFirebaseMap) -> crate::Result<&mut Self> {
86        self.data = Some(serde_json::to_value(data.as_map().get_map())?);
87        Ok(self)
88    }
89
90    pub fn notification(&mut self, notification: AndroidNotification) -> &mut Self {
91        self.notification = Some(notification);
92        self
93    }
94
95    pub fn fcm_options(&mut self, fcm_options: AndroidFcmOptions) -> &mut Self {
96        self.fcm_options = Some(fcm_options);
97        self
98    }
99
100    pub fn direct_boot_ok(&mut self, direct_boot_ok: bool) -> &mut Self {
101        self.direct_boot_ok = Some(direct_boot_ok);
102        self
103    }
104}