Skip to content

Commit

Permalink
Merge pull request #19 from monal-im/feature/monal-version
Browse files Browse the repository at this point in the history
Implement APNS additional data
  • Loading branch information
tmolitor-stud-tu authored Jun 5, 2024
2 parents 7448e7c + ed9d167 commit 411b0b9
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions fpush-apns/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ async-trait = "^0.1"

serde_derive = "^1.0"
serde = { version = "^1.0", features = ["derive"] }
serde_json = "1.0"

a2 = { git = "https://github.com/monal-im/apns2.git", branch = "faltheide/sync" }

Expand Down
7 changes: 7 additions & 0 deletions fpush-apns/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use std::collections::HashMap;
use serde::Deserialize;
use serde_json::Value;

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AppleApnsConfig {
cert_file_path: String,
cert_password: String,
topic: String,
additional_data: Option<HashMap<String, Value>>,
#[serde(default = "ApnsEndpoint::production")]
environment: ApnsEndpoint,
}
Expand All @@ -29,6 +32,10 @@ impl AppleApnsConfig {
ApnsEndpoint::Sandbox => a2::Endpoint::Sandbox,
}
}

pub fn additional_data(&self) -> &Option<HashMap<String, Value>> {
&self.additional_data
}
}

#[derive(Debug, Deserialize)]
Expand Down
14 changes: 13 additions & 1 deletion fpush-apns/src/push.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::time::SystemTime;
use std::collections::HashMap;

use a2::{
Client, DefaultNotificationBuilder, NotificationBuilder, NotificationOptions, Priority,
Expand All @@ -8,11 +9,13 @@ use fpush_traits::push::{PushError, PushResult, PushTrait};

use async_trait::async_trait;
use log::{debug, error};
use serde_json::Value;

use crate::AppleApnsConfig;
pub struct FpushApns {
apns: a2::client::Client,
topic: String,
additional_data: Option<HashMap<String, Value>>,
}

impl FpushApns {
Expand All @@ -35,6 +38,7 @@ impl FpushApns {
let wrapped_conn = Self {
apns: apns_conn,
topic: apns_config.topic().to_string(),
additional_data: apns_config.additional_data().clone(),
};
Ok(wrapped_conn)
}
Expand All @@ -56,7 +60,7 @@ impl PushTrait for FpushApns {
.set_body("New Message?")
.set_mutable_content()
.set_sound("default");
let payload = notification_builder.build(
let mut payload = notification_builder.build(
&token,
NotificationOptions {
apns_priority: Some(Priority::High),
Expand All @@ -68,6 +72,14 @@ impl PushTrait for FpushApns {
..Default::default()
},
);
match &self.additional_data {
None => {}
Some(additional_data) => {
for (key, value) in additional_data {
payload.add_custom_data(key, value).unwrap();
}
}
}
log::debug!(
"Payload send to apple: {}",
payload.clone().to_json_string().unwrap()
Expand Down

0 comments on commit 411b0b9

Please sign in to comment.