pub struct SyncBarkMessageBuilder<'a> { /* private fields */ }Expand description
同步 Bark 消息构建器
与 SyncBarkClient 关联的消息构建器,提供流畅的 API 来构建和直接发送消息。
它包装了通用的 BarkMessageBuilder 并添加了 send() 方法。
§特性
- 支持所有
BarkMessageBuilder的方法 - 提供
send()方法直接发送消息 - 支持
build()方法构建消息对象
§示例
use bark_rs::{SyncBarkClient, Level};
let client = SyncBarkClient::with_device_key("https://api.day.app", "key");
// 构建并发送
let response = client
.message()
.title("标题")
.body("内容")
.level(Level::Active)
.send()?;
// 或者只构建
let message = client
.message()
.body("内容")
.build();Implementations§
Source§impl<'a> SyncBarkMessageBuilder<'a>
impl<'a> SyncBarkMessageBuilder<'a>
Sourcepub fn body(self, body: &str) -> Self
pub fn body(self, body: &str) -> Self
设置推送内容(必需)
详细说明请参见 BarkMessageBuilder::body。
Examples found in repository?
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4 // 创建同步客户端
5 let client = SyncBarkClient::new("https://api.day.app");
6
7 // 批量推送到多个设备
8 let response = client
9 .message()
10 .device_keys(vec![
11 "QJ48vPutCAsPW2B6pE2A3a".to_string(),
12 "device_key_2".to_string(),
13 "device_key_3".to_string(),
14 ])
15 .title("批量推送通知")
16 .body("这是一个发送给多个设备的批量消息")
17 .level(Level::TimeSensitive)
18 .volume(7)
19 .badge(1)
20 .group("批量通知")
21 .send()?;
22
23 println!(
24 "批量推送成功: code={}, message={}",
25 response.code, response.message
26 );
27
28 Ok(())
29}More examples
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4 // 创建同步客户端(带默认设备密钥)
5 let client = SyncBarkClient::with_device_key("https://api.day.app", "QJ48vPutCAsPW2B6pE2A3a");
6
7 // 方式1: 使用客户端的链式调用
8 let response = client
9 .message()
10 .title("同步推送")
11 .body("这是同步客户端发送的消息")
12 .level(Level::Active)
13 .volume(7)
14 .send()?;
15
16 println!(
17 "同步推送成功: code={}, message={}",
18 response.code, response.message
19 );
20
21 // 方式2: 先构建消息,再发送
22 let message = BarkMessage::builder()
23 .title("独立构建的消息")
24 .body("消息构建与发送分离")
25 .level(Level::Critical)
26 .sound("alarm")
27 .badge(1)
28 .build();
29
30 let response = client.send(&message)?;
31 println!(
32 "独立消息发送成功: code={}, message={}",
33 response.code, response.message
34 );
35
36 Ok(())
37}3fn main() {
4 println!("🚨 演示错误处理");
5
6 // 创建没有默认设备密钥的客户端
7 let client = SyncBarkClient::new("https://api.day.app");
8
9 // 尝试发送没有设备密钥的消息
10 let message_without_key = BarkMessage::builder()
11 .title("错误演示")
12 .body("这个消息没有设备密钥")
13 .build();
14
15 match client.send(&message_without_key) {
16 Ok(_) => println!("❌ 意外成功"),
17 Err(BarkError::MissingDeviceKey) => {
18 println!("✅ 正确捕获到缺少设备密钥错误");
19 }
20 Err(e) => println!("❓ 其他错误: {}", e),
21 }
22
23 // 演示正确的错误处理模式
24 let result = client
25 .message()
26 .device_key("QJ48vPutCAsPW2B6pE2A3a")
27 .title("正确的消息")
28 .body("这个消息有设备密钥")
29 .send();
30
31 match result {
32 Ok(response) => {
33 println!(
34 "✅ 消息发送成功: code={}, message={}",
35 response.code, response.message
36 );
37 }
38 Err(BarkError::RequestError(e)) => {
39 println!("❌ 网络请求错误: {}", e);
40 }
41 Err(BarkError::MissingDeviceKey) => {
42 println!("❌ 缺少设备密钥");
43 }
44 Err(BarkError::SerializationError(e)) => {
45 println!("❌ 序列化错误: {}", e);
46 }
47 Err(BarkError::InvalidUrl) => {
48 println!("❌ 无效URL");
49 }
50 }
51
52 println!("🎉 错误处理演示完成!");
53}Sourcepub fn title(self, title: &str) -> Self
pub fn title(self, title: &str) -> Self
设置推送标题
详细说明请参见 BarkMessageBuilder::title。
Examples found in repository?
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4 // 创建同步客户端
5 let client = SyncBarkClient::new("https://api.day.app");
6
7 // 批量推送到多个设备
8 let response = client
9 .message()
10 .device_keys(vec![
11 "QJ48vPutCAsPW2B6pE2A3a".to_string(),
12 "device_key_2".to_string(),
13 "device_key_3".to_string(),
14 ])
15 .title("批量推送通知")
16 .body("这是一个发送给多个设备的批量消息")
17 .level(Level::TimeSensitive)
18 .volume(7)
19 .badge(1)
20 .group("批量通知")
21 .send()?;
22
23 println!(
24 "批量推送成功: code={}, message={}",
25 response.code, response.message
26 );
27
28 Ok(())
29}More examples
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4 // 创建同步客户端(带默认设备密钥)
5 let client = SyncBarkClient::with_device_key("https://api.day.app", "QJ48vPutCAsPW2B6pE2A3a");
6
7 // 方式1: 使用客户端的链式调用
8 let response = client
9 .message()
10 .title("同步推送")
11 .body("这是同步客户端发送的消息")
12 .level(Level::Active)
13 .volume(7)
14 .send()?;
15
16 println!(
17 "同步推送成功: code={}, message={}",
18 response.code, response.message
19 );
20
21 // 方式2: 先构建消息,再发送
22 let message = BarkMessage::builder()
23 .title("独立构建的消息")
24 .body("消息构建与发送分离")
25 .level(Level::Critical)
26 .sound("alarm")
27 .badge(1)
28 .build();
29
30 let response = client.send(&message)?;
31 println!(
32 "独立消息发送成功: code={}, message={}",
33 response.code, response.message
34 );
35
36 Ok(())
37}3fn main() {
4 println!("🚨 演示错误处理");
5
6 // 创建没有默认设备密钥的客户端
7 let client = SyncBarkClient::new("https://api.day.app");
8
9 // 尝试发送没有设备密钥的消息
10 let message_without_key = BarkMessage::builder()
11 .title("错误演示")
12 .body("这个消息没有设备密钥")
13 .build();
14
15 match client.send(&message_without_key) {
16 Ok(_) => println!("❌ 意外成功"),
17 Err(BarkError::MissingDeviceKey) => {
18 println!("✅ 正确捕获到缺少设备密钥错误");
19 }
20 Err(e) => println!("❓ 其他错误: {}", e),
21 }
22
23 // 演示正确的错误处理模式
24 let result = client
25 .message()
26 .device_key("QJ48vPutCAsPW2B6pE2A3a")
27 .title("正确的消息")
28 .body("这个消息有设备密钥")
29 .send();
30
31 match result {
32 Ok(response) => {
33 println!(
34 "✅ 消息发送成功: code={}, message={}",
35 response.code, response.message
36 );
37 }
38 Err(BarkError::RequestError(e)) => {
39 println!("❌ 网络请求错误: {}", e);
40 }
41 Err(BarkError::MissingDeviceKey) => {
42 println!("❌ 缺少设备密钥");
43 }
44 Err(BarkError::SerializationError(e)) => {
45 println!("❌ 序列化错误: {}", e);
46 }
47 Err(BarkError::InvalidUrl) => {
48 println!("❌ 无效URL");
49 }
50 }
51
52 println!("🎉 错误处理演示完成!");
53}Sourcepub fn subtitle(self, subtitle: &str) -> Self
pub fn subtitle(self, subtitle: &str) -> Self
设置推送副标题
详细说明请参见 BarkMessageBuilder::subtitle。
Sourcepub fn device_key(self, device_key: &str) -> Self
pub fn device_key(self, device_key: &str) -> Self
设置单个设备密钥
详细说明请参见 BarkMessageBuilder::device_key。
Examples found in repository?
3fn main() {
4 println!("🚨 演示错误处理");
5
6 // 创建没有默认设备密钥的客户端
7 let client = SyncBarkClient::new("https://api.day.app");
8
9 // 尝试发送没有设备密钥的消息
10 let message_without_key = BarkMessage::builder()
11 .title("错误演示")
12 .body("这个消息没有设备密钥")
13 .build();
14
15 match client.send(&message_without_key) {
16 Ok(_) => println!("❌ 意外成功"),
17 Err(BarkError::MissingDeviceKey) => {
18 println!("✅ 正确捕获到缺少设备密钥错误");
19 }
20 Err(e) => println!("❓ 其他错误: {}", e),
21 }
22
23 // 演示正确的错误处理模式
24 let result = client
25 .message()
26 .device_key("QJ48vPutCAsPW2B6pE2A3a")
27 .title("正确的消息")
28 .body("这个消息有设备密钥")
29 .send();
30
31 match result {
32 Ok(response) => {
33 println!(
34 "✅ 消息发送成功: code={}, message={}",
35 response.code, response.message
36 );
37 }
38 Err(BarkError::RequestError(e)) => {
39 println!("❌ 网络请求错误: {}", e);
40 }
41 Err(BarkError::MissingDeviceKey) => {
42 println!("❌ 缺少设备密钥");
43 }
44 Err(BarkError::SerializationError(e)) => {
45 println!("❌ 序列化错误: {}", e);
46 }
47 Err(BarkError::InvalidUrl) => {
48 println!("❌ 无效URL");
49 }
50 }
51
52 println!("🎉 错误处理演示完成!");
53}Sourcepub fn device_keys(self, device_keys: Vec<String>) -> Self
pub fn device_keys(self, device_keys: Vec<String>) -> Self
设置多个设备密钥(批量推送)
详细说明请参见 BarkMessageBuilder::device_keys。
Examples found in repository?
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4 // 创建同步客户端
5 let client = SyncBarkClient::new("https://api.day.app");
6
7 // 批量推送到多个设备
8 let response = client
9 .message()
10 .device_keys(vec![
11 "QJ48vPutCAsPW2B6pE2A3a".to_string(),
12 "device_key_2".to_string(),
13 "device_key_3".to_string(),
14 ])
15 .title("批量推送通知")
16 .body("这是一个发送给多个设备的批量消息")
17 .level(Level::TimeSensitive)
18 .volume(7)
19 .badge(1)
20 .group("批量通知")
21 .send()?;
22
23 println!(
24 "批量推送成功: code={}, message={}",
25 response.code, response.message
26 );
27
28 Ok(())
29}Sourcepub fn level(self, level: Level) -> Self
pub fn level(self, level: Level) -> Self
设置推送级别
详细说明请参见 BarkMessageBuilder::level。
Examples found in repository?
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4 // 创建同步客户端
5 let client = SyncBarkClient::new("https://api.day.app");
6
7 // 批量推送到多个设备
8 let response = client
9 .message()
10 .device_keys(vec![
11 "QJ48vPutCAsPW2B6pE2A3a".to_string(),
12 "device_key_2".to_string(),
13 "device_key_3".to_string(),
14 ])
15 .title("批量推送通知")
16 .body("这是一个发送给多个设备的批量消息")
17 .level(Level::TimeSensitive)
18 .volume(7)
19 .badge(1)
20 .group("批量通知")
21 .send()?;
22
23 println!(
24 "批量推送成功: code={}, message={}",
25 response.code, response.message
26 );
27
28 Ok(())
29}More examples
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4 // 创建同步客户端(带默认设备密钥)
5 let client = SyncBarkClient::with_device_key("https://api.day.app", "QJ48vPutCAsPW2B6pE2A3a");
6
7 // 方式1: 使用客户端的链式调用
8 let response = client
9 .message()
10 .title("同步推送")
11 .body("这是同步客户端发送的消息")
12 .level(Level::Active)
13 .volume(7)
14 .send()?;
15
16 println!(
17 "同步推送成功: code={}, message={}",
18 response.code, response.message
19 );
20
21 // 方式2: 先构建消息,再发送
22 let message = BarkMessage::builder()
23 .title("独立构建的消息")
24 .body("消息构建与发送分离")
25 .level(Level::Critical)
26 .sound("alarm")
27 .badge(1)
28 .build();
29
30 let response = client.send(&message)?;
31 println!(
32 "独立消息发送成功: code={}, message={}",
33 response.code, response.message
34 );
35
36 Ok(())
37}Sourcepub fn volume(self, volume: u8) -> Self
pub fn volume(self, volume: u8) -> Self
设置铃声音量 (1-10)
详细说明请参见 BarkMessageBuilder::volume。
Examples found in repository?
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4 // 创建同步客户端
5 let client = SyncBarkClient::new("https://api.day.app");
6
7 // 批量推送到多个设备
8 let response = client
9 .message()
10 .device_keys(vec![
11 "QJ48vPutCAsPW2B6pE2A3a".to_string(),
12 "device_key_2".to_string(),
13 "device_key_3".to_string(),
14 ])
15 .title("批量推送通知")
16 .body("这是一个发送给多个设备的批量消息")
17 .level(Level::TimeSensitive)
18 .volume(7)
19 .badge(1)
20 .group("批量通知")
21 .send()?;
22
23 println!(
24 "批量推送成功: code={}, message={}",
25 response.code, response.message
26 );
27
28 Ok(())
29}More examples
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4 // 创建同步客户端(带默认设备密钥)
5 let client = SyncBarkClient::with_device_key("https://api.day.app", "QJ48vPutCAsPW2B6pE2A3a");
6
7 // 方式1: 使用客户端的链式调用
8 let response = client
9 .message()
10 .title("同步推送")
11 .body("这是同步客户端发送的消息")
12 .level(Level::Active)
13 .volume(7)
14 .send()?;
15
16 println!(
17 "同步推送成功: code={}, message={}",
18 response.code, response.message
19 );
20
21 // 方式2: 先构建消息,再发送
22 let message = BarkMessage::builder()
23 .title("独立构建的消息")
24 .body("消息构建与发送分离")
25 .level(Level::Critical)
26 .sound("alarm")
27 .badge(1)
28 .build();
29
30 let response = client.send(&message)?;
31 println!(
32 "独立消息发送成功: code={}, message={}",
33 response.code, response.message
34 );
35
36 Ok(())
37}Sourcepub fn badge(self, badge: u32) -> Self
pub fn badge(self, badge: u32) -> Self
设置应用角标数字
详细说明请参见 BarkMessageBuilder::badge。
Examples found in repository?
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4 // 创建同步客户端
5 let client = SyncBarkClient::new("https://api.day.app");
6
7 // 批量推送到多个设备
8 let response = client
9 .message()
10 .device_keys(vec![
11 "QJ48vPutCAsPW2B6pE2A3a".to_string(),
12 "device_key_2".to_string(),
13 "device_key_3".to_string(),
14 ])
15 .title("批量推送通知")
16 .body("这是一个发送给多个设备的批量消息")
17 .level(Level::TimeSensitive)
18 .volume(7)
19 .badge(1)
20 .group("批量通知")
21 .send()?;
22
23 println!(
24 "批量推送成功: code={}, message={}",
25 response.code, response.message
26 );
27
28 Ok(())
29}Sourcepub fn call(self, call: bool) -> Self
pub fn call(self, call: bool) -> Self
设置是否重复播放铃声
详细说明请参见 BarkMessageBuilder::call。
Sourcepub fn auto_copy(self, auto_copy: bool) -> Self
pub fn auto_copy(self, auto_copy: bool) -> Self
设置是否自动复制推送内容
详细说明请参见 BarkMessageBuilder::auto_copy。
Sourcepub fn copy(self, copy: &str) -> Self
pub fn copy(self, copy: &str) -> Self
设置自定义复制内容
详细说明请参见 BarkMessageBuilder::copy。
Sourcepub fn sound(self, sound: &str) -> Self
pub fn sound(self, sound: &str) -> Self
设置铃声名称
详细说明请参见 BarkMessageBuilder::sound。
Sourcepub fn icon(self, icon: &str) -> Self
pub fn icon(self, icon: &str) -> Self
设置自定义图标
详细说明请参见 BarkMessageBuilder::icon。
Sourcepub fn group(self, group: &str) -> Self
pub fn group(self, group: &str) -> Self
设置消息分组
详细说明请参见 BarkMessageBuilder::group。
Examples found in repository?
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4 // 创建同步客户端
5 let client = SyncBarkClient::new("https://api.day.app");
6
7 // 批量推送到多个设备
8 let response = client
9 .message()
10 .device_keys(vec![
11 "QJ48vPutCAsPW2B6pE2A3a".to_string(),
12 "device_key_2".to_string(),
13 "device_key_3".to_string(),
14 ])
15 .title("批量推送通知")
16 .body("这是一个发送给多个设备的批量消息")
17 .level(Level::TimeSensitive)
18 .volume(7)
19 .badge(1)
20 .group("批量通知")
21 .send()?;
22
23 println!(
24 "批量推送成功: code={}, message={}",
25 response.code, response.message
26 );
27
28 Ok(())
29}Sourcepub fn ciphertext(self, ciphertext: &str) -> Self
pub fn ciphertext(self, ciphertext: &str) -> Self
设置加密文本
详细说明请参见 BarkMessageBuilder::ciphertext。
Sourcepub fn is_archive(self, is_archive: bool) -> Self
pub fn is_archive(self, is_archive: bool) -> Self
设置是否保存到历史
详细说明请参见 BarkMessageBuilder::is_archive。
Sourcepub fn url(self, url: &str) -> Self
pub fn url(self, url: &str) -> Self
设置点击跳转 URL
详细说明请参见 BarkMessageBuilder::url。
Sourcepub fn action(self, action: &str) -> Self
pub fn action(self, action: &str) -> Self
设置动作类型
详细说明请参见 BarkMessageBuilder::action。
Sourcepub fn id(self, id: &str) -> Self
pub fn id(self, id: &str) -> Self
设置消息唯一标识
详细说明请参见 BarkMessageBuilder::id。
Sourcepub fn delete(self, delete: bool) -> Self
pub fn delete(self, delete: bool) -> Self
设置是否删除消息
详细说明请参见 BarkMessageBuilder::delete。
Sourcepub fn send(self) -> Result<BarkResponse>
pub fn send(self) -> Result<BarkResponse>
构建并立即发送消息
这是一个便捷方法,相当于先调用 build() 再调用 SyncBarkClient::send。
§返回值
成功时返回 BarkResponse,失败时返回 BarkError
§错误
可能返回的错误类型与 SyncBarkClient::send 相同。
§示例
use bark_rs::{SyncBarkClient, Level};
let client = SyncBarkClient::with_device_key("https://api.day.app", "key");
let response = client
.message()
.body("测试消息")
.title("测试")
.level(Level::Active)
.send()?;
println!("发送成功: {}", response.message);Examples found in repository?
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4 // 创建同步客户端
5 let client = SyncBarkClient::new("https://api.day.app");
6
7 // 批量推送到多个设备
8 let response = client
9 .message()
10 .device_keys(vec![
11 "QJ48vPutCAsPW2B6pE2A3a".to_string(),
12 "device_key_2".to_string(),
13 "device_key_3".to_string(),
14 ])
15 .title("批量推送通知")
16 .body("这是一个发送给多个设备的批量消息")
17 .level(Level::TimeSensitive)
18 .volume(7)
19 .badge(1)
20 .group("批量通知")
21 .send()?;
22
23 println!(
24 "批量推送成功: code={}, message={}",
25 response.code, response.message
26 );
27
28 Ok(())
29}More examples
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4 // 创建同步客户端(带默认设备密钥)
5 let client = SyncBarkClient::with_device_key("https://api.day.app", "QJ48vPutCAsPW2B6pE2A3a");
6
7 // 方式1: 使用客户端的链式调用
8 let response = client
9 .message()
10 .title("同步推送")
11 .body("这是同步客户端发送的消息")
12 .level(Level::Active)
13 .volume(7)
14 .send()?;
15
16 println!(
17 "同步推送成功: code={}, message={}",
18 response.code, response.message
19 );
20
21 // 方式2: 先构建消息,再发送
22 let message = BarkMessage::builder()
23 .title("独立构建的消息")
24 .body("消息构建与发送分离")
25 .level(Level::Critical)
26 .sound("alarm")
27 .badge(1)
28 .build();
29
30 let response = client.send(&message)?;
31 println!(
32 "独立消息发送成功: code={}, message={}",
33 response.code, response.message
34 );
35
36 Ok(())
37}3fn main() {
4 println!("🚨 演示错误处理");
5
6 // 创建没有默认设备密钥的客户端
7 let client = SyncBarkClient::new("https://api.day.app");
8
9 // 尝试发送没有设备密钥的消息
10 let message_without_key = BarkMessage::builder()
11 .title("错误演示")
12 .body("这个消息没有设备密钥")
13 .build();
14
15 match client.send(&message_without_key) {
16 Ok(_) => println!("❌ 意外成功"),
17 Err(BarkError::MissingDeviceKey) => {
18 println!("✅ 正确捕获到缺少设备密钥错误");
19 }
20 Err(e) => println!("❓ 其他错误: {}", e),
21 }
22
23 // 演示正确的错误处理模式
24 let result = client
25 .message()
26 .device_key("QJ48vPutCAsPW2B6pE2A3a")
27 .title("正确的消息")
28 .body("这个消息有设备密钥")
29 .send();
30
31 match result {
32 Ok(response) => {
33 println!(
34 "✅ 消息发送成功: code={}, message={}",
35 response.code, response.message
36 );
37 }
38 Err(BarkError::RequestError(e)) => {
39 println!("❌ 网络请求错误: {}", e);
40 }
41 Err(BarkError::MissingDeviceKey) => {
42 println!("❌ 缺少设备密钥");
43 }
44 Err(BarkError::SerializationError(e)) => {
45 println!("❌ 序列化错误: {}", e);
46 }
47 Err(BarkError::InvalidUrl) => {
48 println!("❌ 无效URL");
49 }
50 }
51
52 println!("🎉 错误处理演示完成!");
53}Sourcepub fn build(self) -> BarkMessage
pub fn build(self) -> BarkMessage
构建消息对象而不发送
如果您需要先构建消息再由其他客户端发送,或者需要复用消息,可以使用这个方法。
§返回值
返回构建完成的 BarkMessage
§示例
use bark_rs::SyncBarkClient;
let client = SyncBarkClient::new("https://api.day.app");
let message = client
.message()
.body("消息内容")
.title("消息标题")
.build();
// 现在可以用不同的客户端发送这个消息