1 unstable release

0.1.3 Nov 5, 2024

#28 in #push

MIT/Apache

20KB
458 lines

JPush for Rust

App Push SDK for Rust

config toml

[jpush]
url = "https:// api. jpush. cn/ v3/ push"
app_key = "your app key"
secret = "your secret"

use demo

use jpush::JPushClient;
use jpush::config::JPushConfig;
use jpush::msg::{JPushMessage, AndroidNotification, HarmonyOSNotification, IosNotification, Message, Platform, PlatformType};

async fn push() {
    let config = JPushConfig {
        url: "https://api.jpush.cn/v3/push".to_string(),
        app_key: "your app key".to_string(),
        secret: "your secret".to_string(),
    };
    let client = JPushClient::new(config);
    let mut message = jpush::msg::JPushMessage::default();
    message
        .set_platform(Platform::VecString(vec![PlatformType::Ios, PlatformType::Android, PlatformType::HarmonyOS]))
        // .set_platform(Platform::String(PlatformType::All))
        .add_alias(vec![1135])
        .set_notification_alert("hello world".to_string())
        .set_ios_notification(IosNotification::new("hello world".to_string(), None))
        .set_android_notification(AndroidNotification::new("hello ".to_string(), "world".to_string(), None))
        .set_hmos_notification(HarmonyOSNotification::new("hello ".to_string(), "world".to_string(), "im".to_string(), None))
        .set_message(Message::new("hello ".to_string(), "world".to_string(), None))
        .set_options(jpush::msg::Options::new(86400, true, 1));
    println!("{:?}", message);
    let res = client.send(message).await;
    match res {
        Ok(res) => {
            println!("{:?}", res)
        }
        Err(e) => {
            println!("{}", e)
        }
    }
}

depends on spring-rs use demo

[dependencies]
jpush = { version = "0.1.2", features = ["spring-rs"] }
use jpush::JPushPlugin;
use jpush::client::JPushClient;
use jpush::msg::{JPushMessage, AndroidNotification, HarmonyOSNotification, IosNotification, Message, Platform, PlatformType};
use spring::App;

#[tokio::main]
async fn main() {
    App::new()
        .config_file("./config/job.toml")
        .add_plugin(JPushPlugin)
        .run()
        .await;
}

async fn push(Component(client): Component<JPushClient>) -> Result<(), Box<dyn std::error::Error>> {
    let mut message = jpush::msg::JPushMessage::default();
        message
        .set_platform(Platform::VecString(vec![PlatformType::Ios, PlatformType::Android, PlatformType::HarmonyOS]))
        // .set_platform(Platform::String(PlatformType::All))
        .add_alias(vec![1135])
        .set_notification_alert("hello world".to_string())
        .set_ios_notification(IosNotification::new("hello world".to_string(), None))
        .set_android_notification(AndroidNotification::new("hello ".to_string(), "world".to_string(), None))
        .set_hmos_notification(HarmonyOSNotification::new("hello ".to_string(), "world".to_string(), "im".to_string(), None))
        .set_message(Message::new("hello ".to_string(), "world".to_string(), None))
        .set_options(jpush::msg::Options::new(86400, true, 1));
    println!("{:?}", message);
    let res = client.send(message).await;
    match res {
        Ok(res) => {
            println!("{:?}", res)
        }
        Err(e) => {
            println!("{}", e)
        }
    }
}


Dependencies

~4–17MB
~223K SLoC