forked from ywegel/oauth_fcm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrocket_example.rs
46 lines (39 loc) · 1.16 KB
/
rocket_example.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
use rocket::{post, State};
use serde::Serialize;
use oauth_fcm::{create_shared_token_manager, send_fcm_message, SharedTokenManager};
#[derive(Serialize)]
struct MyData {
message: String,
count: i32,
}
#[post("/send")]
async fn send_notification(token_manager: &State<SharedTokenManager>) -> Result<String, String> {
// It is a good idea to load these from an .env file. Additionally, you can store them in a shared `Config` state.
let device_token = "YOUR_DEVICE_TOKEN";
let project_id = "YOUR_PROJECT_ID";
let data = MyData {
message: "Hello from Rocket!".to_string(),
count: 42,
};
send_fcm_message(
device_token,
None,
Some(data),
token_manager.inner(),
project_id,
)
.await
.map_err(|e| e.to_string())?;
Ok("FCM message sent successfully".to_string())
}
#[rocket::main]
async fn main() {
let shared_token_manager =
create_shared_token_manager("path/to/google/credentials.json").unwrap();
rocket::build()
.manage(shared_token_manager)
.mount("/", rocket::routes![send_notification])
.launch()
.await
.unwrap();
}