Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add implementation to support safari web push (#57)
- Loading branch information
Showing
7 changed files
with
139 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
use crate::request::notification::{NotificationBuilder, NotificationOptions}; | ||
use crate::request::payload::{APSAlert, Payload, APS}; | ||
use std::collections::BTreeMap; | ||
|
||
#[derive(Serialize, Debug, Clone)] | ||
#[serde(rename_all = "kebab-case")] | ||
pub struct WebPushAlert<'a> { | ||
pub title: &'a str, | ||
pub body: &'a str, | ||
pub action: &'a str, | ||
} | ||
|
||
/// A builder to create a simple APNs notification payload. | ||
/// | ||
/// # Example | ||
/// | ||
/// ```rust | ||
/// # use a2::request::notification::{NotificationBuilder, WebNotificationBuilder, WebPushAlert}; | ||
/// # fn main() { | ||
/// let mut builder = WebNotificationBuilder::new(WebPushAlert {title: "Hello", body: "World", action: "View"}, &["arg1"]); | ||
/// builder.set_sound("prööt"); | ||
/// let payload = builder.build("device_id", Default::default()) | ||
/// .to_json_string().unwrap(); | ||
/// # } | ||
/// ``` | ||
pub struct WebNotificationBuilder<'a> { | ||
alert: WebPushAlert<'a>, | ||
sound: Option<&'a str>, | ||
url_args: &'a [&'a str], | ||
} | ||
|
||
impl<'a> WebNotificationBuilder<'a> { | ||
/// Creates a new builder with the minimum amount of content. | ||
/// | ||
/// ```rust | ||
/// # use a2::request::notification::{WebNotificationBuilder, NotificationBuilder, WebPushAlert}; | ||
/// # fn main() { | ||
/// let mut builder = WebNotificationBuilder::new(WebPushAlert {title: "Hello", body: "World", action: "View"}, &["arg1"]); | ||
/// let payload = builder.build("token", Default::default()); | ||
/// | ||
/// assert_eq!( | ||
/// "{\"aps\":{\"alert\":{\"action\":\"View\",\"body\":\"World\",\"title\":\"Hello\"},\"url-args\":[\"arg1\"]}}", | ||
/// &payload.to_json_string().unwrap() | ||
/// ); | ||
/// # } | ||
/// ``` | ||
pub fn new(alert: WebPushAlert<'a>, url_args: &'a [&'a str]) -> WebNotificationBuilder<'a> { | ||
WebNotificationBuilder { | ||
alert, | ||
sound: None, | ||
url_args: url_args, | ||
} | ||
} | ||
|
||
/// File name of the custom sound to play when receiving the notification. | ||
/// | ||
/// ```rust | ||
/// # use a2::request::notification::{WebNotificationBuilder, NotificationBuilder, WebPushAlert}; | ||
/// # fn main() { | ||
/// let mut builder = WebNotificationBuilder::new(WebPushAlert {title: "Hello", body: "World", action: "View"}, &["arg1"]); | ||
/// builder.set_sound("meow"); | ||
/// let payload = builder.build("token", Default::default()); | ||
/// | ||
/// assert_eq!( | ||
/// "{\"aps\":{\"alert\":{\"action\":\"View\",\"body\":\"World\",\"title\":\"Hello\"},\"sound\":\"meow\",\"url-args\":[\"arg1\"]}}", | ||
/// &payload.to_json_string().unwrap() | ||
/// ); | ||
/// # } | ||
/// ``` | ||
pub fn set_sound(&mut self, sound: &'a str) -> &mut Self { | ||
self.sound = Some(sound); | ||
self | ||
} | ||
} | ||
|
||
impl<'a> NotificationBuilder<'a> for WebNotificationBuilder<'a> { | ||
fn build(self, device_token: &'a str, options: NotificationOptions<'a>) -> Payload<'a> { | ||
Payload { | ||
aps: APS { | ||
alert: Some(APSAlert::WebPush(self.alert)), | ||
badge: None, | ||
sound: self.sound, | ||
content_available: None, | ||
category: None, | ||
mutable_content: None, | ||
url_args: Some(self.url_args), | ||
}, | ||
device_token, | ||
options, | ||
data: BTreeMap::new(), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_webpush_notification() { | ||
let payload = WebNotificationBuilder::new( | ||
WebPushAlert { | ||
action: "View", | ||
title: "Hello", | ||
body: "world", | ||
}, | ||
&vec!["arg1"], | ||
) | ||
.build("device-token", Default::default()) | ||
.to_json_string() | ||
.unwrap(); | ||
|
||
let expected_payload = json!({ | ||
"aps": { | ||
"alert": { | ||
"body": "world", | ||
"action": "View", | ||
"title": "Hello" | ||
}, | ||
"url-args": ["arg1"] | ||
} | ||
}) | ||
.to_string(); | ||
|
||
assert_eq!(expected_payload, payload); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters