firebase_client/
notification.rs

1use serde::Serialize;
2
3#[derive(Serialize, Clone, Debug, Default)]
4pub struct FirebasePayload {
5    message: FirebaseNotification,
6}
7
8#[derive(Serialize, Clone, Debug, Default)]
9pub struct FirebaseNotification {
10    token: String,
11    notification: Notification,
12    android: AndroidField,
13    #[serde(skip_serializing_if = "Option::is_none")]
14    data: Option<serde_json::Value>,
15    apns: ApnField,
16}
17
18#[derive(Serialize, Clone, Debug, Default)]
19pub struct AndroidField {
20    notification: AndroidNotification,
21}
22
23#[derive(Serialize, Clone, Debug, Default)]
24pub struct AndroidNotification {
25    channel_id: Option<String>,
26}
27
28#[derive(Serialize, Clone, Debug, Default)]
29pub struct Notification {
30    title: String,
31    body: Option<String>,
32}
33
34#[derive(Serialize, Clone, Debug, Default)]
35pub struct ApnField {
36    payload: ApnPayload,
37}
38
39#[derive(Serialize, Clone, Debug, Default)]
40pub struct ApnPayload {
41    aps: ApnAps,
42}
43
44#[derive(Serialize, Clone, Debug, Default)]
45pub struct ApnAps {
46    alert: ApnAlert,
47    #[serde(skip_serializing_if = "Option::is_none")]
48    sound: Option<String>,
49}
50
51#[derive(Serialize, Clone, Debug, Default)]
52pub struct ApnAlert {
53    title: String,
54    body: Option<String>,
55}
56
57#[derive(Default)]
58pub struct NotificationBuilder {
59    token: String,
60    title: String,
61    message: Option<String>,
62    android_channel_id: Option<String>,
63    data: Option<serde_json::Value>,
64    apn_sound: Option<String>,
65}
66
67impl NotificationBuilder {
68    pub fn new(title: &str, token: &str) -> Self {
69        Self {
70            title: title.to_string(),
71            token: token.to_string(),
72            ..Default::default()
73        }
74    }
75
76    pub fn message(self, message: &str) -> Self {
77        Self {
78            message: Some(message.to_string()),
79            ..self
80        }
81    }
82
83    pub fn android_channel_id(self, android_channel_id: &str) -> Self {
84        Self {
85            android_channel_id: Some(android_channel_id.to_string()),
86            ..self
87        }
88    }
89
90    pub fn data<T: Serialize>(self, data: T) -> Self {
91        Self {
92            data: Some(serde_json::to_value(data).unwrap()),
93            ..self
94        }
95    }
96
97    pub fn apn_sound(self, apn_sound: String) -> Self {
98        Self {
99            apn_sound: Some(apn_sound),
100            ..self
101        }
102    }
103
104    pub fn build(self) -> FirebasePayload {
105        let notification = Notification {
106            title: self.title.clone(),
107            body: self.message.clone(),
108        };
109        let android = AndroidNotification {
110            channel_id: self.android_channel_id,
111        };
112        let android = AndroidField {
113            notification: android,
114        };
115
116        let apn_field = {
117            let apn_alert = ApnAlert {
118                title: self.title,
119                body: self.message,
120            };
121            let apn_aps = ApnAps {
122                alert: apn_alert,
123                sound: self.apn_sound,
124            };
125            let apn_payload = ApnPayload { aps: apn_aps };
126            ApnField {
127                payload: apn_payload,
128            }
129        };
130
131        let firebase_notification = FirebaseNotification {
132            notification,
133            android,
134            token: self.token,
135            data: self.data,
136            apns: apn_field,
137        };
138        FirebasePayload {
139            message: firebase_notification,
140        }
141    }
142}