diff --git a/services/tunnelbroker/rust-notifications/src/apns.rs b/services/tunnelbroker/rust-notifications/src/apns.rs index bfc419e88..49cefba89 100644 --- a/services/tunnelbroker/rust-notifications/src/apns.rs +++ b/services/tunnelbroker/rust-notifications/src/apns.rs @@ -1,33 +1,56 @@ +use crate::ffi::apns_status; use a2::{ - Client, Endpoint, NotificationBuilder, NotificationOptions, - PlainNotificationBuilder, + Client, Endpoint, Error, + ErrorReason::{BadDeviceToken, Unregistered}, + NotificationBuilder, NotificationOptions, PlainNotificationBuilder, }; -use anyhow::Result; +use anyhow::{anyhow, Result}; use std::fs::File; pub async fn send_by_a2_client( certificate_path: &str, certificate_password: &str, device_token: &str, topic: &str, message: &str, sandbox: bool, -) -> Result { +) -> Result { let mut certificate = File::open(certificate_path)?; let endpoint = if sandbox { Endpoint::Sandbox } else { Endpoint::Production }; let client = Client::certificate(&mut certificate, certificate_password, endpoint)?; let options = NotificationOptions { apns_topic: Some(topic), ..Default::default() }; let builder = PlainNotificationBuilder::new(message); let mut payload = builder.build(device_token, options); payload.aps.content_available = Some(1); - let response = client.send(payload).await?; - Ok(response.code) + match client.send(payload).await { + Ok(_) => Ok(apns_status::Ok), + Err(Error::ResponseError(response)) => { + if let Some(error_body) = response.error { + match error_body.reason { + // We are returning `Ok` with the error types here to distinguish the exact + // error type in a C++ side + BadDeviceToken => Ok(apns_status::BadDeviceToken), + Unregistered => Ok(apns_status::Unregistered), + _ => Err(anyhow!( + "Notification was not accepted by APNs, reason: {:?}", + error_body.reason + )), + } + } else { + Err(anyhow!( + "Unhandled response error from APNs, response: {:?}", + response + )) + } + } + Err(error) => Err(anyhow::Error::new(error)), + } } diff --git a/services/tunnelbroker/rust-notifications/src/lib.rs b/services/tunnelbroker/rust-notifications/src/lib.rs index 50962ff03..030bf7532 100644 --- a/services/tunnelbroker/rust-notifications/src/lib.rs +++ b/services/tunnelbroker/rust-notifications/src/lib.rs @@ -1,70 +1,77 @@ +use crate::ffi::apns_status; use anyhow::Result; use env_logger; use lazy_static::lazy_static; use log::info; use tokio::runtime::Runtime; pub mod apns; pub mod fcm; #[cxx::bridge] mod ffi { + #[cxx_name = "apnsReturnStatus"] + enum apns_status { + Ok, + Unregistered, + BadDeviceToken, + } extern "Rust" { #[cxx_name = "sendNotifToAPNS"] fn send_notif_to_apns( certificate_path: &str, certificate_password: &str, device_token: &str, topic: &str, message: &str, sandbox: bool, - ) -> Result; + ) -> 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! { pub static ref RUNTIME: Runtime = { env_logger::init(); info!("Tokio runtime initialization"); Runtime::new().unwrap() }; } pub fn send_notif_to_apns( certificate_path: &str, certificate_password: &str, device_token: &str, topic: &str, message: &str, sandbox: bool, -) -> Result { +) -> Result { RUNTIME.block_on(apns::send_by_a2_client( certificate_path, certificate_password, device_token, topic, 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, )) }