Crates.io | oauth_fcm |
lib.rs | oauth_fcm |
version | 0.2.0 |
source | src |
created_at | 2024-04-29 13:13:04.560898 |
updated_at | 2024-08-07 19:13:47.183128 |
description | A library for sending both data and notification Firebase Cloud Messaging (FCM) messages |
homepage | |
repository | https://github.com/ywegel/oauth_fcm |
max_upload_size | |
id | 1224170 |
size | 109,697 |
This library is designed to simplify the process of sending Firebase Cloud Messaging (FCM) messages. It handles the OAuth token for you, ensuring that tokens are refreshed when expired, and provides a simple interface for sending both FCM data and notification messages.
Add the following to your Cargo.toml
file:
[dependencies]
oauth_fcm = "0.2.0"
Simple example for axum. More detailed examples for other frameworks can be found in the examples' folder.
use std::fs::File;
use oauth_fcm::{create_shared_token_manager, send_fcm_message, FcmNotification, SharedTokenManager};
#[derive(serde::Serialize)]
struct YourDataType {
// Your data here
key: String,
}
async fn send_notification_route(Extension(token_manager): Extension<SharedTokenManager>, ) {
let data = YourDataType {
key: "value".to_string(),
};
let notification = FcmNotification {
title: "Title".to_string(),
body: "Body".to_string(),
};
send_fcm_message("DEVICE_TOKEN", Some(notification), Some(data), &token_manager, "PROJECT_ID").await.unwrap();
}
#[tokio::main]
async fn main() {
let shared_token_manager =
create_shared_token_manager(File::open("path/to/google/credentials.json")).expect("Could not find credentials.json");
let app = Router::new()
.route("/send", post(send_notification_route))
.layer(Extension(shared_token_manager));
let listener = tokio::net::TcpListener::bind(format!("{}:{}", "127.0.0.1", "8080")).await.unwrap();
axum::serve(listener, app).await.unwrap();
}
firebase-adminsdk-xyz@your-project.iam.gserviceaccount.com
let shared_token_manager =
create_shared_token_manager(File::open("location/your-key-name-xyz.json").unwrap())
.expect("Failed to create SharedTokenManager");
your-key-name-*.json
For more detailed examples, please refer to the Examples directory in the repository. There you can find example implementations for either Rocket or Axum. Feel free to open a merge request for any other framework.
Please read CONTRIBUTING.md for details on contributing. By contributing, you agree to license your code under the project's license.
Licensed under MIT license
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, shall be licensed as MIT, without any additional terms or conditions.