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(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    pub fn new() -> Self {
24        Self {
25            ..Default::default()
26        }
27    }
28
29    /// Sets the message name.
30    pub fn set_name(&mut self, name: Option<String>) {
31        self.name = name;
32    }
33
34    /// Sets custom data key-value pairs.
35    pub fn set_data(&mut self, data: Option<HashMap<String, String>>) {
36        self.data = data;
37    }
38
39    /// Sets the notification content.
40    pub fn set_notification(&mut self, notification: Option<FcmNotification>) {
41        self.notification = notification;
42    }
43
44    /// Sets Android-specific configuration.
45    pub fn set_android(&mut self, android: Option<AndroidConfig>) {
46        self.android = android;
47    }
48
49    /// Sets Webpush-specific configuration.
50    pub fn set_webpush(&mut self, webpush: Option<WebpushConfig>) {
51        self.webpush = webpush;
52    }
53
54    /// Sets APNs-specific configuration.
55    pub fn set_apns(&mut self, apns: Option<ApnsConfig>) {
56        self.apns = apns;
57    }
58
59    /// Sets FCM options.
60    pub fn set_fcm_options(&mut self, fcm_options: Option<FcmOptions>) {
61        self.fcm_options = fcm_options;
62    }
63
64    /// Sets the message target (token, topic, or condition).
65    pub fn set_target(&mut self, target: Target) {
66        self.target = target;
67    }
68
69    pub fn name(&self) -> Option<&String> {
70        self.name.as_ref()
71    }
72
73    pub fn data(&self) -> Option<&HashMap<String, String, RandomState>> {
74        self.data.as_ref()
75    }
76
77    pub fn notification(&self) -> Option<&FcmNotification> {
78        self.notification.as_ref()
79    }
80
81    pub fn android(&self) -> Option<&AndroidConfig> {
82        self.android.as_ref()
83    }
84
85    pub fn webpush(&self) -> Option<&WebpushConfig> {
86        self.webpush.as_ref()
87    }
88
89    pub fn apns(&self) -> Option<&ApnsConfig> {
90        self.apns.as_ref()
91    }
92
93    pub fn fcm_options(&self) -> Option<&FcmOptions> {
94        self.fcm_options.as_ref()
95    }
96
97    pub fn target(&self) -> &Target {
98        &self.target
99    }
100}