diff --git a/services/tunnelbroker/rust-notifications/src/apns.rs b/services/tunnelbroker/rust-notifications/src/apns.rs --- a/services/tunnelbroker/rust-notifications/src/apns.rs +++ b/services/tunnelbroker/rust-notifications/src/apns.rs @@ -1,8 +1,10 @@ +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( @@ -11,7 +13,7 @@ device_token: &str, message: &str, sandbox: bool, -) -> Result { +) -> Result { let mut certificate = File::open(certificate_path)?; let endpoint = if sandbox { Endpoint::Sandbox @@ -25,6 +27,25 @@ }; let builder = PlainNotificationBuilder::new(message); let payload = builder.build(device_token, options); - 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 { + 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 --- a/services/tunnelbroker/rust-notifications/src/lib.rs +++ b/services/tunnelbroker/rust-notifications/src/lib.rs @@ -1,3 +1,4 @@ +use crate::ffi::apns_status; use anyhow::Result; use lazy_static::lazy_static; use tokio::runtime::Runtime; @@ -6,6 +7,12 @@ #[cxx::bridge] mod ffi { + #[cxx_name = "apnsReturnStatus"] + enum apns_status { + Ok, + Unregistered, + BadDeviceToken, + } extern "Rust" { #[cxx_name = "sendNotifToAPNS"] fn send_notif_to_apns( @@ -14,7 +21,7 @@ device_token: &str, message: &str, sandbox: bool, - ) -> Result; + ) -> Result; #[cxx_name = "sendNotifToFCM"] fn send_notif_to_fcm( @@ -37,7 +44,7 @@ device_token: &str, message: &str, sandbox: bool, -) -> Result { +) -> Result { RUNTIME.block_on(apns::send_by_a2_client( certificate_path, certificate_password,