forked from panicbit/fcm-rust
-
Notifications
You must be signed in to change notification settings - Fork 10
/
simple_sender.rs
54 lines (46 loc) · 1.41 KB
/
simple_sender.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// cargo run --example simple_sender -- --help
use std::path::PathBuf;
use clap::Parser;
use fcm::{
message::{Message, Notification, Target},
FcmClient,
};
use serde_json::json;
#[derive(Parser, Debug)]
struct CliArgs {
#[arg(short = 't', long)]
device_token: String,
/// Set path to the service account key JSON file. Default is to use
/// path from the `GOOGLE_APPLICATION_CREDENTIALS` environment variable
/// (which can be also located in `.env` file).
#[arg(short = 'k', long, value_name = "FILE")]
service_account_key_path: Option<PathBuf>,
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let args = CliArgs::parse();
let builder = FcmClient::builder();
let builder = if let Some(path) = args.service_account_key_path {
builder.service_account_key_json_path(path)
} else {
builder
};
let client = builder.build().await.unwrap();
let message = Message {
data: Some(json!({
"key": "value",
})),
notification: Some(Notification {
title: Some("Title".to_string()),
..Default::default()
}),
target: Target::Token(args.device_token),
fcm_options: None,
android: None,
apns: None,
webpush: None,
};
let response = client.send(message).await?;
println!("Response: {:#?}", response);
Ok(())
}