This fork is a rewrite to use Google's HTTP v1 API.
Add the following to your Cargo.toml
file:
[dependencies]
fcm = { git = "https://github.com/rj76/fcm-rust.git" }
Optionally, add the credentials described in the Credentials
to a .env
file at the root of your project.
For a complete usage example, you may check the Examples section.
use fcm;
let client = fcm::FcmClient::builder()
// Comment to use GOOGLE_APPLICATION_CREDENTIALS environment
// variable. The variable can also be defined in .env file.
.service_account_key_json_path("service_account_key.json")
.build()
.await
.unwrap();
use fcm::message::{Message, Notification, Target};
// Replace "device_token" with the actual device token
let device_token = "device_token".to_string();
let message = Message {
data: Some(json!({
"message": "Howdy!",
})),
notification: Some(Notification {
title: Some("Hello".to_string()),
body: Some(format!("it's {}", chrono::Utc::now())),
image: None,
}),
target: Target::Token(device_token),
android: None,
webpush: None,
apns: None,
fcm_options: None,
};
let response = client.send(message).await.unwrap();
If client is not configured with service account key JSON file path
then this library expects the Google credentials JSON location to be
defined in GOOGLE_APPLICATION_CREDENTIALS
environment variable.
The variable definition can also be located in the .env
file.
Please follow the instructions in the Firebase Documentation to create a service account key JSON file.
For a complete usage example, you may check out the
simple_sender
example.
The example can be run with
cargo run --example simple_sender -- -t <device_token> -k <service_account_key_path>
If GOOGLE_APPLICATION_CREDENTIALS
environment variable is defined in current
environment or in .env
file, then the example can be run with
cargo run --example simple_sender -- -t <device_token>
To define the environment variable using .env
file copy the .env.example
file to .env
and fill in the required values.