forked from ywegel/oauth_fcm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
axum_example.rs
45 lines (37 loc) · 1.37 KB
/
axum_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
use axum::{extract::Extension, routing::post, Router};
use oauth_fcm::{create_shared_token_manager, send_fcm_message, SharedTokenManager};
use serde::Serialize;
#[derive(Serialize)]
struct MyData {
message: String,
count: i32,
}
async fn send_notification(
Extension(token_manager): Extension<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 Axum!".to_string(),
count: 42,
};
send_fcm_message(device_token, None, Some(data), &token_manager, project_id)
.await
.map_err(|e| e.to_string())?;
Ok("FCM message sent successfully".to_string())
}
#[tokio::main]
async fn main() {
let shared_token_manager = create_shared_token_manager("path/to/google/credentials.json")
.expect("Could not find credentials.json");
let app = Router::new()
.route("/send", post(send_notification))
.layer(Extension(shared_token_manager));
let listener = tokio::net::TcpListener::bind(format!("{}:{}", "127.0.0.1", "8080"))
.await
.expect("Failed to bind to address");
axum::serve(listener, app)
.await
.expect("Failed to start axum server");
}