fcm_service/domain/
fcm_message.rs

1use std::{collections::HashMap, hash::RandomState};
2
3use serde::{Deserialize, Serialize};
4
5use super::{AndroidConfig, ApnsConfig, FcmNotification, FcmOptions, Target, WebpushConfig};
6
7/// Represents an FCM message with all supported fields.
8#[derive(Clone, Debug, Serialize, Deserialize, Default)]
9pub struct FcmMessage {
10    name: Option<String>,
11    data: Option<HashMap<String, String>>,
12    notification: Option<FcmNotification>,
13    android: Option<AndroidConfig>,
14    webpush: Option<WebpushConfig>,
15    apns: Option<ApnsConfig>,
16    fcm_options: Option<FcmOptions>,
17    #[serde(flatten)]
18    target: Target,
19}
20
21impl FcmMessage {
22    /// Creates a new, empty `FcmMessage`.
23    #[must_use]
24    pub fn new() -> Self {
25        Self {
26            ..Default::default()
27        }
28    }
29
30    /// Sets the message name.
31    pub fn set_name(&mut self, name: Option<String>) {
32        self.name = name;
33    }
34
35    /// Sets custom data key-value pairs.
36    pub fn set_data(&mut self, data: Option<HashMap<String, String>>) {
37        self.data = data;
38    }
39
40    /// Sets the notification content.
41    pub fn set_notification(&mut self, notification: Option<FcmNotification>) {
42        self.notification = notification;
43    }
44
45    /// Sets Android-specific configuration.
46    pub fn set_android(&mut self, android: Option<AndroidConfig>) {
47        self.android = android;
48    }
49
50    /// Sets Webpush-specific configuration.
51    pub fn set_webpush(&mut self, webpush: Option<WebpushConfig>) {
52        self.webpush = webpush;
53    }
54
55    /// Sets APNs-specific configuration.
56    pub fn set_apns(&mut self, apns: Option<ApnsConfig>) {
57        self.apns = apns;
58    }
59
60    /// Sets FCM options.
61    pub fn set_fcm_options(&mut self, fcm_options: Option<FcmOptions>) {
62        self.fcm_options = fcm_options;
63    }
64
65    /// Sets the message target (token, topic, or condition).
66    pub fn set_target(&mut self, target: Target) {
67        self.target = target;
68    }
69
70    #[must_use]
71    pub fn name(&self) -> Option<&String> {
72        self.name.as_ref()
73    }
74
75    #[must_use]
76    pub fn data(&self) -> Option<&HashMap<String, String, RandomState>> {
77        self.data.as_ref()
78    }
79
80    #[must_use]
81    pub fn notification(&self) -> Option<&FcmNotification> {
82        self.notification.as_ref()
83    }
84
85    #[must_use]
86    pub fn android(&self) -> Option<&AndroidConfig> {
87        self.android.as_ref()
88    }
89
90    #[must_use]
91    pub fn webpush(&self) -> Option<&WebpushConfig> {
92        self.webpush.as_ref()
93    }
94
95    #[must_use]
96    pub fn apns(&self) -> Option<&ApnsConfig> {
97        self.apns.as_ref()
98    }
99
100    #[must_use]
101    pub fn fcm_options(&self) -> Option<&FcmOptions> {
102        self.fcm_options.as_ref()
103    }
104
105    #[must_use]
106    pub fn target(&self) -> &Target {
107        &self.target
108    }
109}