diff --git a/services/tunnelbroker/rust-notifications/src/fcm.rs b/services/tunnelbroker/rust-notifications/src/fcm.rs new file mode 100644 index 000000000..a065034fc --- /dev/null +++ b/services/tunnelbroker/rust-notifications/src/fcm.rs @@ -0,0 +1,24 @@ +use anyhow::{anyhow, Result}; +use fcm::{Client, MessageBuilder, NotificationBuilder}; + +pub async fn send_by_fcm_client( + fcm_api_key: &str, + device_registration_id: &str, + message_title: &str, + message_body: &str, +) -> Result { + let client = Client::new(); + let mut notification_builder = NotificationBuilder::new(); + notification_builder.title(message_title); + notification_builder.body(message_body); + + let notification = notification_builder.finalize(); + let mut message_builder = + MessageBuilder::new(fcm_api_key, device_registration_id); + message_builder.notification(notification); + let result = client.send(message_builder.finalize()).await?; + match result.message_id { + Some(message_id) => Ok(message_id), + None => Err(anyhow!("FCM client returned an empty message id")), + } +} diff --git a/services/tunnelbroker/rust-notifications/src/lib.rs b/services/tunnelbroker/rust-notifications/src/lib.rs index c3cd17252..262937df0 100644 --- a/services/tunnelbroker/rust-notifications/src/lib.rs +++ b/services/tunnelbroker/rust-notifications/src/lib.rs @@ -1,39 +1,62 @@ use anyhow::Result; use lazy_static::lazy_static; use tokio::runtime::Runtime; pub mod apns; +pub mod fcm; #[cxx::bridge] mod ffi { extern "Rust" { #[cxx_name = "sendNotifToAPNS"] fn send_notif_to_apns( certificate_path: &str, certificate_password: &str, device_token: &str, message: &str, sandbox: bool, ) -> Result; + + #[cxx_name = "sendNotifToFCM"] + fn send_notif_to_fcm( + fcm_api_key: &str, + device_registration_id: &str, + message_title: &str, + message_body: &str, + ) -> Result; } } lazy_static! { // Lazy static Tokio runtime initialization pub static ref RUNTIME: Runtime = Runtime::new().unwrap(); } pub fn send_notif_to_apns( certificate_path: &str, certificate_password: &str, device_token: &str, message: &str, sandbox: bool, ) -> Result { RUNTIME.block_on(apns::send_by_a2_client( certificate_path, certificate_password, device_token, message, sandbox, )) } + +pub fn send_notif_to_fcm( + fcm_api_key: &str, + device_registration_id: &str, + message_title: &str, + message_body: &str, +) -> Result { + RUNTIME.block_on(fcm::send_by_fcm_client( + fcm_api_key, + device_registration_id, + message_title, + message_body, + )) +}