Skip to main content

firebase_admin_sdk/messaging/
models.rs

1use serde::{Deserialize, Serialize};
2use std::collections::HashMap;
3
4/// Represents a message to be sent via FCM.
5#[derive(Debug, Clone, Serialize, Deserialize, Default)]
6#[serde(rename_all = "camelCase")]
7pub struct Message {
8    /// Output Only. The identifier of the message sent.
9    #[serde(skip_serializing_if = "Option::is_none")]
10    pub name: Option<String>,
11
12    /// Arbitrary key/value payload.
13    #[serde(skip_serializing_if = "Option::is_none")]
14    pub data: Option<HashMap<String, String>>,
15
16    /// Basic notification template to use across all platforms.
17    #[serde(skip_serializing_if = "Option::is_none")]
18    pub notification: Option<Notification>,
19
20    /// Android specific options for messages sent through FCM connection server.
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub android: Option<AndroidConfig>,
23
24    /// Webpush protocol options.
25    #[serde(skip_serializing_if = "Option::is_none")]
26    pub webpush: Option<WebpushConfig>,
27
28    /// Apple Push Notification Service specific options.
29    #[serde(skip_serializing_if = "Option::is_none")]
30    pub apns: Option<ApnsConfig>,
31
32    /// Template for FCM options across all platforms.
33    #[serde(skip_serializing_if = "Option::is_none")]
34    pub fcm_options: Option<FcmOptions>,
35
36    /// Registration token to send a message to.
37    #[serde(skip_serializing_if = "Option::is_none")]
38    pub token: Option<String>,
39
40    /// Topic name to send a message to, e.g. "weather".
41    /// Note: "/topics/" prefix should not be provided.
42    #[serde(skip_serializing_if = "Option::is_none")]
43    pub topic: Option<String>,
44
45    /// Condition to send a message to, e.g. "'foo' in topics && 'bar' in topics".
46    #[serde(skip_serializing_if = "Option::is_none")]
47    pub condition: Option<String>,
48}
49
50/// Represents a message to be sent to multiple recipients via FCM.
51#[derive(Debug, Clone, Serialize, Deserialize, Default)]
52#[serde(rename_all = "camelCase")]
53pub struct MulticastMessage {
54    /// A list of registration tokens to send the message to.
55    pub tokens: Vec<String>,
56
57    /// Arbitrary key/value payload.
58    #[serde(skip_serializing_if = "Option::is_none")]
59    pub data: Option<HashMap<String, String>>,
60
61    /// Basic notification template to use across all platforms.
62    #[serde(skip_serializing_if = "Option::is_none")]
63    pub notification: Option<Notification>,
64
65    /// Android specific options for messages sent through FCM connection server.
66    #[serde(skip_serializing_if = "Option::is_none")]
67    pub android: Option<AndroidConfig>,
68
69    /// Webpush protocol options.
70    #[serde(skip_serializing_if = "Option::is_none")]
71    pub webpush: Option<WebpushConfig>,
72
73    /// Apple Push Notification Service specific options.
74    #[serde(skip_serializing_if = "Option::is_none")]
75    pub apns: Option<ApnsConfig>,
76
77    /// Template for FCM options across all platforms.
78    #[serde(skip_serializing_if = "Option::is_none")]
79    pub fcm_options: Option<FcmOptions>,
80}
81
82/// Basic notification template to use across all platforms.
83#[derive(Debug, Clone, Serialize, Deserialize, Default)]
84#[serde(rename_all = "camelCase")]
85pub struct Notification {
86    /// The notification's title.
87    #[serde(skip_serializing_if = "Option::is_none")]
88    pub title: Option<String>,
89
90    /// The notification's body text.
91    #[serde(skip_serializing_if = "Option::is_none")]
92    pub body: Option<String>,
93
94    /// The URL of an image to be downloaded on the device and displayed in the notification.
95    #[serde(skip_serializing_if = "Option::is_none")]
96    pub image: Option<String>,
97}
98
99/// Android specific options for messages sent through FCM connection server.
100#[derive(Debug, Clone, Serialize, Deserialize, Default)]
101#[serde(rename_all = "camelCase")]
102pub struct AndroidConfig {
103    /// An identifier of a group of messages that can be collapsed, so that only the last message gets sent when delivery can be resumed.
104    #[serde(skip_serializing_if = "Option::is_none")]
105    pub collapse_key: Option<String>,
106
107    /// Message priority. Can be "NORMAL" or "HIGH".
108    #[serde(skip_serializing_if = "Option::is_none")]
109    pub priority: Option<AndroidMessagePriority>,
110
111    /// How long (in seconds) the message should be kept in FCM storage if the device is offline.
112    #[serde(skip_serializing_if = "Option::is_none")]
113    pub ttl: Option<String>,
114
115    /// Package name of the application where the registration token must match in order to receive the message.
116    #[serde(skip_serializing_if = "Option::is_none")]
117    pub restricted_package_name: Option<String>,
118
119    /// Arbitrary key/value payload. If present, it will override google.firebase.fcm.v1.Message.data.
120    #[serde(skip_serializing_if = "Option::is_none")]
121    pub data: Option<HashMap<String, String>>,
122
123    /// Notification to send to android devices.
124    #[serde(skip_serializing_if = "Option::is_none")]
125    pub notification: Option<AndroidNotification>,
126
127    /// Options for features provided by the FCM SDK for Android.
128    #[serde(skip_serializing_if = "Option::is_none")]
129    pub fcm_options: Option<AndroidFcmOptions>,
130
131    /// If set to true, messages will be allowed to be delivered to the app while the device is in direct boot mode.
132    #[serde(skip_serializing_if = "Option::is_none")]
133    pub direct_boot_ok: Option<bool>,
134}
135
136/// Priority of a message to send to Android devices.
137#[derive(Debug, Clone, Serialize, Deserialize)]
138#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
139pub enum AndroidMessagePriority {
140    /// Normal priority.
141    Normal,
142    /// High priority.
143    High,
144}
145
146/// Notification to send to android devices.
147#[derive(Debug, Clone, Serialize, Deserialize, Default)]
148#[serde(rename_all = "camelCase")]
149pub struct AndroidNotification {
150    /// The notification's title.
151    #[serde(skip_serializing_if = "Option::is_none")]
152    pub title: Option<String>,
153
154    /// The notification's body text.
155    #[serde(skip_serializing_if = "Option::is_none")]
156    pub body: Option<String>,
157
158    /// The notification's icon.
159    #[serde(skip_serializing_if = "Option::is_none")]
160    pub icon: Option<String>,
161
162    /// The notification's icon color, expressed in #rrggbb format.
163    #[serde(skip_serializing_if = "Option::is_none")]
164    pub color: Option<String>,
165
166    /// The sound to play when the device receives the notification.
167    #[serde(skip_serializing_if = "Option::is_none")]
168    pub sound: Option<String>,
169
170    /// Identifier used to replace existing notifications in the notification drawer.
171    #[serde(skip_serializing_if = "Option::is_none")]
172    pub tag: Option<String>,
173
174    /// The action associated with a user click on the notification.
175    #[serde(skip_serializing_if = "Option::is_none")]
176    pub click_action: Option<String>,
177
178    /// The key to the body string in the app's string resources.
179    #[serde(skip_serializing_if = "Option::is_none")]
180    pub body_loc_key: Option<String>,
181
182    /// Variable string values to be used in place of the format specifiers in body_loc_key.
183    #[serde(skip_serializing_if = "Option::is_none")]
184    pub body_loc_args: Option<Vec<String>>,
185
186    /// The key to the title string in the app's string resources.
187    #[serde(skip_serializing_if = "Option::is_none")]
188    pub title_loc_key: Option<String>,
189
190    /// Variable string values to be used in place of the format specifiers in title_loc_key.
191    #[serde(skip_serializing_if = "Option::is_none")]
192    pub title_loc_args: Option<Vec<String>>,
193
194    /// The notification's channel id.
195    #[serde(skip_serializing_if = "Option::is_none")]
196    pub channel_id: Option<String>,
197
198    /// Sets the "ticker" text, which is sent to accessibility services.
199    #[serde(skip_serializing_if = "Option::is_none")]
200    pub ticker: Option<String>,
201
202    /// When set to false or unset, the notification is automatically dismissed when the user clicks it in the panel.
203    #[serde(skip_serializing_if = "Option::is_none")]
204    pub sticky: Option<bool>,
205
206    /// Set the time that the event in the notification occurred. Notifications in the panel are sorted by this time.
207    #[serde(skip_serializing_if = "Option::is_none")]
208    pub event_time: Option<String>, // Timestamp format
209
210    /// Set whether or not this notification is relevant only to the current device.
211    #[serde(skip_serializing_if = "Option::is_none")]
212    pub local_only: Option<bool>,
213
214    /// Set the relative priority for this notification.
215    #[serde(skip_serializing_if = "Option::is_none")]
216    pub notification_priority: Option<NotificationPriority>,
217
218    /// If set to true, use the Android framework's default sound for the notification.
219    #[serde(skip_serializing_if = "Option::is_none")]
220    pub default_sound: Option<bool>,
221
222    /// If set to true, use the Android framework's default vibrate pattern for the notification.
223    #[serde(skip_serializing_if = "Option::is_none")]
224    pub default_vibrate_timings: Option<bool>,
225
226    /// If set to true, use the Android framework's default LED light settings for the notification.
227    #[serde(skip_serializing_if = "Option::is_none")]
228    pub default_light_settings: Option<bool>,
229
230    /// Set the vibration pattern to use.
231    #[serde(skip_serializing_if = "Option::is_none")]
232    pub vibrate_timings: Option<Vec<String>>, // Duration format
233
234    /// Set the Notification.visibility of the notification.
235    #[serde(skip_serializing_if = "Option::is_none")]
236    pub visibility: Option<Visibility>,
237
238    /// Compute the count of the number of unread messages in your application's launcher icon.
239    #[serde(skip_serializing_if = "Option::is_none")]
240    pub notification_count: Option<i32>,
241
242    /// Settings to control the notification's LED blinking rate and color.
243    #[serde(skip_serializing_if = "Option::is_none")]
244    pub light_settings: Option<LightSettings>,
245
246    /// The URL of an image to be displayed in the notification.
247    #[serde(skip_serializing_if = "Option::is_none")]
248    pub image: Option<String>,
249}
250
251/// Priority levels for a notification.
252#[derive(Debug, Clone, Serialize, Deserialize)]
253#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
254pub enum NotificationPriority {
255    /// Priority not specified.
256    PriorityUnspecified,
257    /// Min priority.
258    PriorityMin,
259    /// Low priority.
260    PriorityLow,
261    /// Default priority.
262    PriorityDefault,
263    /// High priority.
264    PriorityHigh,
265    /// Max priority.
266    PriorityMax,
267}
268
269/// Visibility of the notification.
270#[derive(Debug, Clone, Serialize, Deserialize)]
271#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
272pub enum Visibility {
273    /// Visibility not specified.
274    VisibilityUnspecified,
275    /// Private.
276    Private,
277    /// Public.
278    Public,
279    /// Secret.
280    Secret,
281}
282
283/// Settings to control the notification's LED blinking rate and color.
284#[derive(Debug, Clone, Serialize, Deserialize, Default)]
285#[serde(rename_all = "camelCase")]
286pub struct LightSettings {
287    /// The color of the LED.
288    pub color: Option<Color>,
289    /// The amount of time the LED is on.
290    pub light_on_duration: Option<String>,
291    /// The amount of time the LED is off.
292    pub light_off_duration: Option<String>,
293}
294
295/// Represents a color in the RGB color space.
296#[derive(Debug, Clone, Serialize, Deserialize, Default)]
297#[serde(rename_all = "camelCase")]
298pub struct Color {
299    /// The amount of red in the color as a value in the interval [0, 1].
300    pub red: Option<f32>,
301    /// The amount of green in the color as a value in the interval [0, 1].
302    pub green: Option<f32>,
303    /// The amount of blue in the color as a value in the interval [0, 1].
304    pub blue: Option<f32>,
305    /// The fraction of this color that should be applied to the pixel.
306    pub alpha: Option<f32>,
307}
308
309/// Options for features provided by the FCM SDK for Android.
310#[derive(Debug, Clone, Serialize, Deserialize, Default)]
311#[serde(rename_all = "camelCase")]
312pub struct AndroidFcmOptions {
313    /// Label associated with the message's analytics data.
314    #[serde(skip_serializing_if = "Option::is_none")]
315    pub analytics_label: Option<String>,
316}
317
318/// Webpush protocol options.
319#[derive(Debug, Clone, Serialize, Deserialize, Default)]
320#[serde(rename_all = "camelCase")]
321pub struct WebpushConfig {
322    /// HTTP headers defined in webpush protocol.
323    #[serde(skip_serializing_if = "Option::is_none")]
324    pub headers: Option<HashMap<String, String>>,
325
326    /// Arbitrary key/value payload. If present, it will override google.firebase.fcm.v1.Message.data.
327    #[serde(skip_serializing_if = "Option::is_none")]
328    pub data: Option<HashMap<String, String>>,
329
330    /// Web Notification options as a JSON object.
331    #[serde(skip_serializing_if = "Option::is_none")]
332    pub notification: Option<serde_json::Value>, // Webpush notification is loose JSON
333
334    /// Options for features provided by the FCM SDK for Web.
335    #[serde(skip_serializing_if = "Option::is_none")]
336    pub fcm_options: Option<WebpushFcmOptions>,
337}
338
339/// Options for features provided by the FCM SDK for Web.
340#[derive(Debug, Clone, Serialize, Deserialize, Default)]
341#[serde(rename_all = "camelCase")]
342pub struct WebpushFcmOptions {
343    /// The link to open when the user clicks on the notification.
344    #[serde(skip_serializing_if = "Option::is_none")]
345    pub link: Option<String>,
346    /// Label associated with the message's analytics data.
347    #[serde(skip_serializing_if = "Option::is_none")]
348    pub analytics_label: Option<String>,
349}
350
351/// Apple Push Notification Service specific options.
352#[derive(Debug, Clone, Serialize, Deserialize, Default)]
353#[serde(rename_all = "camelCase")]
354pub struct ApnsConfig {
355    /// HTTP request headers defined in Apple Push Notification Service.
356    #[serde(skip_serializing_if = "Option::is_none")]
357    pub headers: Option<HashMap<String, String>>,
358
359    /// APNs payload as a JSON object, including both 'aps' dictionary and custom payload.
360    #[serde(skip_serializing_if = "Option::is_none")]
361    pub payload: Option<ApnsPayload>,
362
363    /// Options for features provided by the FCM SDK for iOS.
364    #[serde(skip_serializing_if = "Option::is_none")]
365    pub fcm_options: Option<ApnsFcmOptions>,
366}
367
368/// APNs payload.
369#[derive(Debug, Clone, Serialize, Deserialize, Default)]
370#[serde(rename_all = "camelCase")]
371pub struct ApnsPayload {
372    /// The aps dictionary.
373    #[serde(skip_serializing_if = "Option::is_none")]
374    pub aps: Option<Aps>,
375
376    /// Custom data to include in the payload.
377    #[serde(flatten)]
378    pub custom_data: Option<HashMap<String, serde_json::Value>>,
379}
380
381/// The aps dictionary.
382#[derive(Debug, Clone, Serialize, Deserialize, Default)]
383#[serde(rename_all = "kebab-case")]
384pub struct Aps {
385    /// The alert dictionary or string.
386    #[serde(skip_serializing_if = "Option::is_none")]
387    pub alert: Option<ApsAlert>,
388
389    /// The badge number to display.
390    #[serde(skip_serializing_if = "Option::is_none")]
391    pub badge: Option<i32>,
392
393    /// The name of a sound file to play.
394    #[serde(skip_serializing_if = "Option::is_none")]
395    pub sound: Option<String>,
396
397    /// Content available flag.
398    #[serde(skip_serializing_if = "Option::is_none")]
399    pub content_available: Option<i32>, // 1
400
401    /// Mutable content flag.
402    #[serde(skip_serializing_if = "Option::is_none")]
403    pub mutable_content: Option<i32>, // 1
404
405    /// The category identifier.
406    #[serde(skip_serializing_if = "Option::is_none")]
407    pub category: Option<String>,
408
409    /// The thread identifier.
410    #[serde(skip_serializing_if = "Option::is_none")]
411    pub thread_id: Option<String>,
412}
413
414/// An alert which can be a string or a dictionary.
415#[derive(Debug, Clone, Serialize, Deserialize)]
416#[serde(untagged)]
417pub enum ApsAlert {
418    /// An alert message string.
419    String(String),
420    /// An alert dictionary.
421    Dictionary(ApsAlertDictionary),
422}
423
424/// An alert dictionary.
425#[derive(Debug, Clone, Serialize, Deserialize, Default)]
426#[serde(rename_all = "kebab-case")]
427pub struct ApsAlertDictionary {
428    /// The title of the notification.
429    #[serde(skip_serializing_if = "Option::is_none")]
430    pub title: Option<String>,
431    /// The subtitle of the notification.
432    #[serde(skip_serializing_if = "Option::is_none")]
433    pub subtitle: Option<String>,
434    /// The body text of the notification.
435    #[serde(skip_serializing_if = "Option::is_none")]
436    pub body: Option<String>,
437    /// The key to the body string in the app's Localizable.strings file.
438    #[serde(skip_serializing_if = "Option::is_none")]
439    pub loc_key: Option<String>,
440    /// Variable string values to appear in place of the format specifiers in loc_key.
441    #[serde(skip_serializing_if = "Option::is_none")]
442    pub loc_args: Option<Vec<String>>,
443    /// The key to the title string in the app's Localizable.strings file.
444    #[serde(skip_serializing_if = "Option::is_none")]
445    pub title_loc_key: Option<String>,
446    /// Variable string values to appear in place of the format specifiers in title_loc_key.
447    #[serde(skip_serializing_if = "Option::is_none")]
448    pub title_loc_args: Option<Vec<String>>,
449    /// The key to the subtitle string in the app's Localizable.strings file.
450    #[serde(skip_serializing_if = "Option::is_none")]
451    pub subtitle_loc_key: Option<String>,
452    /// Variable string values to appear in place of the format specifiers in subtitle_loc_key.
453    #[serde(skip_serializing_if = "Option::is_none")]
454    pub subtitle_loc_args: Option<Vec<String>>,
455    /// The key to the label of the action button.
456    #[serde(skip_serializing_if = "Option::is_none")]
457    pub action_loc_key: Option<String>,
458    /// The filename of an image file in the app bundle.
459    #[serde(skip_serializing_if = "Option::is_none")]
460    pub launch_image: Option<String>,
461}
462
463/// Options for features provided by the FCM SDK for iOS.
464#[derive(Debug, Clone, Serialize, Deserialize, Default)]
465#[serde(rename_all = "camelCase")]
466pub struct ApnsFcmOptions {
467    /// Label associated with the message's analytics data.
468    #[serde(skip_serializing_if = "Option::is_none")]
469    pub analytics_label: Option<String>,
470    /// URL of an image to be displayed in the notification.
471    #[serde(skip_serializing_if = "Option::is_none")]
472    pub image: Option<String>,
473}
474
475/// Template for FCM options across all platforms.
476#[derive(Debug, Clone, Serialize, Deserialize, Default)]
477#[serde(rename_all = "camelCase")]
478pub struct FcmOptions {
479    /// Label associated with the message's analytics data.
480    #[serde(skip_serializing_if = "Option::is_none")]
481    pub analytics_label: Option<String>,
482}
483
484/// Response from the topic management APIs.
485#[derive(Debug, Clone, Serialize, Deserialize, Default)]
486#[serde(rename_all = "camelCase")]
487pub struct TopicManagementResponse {
488    /// The number of tokens successfully subscribed/unsubscribed.
489    pub success_count: usize,
490    /// The number of tokens that failed to subscribe/unsubscribe.
491    pub failure_count: usize,
492    /// The list of errors.
493    pub errors: Vec<TopicManagementError>,
494}
495
496/// Error details for a single registration token.
497#[derive(Debug, Clone, Serialize, Deserialize)]
498#[serde(rename_all = "camelCase")]
499pub struct TopicManagementError {
500    /// The index of the token in the request list.
501    pub index: usize,
502    /// The error message.
503    pub reason: String,
504}
505
506/// Response from a batch send operation.
507#[derive(Debug, Clone, Default)]
508pub struct BatchResponse {
509    /// The number of messages successfully sent.
510    pub success_count: usize,
511    /// The number of messages that failed to send.
512    pub failure_count: usize,
513    /// The list of responses for each message.
514    pub responses: Vec<SendResponse>,
515}
516
517/// Response for an individual message in a batch.
518#[derive(Debug, Clone)]
519pub struct SendResponse {
520    /// Whether the message was sent successfully.
521    pub success: bool,
522    /// The message ID, if sent successfully.
523    pub message_id: Option<String>,
524    /// The error message, if failed.
525    pub error: Option<String>,
526}
527
528#[derive(Debug, Deserialize)]
529pub(crate) struct SendResponseInternal {
530    pub name: String,
531}