From f56b18fd28054aa5b241a897d22bcfc58c321286 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Thu, 5 Sep 2024 14:21:32 +0200 Subject: Send notifications by Expo API --- src/expo.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/expo.rs (limited to 'src/expo.rs') diff --git a/src/expo.rs b/src/expo.rs new file mode 100644 index 0000000..1a6a4e5 --- /dev/null +++ b/src/expo.rs @@ -0,0 +1,31 @@ +use expo_push_notification_client::{Expo, ExpoClientOptions, ExpoPushMessage, ValidationError}; + +/// Connection to an Expo client +static mut EXPO_CONNECTION: Option = None; + +/// Setup a new Expo API +pub fn setup(access_token: String) { + unsafe { + EXPO_CONNECTION = Some(Expo::new(ExpoClientOptions { + access_token: Some(access_token), + })) + } +} + +/// Send notifications using Expo +pub async fn send(tokens: Vec, body: String, title: String) -> Result<(), ValidationError> { + let expo = unsafe { + EXPO_CONNECTION + .clone() + .expect("You need to call `setup()` first") + }; + + let expo_push_message = ExpoPushMessage::builder(tokens) + .body(body) + .title(title) + .build()?; + + let _ = expo.send_push_notifications(expo_push_message).await; + + Ok(()) +} -- cgit v1.2.3-18-g5258