1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
use uuid::Uuid;
pub static APN_URL_PRODUCTION: &'static str = "https://api.push.apple.com";
pub static APN_URL_DEV: &'static str = "https://api.development.push.apple.com";
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Copy, Debug)]
pub enum Priority {
#[serde(rename = "5")]
Low,
#[serde(rename = "10")]
High,
}
impl Priority {
pub fn to_int(self) -> u32 {
match self {
Priority::Low => 5,
Priority::High => 10,
}
}
}
#[derive(Fail, Debug)]
#[fail(display = "CollapseId too long (must be at most 64 bytes)")]
pub struct CollapseIdTooLongError;
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct CollapseId(String);
impl CollapseId {
pub fn new(value: String) -> Result<Self, CollapseIdTooLongError> {
if value.len() > 64 {
Err(CollapseIdTooLongError)
} else {
Ok(CollapseId(value))
}
}
pub fn as_str(&self) -> &str {
&self.0
}
}
#[derive(Serialize, Deserialize, Default, Clone, Debug)]
pub struct AlertPayload {
#[serde(skip_serializing_if = "Option::is_none")]
pub title: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub body: Option<String>,
#[serde(rename = "title-loc-key", skip_serializing_if = "Option::is_none")]
pub title_loc_key: Option<String>,
#[serde(rename = "title-loc-args", skip_serializing_if = "Option::is_none")]
pub title_loc_args: Option<Vec<String>>,
#[serde(rename = "action-loc-key", skip_serializing_if = "Option::is_none")]
pub action_loc_key: Option<String>,
#[serde(rename = "loc-key", skip_serializing_if = "Option::is_none")]
pub loc_key: Option<String>,
#[serde(rename = "loc-args", skip_serializing_if = "Option::is_none")]
pub loc_args: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub loc_image: Option<String>,
}
impl AlertPayload {
fn new(title: Option<String>, body: Option<String>) -> Self {
AlertPayload {
title: title,
body: body,
title_loc_key: None,
title_loc_args: None,
action_loc_key: None,
loc_key: None,
loc_args: None,
loc_image: None,
}
}
}
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(untagged)]
pub enum Alert {
Simple(String),
Payload(AlertPayload),
}
#[derive(Serialize, Deserialize, Default, Clone, Debug)]
pub struct Payload {
#[serde(skip_serializing_if = "Option::is_none")]
pub alert: Option<Alert>,
#[serde(skip_serializing_if = "Option::is_none")]
pub badge: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub sound: Option<String>,
#[serde(rename = "content-available", skip_serializing_if = "Option::is_none")]
pub content_available: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub category: Option<String>,
#[serde(rename = "thread-id", skip_serializing_if = "Option::is_none")]
pub thread_id: Option<String>,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub(crate) struct ApnsRequest {
pub aps: Payload,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Notification {
pub topic: String,
pub device_token: String,
pub payload: Payload,
pub id: Option<Uuid>,
pub expiration: Option<u64>,
pub priority: Option<Priority>,
pub collapse_id: Option<CollapseId>,
}
impl Notification {
pub fn new(topic: String, device_token: String, payload: Payload) -> Self {
Notification {
topic,
device_token,
payload,
id: None,
expiration: None,
priority: None,
collapse_id: None,
}
}
}
pub struct NotificationBuilder {
notification: Notification,
}
impl NotificationBuilder {
pub fn new(topic: String, device_id: String) -> Self {
NotificationBuilder {
notification: Notification::new(topic, device_id, Payload::default()),
}
}
pub fn payload(mut self, payload: Payload) -> Self {
self.notification.payload = payload;
self
}
pub fn alert<S: Into<String>>(mut self, alert: S) -> Self {
self.notification.payload.alert = Some(Alert::Simple(alert.into()));
self
}
pub fn title<S: Into<String>>(mut self, title: S) -> Self {
let title = title.into();
let payload = match self.notification.payload.alert.take() {
None => AlertPayload::new(Some(title), None),
Some(Alert::Simple(_)) => AlertPayload::new(Some(title), None),
Some(Alert::Payload(mut payload)) => {
payload.title = Some(title);
payload
}
};
self.notification.payload.alert = Some(Alert::Payload(payload));
self
}
pub fn body<S: Into<String>>(mut self, body: S) -> Self {
let body = body.into();
let payload = match self.notification.payload.alert.take() {
None => AlertPayload::new(None, Some(body)),
Some(Alert::Simple(title)) => AlertPayload::new(Some(title), Some(body)),
Some(Alert::Payload(mut payload)) => {
payload.body = Some(body);
payload
}
};
self.notification.payload.alert = Some(Alert::Payload(payload));
self
}
pub fn badge(mut self, number: u32) -> Self {
self.notification.payload.badge = Some(number);
self
}
pub fn sound<S: Into<String>>(mut self, sound: S) -> Self {
self.notification.payload.sound = Some(sound.into());
self
}
pub fn content_available(mut self) -> Self {
self.notification.payload.content_available = Some(true);
self
}
pub fn category(mut self, category: String) -> Self {
self.notification.payload.category = Some(category);
self
}
pub fn thread_id(mut self, thread_id: String) -> Self {
self.notification.payload.thread_id = Some(thread_id);
self
}
pub fn id(mut self, id: Uuid) -> Self {
self.notification.id = Some(id);
self
}
pub fn expiration(mut self, expiration: u64) -> Self {
self.notification.expiration = Some(expiration);
self
}
pub fn priority(mut self, priority: Priority) -> Self {
self.notification.priority = Some(priority);
self
}
pub fn collapse_id(mut self, id: CollapseId) -> Self {
self.notification.collapse_id = Some(id);
self
}
pub fn build(self) -> Notification {
self.notification
}
}