Home
Softono
expo-push-notification-client-rust

expo-push-notification-client-rust

Open source Apache-2.0 Rust
45
Stars
6
Forks
1
Issues
1
Watchers
1 week
Last Commit

About expo-push-notification-client-rust

Expo Push Notification Client for Rust

Platforms

Web Self-hosted

Languages

Rust

Expo Push Notification Client for Rust

This is a community Expo Push Notification Client for Rust.

ci crates.io docs.rs license

Client (ReactNative with Expo)

You need to get Expo Push Token from Expo SDK and send it to Expo server first. See docs for more details.

Server (Rust)

Install

cargo add expo_push_notification_client

Usage

use expo_push_notification_client::{Expo, ExpoClientOptions, ExpoPushMessage};

// Initialize Expo client
let expo = Expo::new(ExpoClientOptions {
    access_token: Some(access_token),
});

// Define Expo Push Tokens to send notifications to
let expo_push_tokens = ["ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]"];

// Build Expo Push Message with specified tokens
let expo_push_message = ExpoPushMessage::builder(expo_push_tokens).build()?;

// Send push notifications using Expo client
let tickets = expo.send_push_notifications(expo_push_message).await;

// Extract push notification IDs from tickets
let mut expo_push_ids = vec![];
for ticket in tickets {
    match ticket {
        ExpoPushTicket::Ok(ticket) => {
            expo_push_ids.push(ticket.id);
        }
        ExpoPushTicket::Error(e) => {
            // Handle error
        }
    }
}

// Retrieve push notification receipts using Expo client
expo.get_push_notification_receipts(expo_push_ids).await;

Additionally, you can further customize the ExpoPushMessage by adding more options. Refer to the docs for more details.

use expo_push_notification_client::{Expo, ExpoClientOptions, ExpoPushMessage, Priority, Sound, InterruptionLevel};

// Build Expo Push Message with detailed configurations
#[derive(Serialize)]
struct Data {
    data: String,
}

let expo_push_message = ExpoPushMessage::builder(["ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]"])
    .body("body")
    .data(&Data { data: "data".to_string()})?
    .ttl(100)
    .expiration(100)
    .priority(Priority::High)
    .subtitle("subtitle")
    .sound(Sound::Default)
    .badge(1)
    .channel_id("channel_id")
    .category_id("category_id")
    .mutable_content(true)
    .interruption_level(InterruptionLevel::TimeSensitive)
    .title("title")
    .build()?;

TLS Backend

This crate uses reqwest for HTTP requests. By default, it uses reqwest's default-tls feature, which currently enables rustls, a TLS backend written in Rust. You can choose a different TLS backend by disabling the default features and enabling one of the following features in your Cargo.toml.

Using native-tls

expo_push_notification_client = { version = "2.0.0", default-features = false, features = ["native-tls"] }

Using rustls (explicitly)

If you have disabled default features but still want to use rustls, you can enable it explicitly.

expo_push_notification_client = { version = "2.0.0", default-features = false, features = ["rustls"] }