summaryrefslogtreecommitdiff
path: root/src/expo.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/expo.rs')
-rw-r--r--src/expo.rs38
1 files changed, 21 insertions, 17 deletions
diff --git a/src/expo.rs b/src/expo.rs
index 1a6a4e5..f4cecc6 100644
--- a/src/expo.rs
+++ b/src/expo.rs
@@ -1,31 +1,35 @@
-use expo_push_notification_client::{Expo, ExpoClientOptions, ExpoPushMessage, ValidationError};
+use expo_push_notification_client::{Expo, ExpoClientOptions, ExpoPushMessage};
-/// Connection to an Expo client
-static mut EXPO_CONNECTION: Option<Expo> = None;
+use crate::errors::AppError;
/// Setup a new Expo API
-pub fn setup(access_token: String) {
- unsafe {
- EXPO_CONNECTION = Some(Expo::new(ExpoClientOptions {
- access_token: Some(access_token),
- }))
- }
+pub fn setup(access_token: String) -> Expo {
+ Expo::new(ExpoClientOptions {
+ access_token: Some(access_token),
+ })
}
/// Send notifications using Expo
-pub async fn send(tokens: Vec<String>, body: String, title: String) -> Result<(), ValidationError> {
- let expo = unsafe {
- EXPO_CONNECTION
- .clone()
- .expect("You need to call `setup()` first")
- };
-
+pub async fn send(
+ expo: Expo,
+ tokens: Vec<String>,
+ body: String,
+ title: String,
+) -> Result<(), AppError> {
let expo_push_message = ExpoPushMessage::builder(tokens)
.body(body)
.title(title)
.build()?;
- let _ = expo.send_push_notifications(expo_push_message).await;
+ if expo
+ .send_push_notifications(expo_push_message)
+ .await
+ .is_err()
+ {
+ return Err(AppError::BadRequest(
+ "Expo Notifications sending".to_string(),
+ ));
+ }
Ok(())
}