diff options
author | Santo Cariotti <santo@dcariotti.me> | 2024-09-05 14:21:32 +0200 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2024-09-05 14:21:32 +0200 |
commit | f56b18fd28054aa5b241a897d22bcfc58c321286 (patch) | |
tree | 10908e8ada3641be175f1af9857c3be6b7489f7c /src/expo.rs | |
parent | b592d19f1a74a32df0aaed7dbf7484d0ef5ad35a (diff) |
Send notifications by Expo API
Diffstat (limited to 'src/expo.rs')
-rw-r--r-- | src/expo.rs | 31 |
1 files changed, 31 insertions, 0 deletions
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<Expo> = 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<String>, 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(()) +} |