diff --git a/services/tunnelbroker/rust-notifications/src/fcm.rs b/services/tunnelbroker/rust-notifications/src/fcm.rs new file mode 100644 --- /dev/null +++ b/services/tunnelbroker/rust-notifications/src/fcm.rs @@ -0,0 +1,20 @@ +use fcm::{Client, FcmResponse, MessageBuilder, NotificationBuilder}; + +#[allow(non_snake_case)] +pub async fn sendByFCMClient( + fcmAPIKey: &str, + deviceRegistrationID: &str, + messageTitle: &str, + messageBody: &str, +) -> FcmResponse { + let client = Client::new(); + let mut notificationBuilder = NotificationBuilder::new(); + notificationBuilder.title(messageTitle); + notificationBuilder.body(messageBody); + + let notification = notificationBuilder.finalize(); + let mut message_builder = + MessageBuilder::new(fcmAPIKey, deviceRegistrationID); + message_builder.notification(notification); + client.send(message_builder.finalize()).await.unwrap() +} diff --git a/services/tunnelbroker/rust-notifications/src/lib.rs b/services/tunnelbroker/rust-notifications/src/lib.rs --- a/services/tunnelbroker/rust-notifications/src/lib.rs +++ b/services/tunnelbroker/rust-notifications/src/lib.rs @@ -3,6 +3,7 @@ use tokio::runtime::Runtime; pub mod apns; +pub mod fcm; #[cxx::bridge] mod ffi { @@ -14,6 +15,12 @@ message: &str, sandbox: bool, ) -> bool; + fn sendNotifToFCM( + fcmAPIKey: &str, + deviceRegistrationID: &str, + messageTitle: &str, + messageBody: &str, + ) -> bool; } } @@ -58,3 +65,39 @@ } true } + +#[allow(non_snake_case)] +pub fn sendNotifToFCM( + fcmAPIKey: &str, + deviceRegistrationID: &str, + messageTitle: &str, + messageBody: &str, +) -> bool { + // Because of C++ doesn't know how to 'Panic' we need to catch all the panics + let result = std::panic::catch_unwind(|| { + RUNTIME.block_on(async { + fcm::sendByFCMClient( + fcmAPIKey, + deviceRegistrationID, + messageTitle, + messageBody, + ) + .await; + }) + }); + match result { + Ok(runtimeResult) => { + info!( + "Push notification to device {:?} was sent to FCM with the result {:?}", + deviceRegistrationID, runtimeResult + ); + } + Err(err) => { + error!("Error: Panic in the Rust notification library in a sendNotifToFCM: {:?}", + err + ); + return false; + } + } + true +}