Crate firebae_cm
source ·Expand description
This crate is a Rust wrapper around the Firebase Cloud Messaging V1 protocol. This allows you to create cloud messages in pure Rust code and send them via push messages to other devices.
It is highly recommended to read the Firebase documentation. In order for this to work, you need a Google Cloud Platform account and Firebase to be setup.
Usage
This crate can be found on crates.io,
so you can use it by adding firebae-cm
to your project’s Cargo.toml
.
Example
use firebae_cm::{
Client,
Error,
Message,
MessageBody,
Receiver,
};
#[tokio::main]
async fn main() {
let token = "your_jwt_token";
// Define the receiver mode (Token, Topic or Condition).
let receivers = Receiver::topic("subscribers");
// Create an empty message to your receiver(s).
let body = MessageBody::new(receivers);
// Finalize the message with the correct url and authentication.
let message = Message::new("project_id", token, body);
// Create a client and send the data.
let client = Client::new();
let response: Result<String, Error> = client.send(message).await;
println!("{:?}", response);
}
Features
- oauth - Enables automatic OAuth authentication.
You will still need Firebase to be setup correctly and the path to a
valid
credentials.json
file in theGOOGLE_APPLICATION_CREDENTIALS
environment variable. Then, creating a message can be done usingMessage::with_oauth("project_id", body).await?
.
Structs
- Represents all settings for an Android message. See https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#AndroidConfig for a complete list.
- Additional Firebase options for Android. Contains only the analytics_label. See https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#androidfcmoptions.
- Represents the settings for a notification in Android. All settings are optional, and all settings (setter functions) follow the scheme below. See https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#androidnotification.
- Represents all settings for Apple notifications. See https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#apnsconfig.
- Additional Firebase options for Apple. See https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#apnsfcmoptions.
- A reqwest client wrapper to send Firebase messages.
- Represents a color as described in the documentation.
- A Firebase Cloud Message Error. For example, an invalid JWT token would return the following error:
- Additional platform independent Firebase options. Contains only the analytics_label. See https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#fcmoptions.
- Represents a String: String map that can be used in some Firebase objects for fields such as data, payload and headers. You can use the crate::AsFirebaseMap derive implementation for ease.
- Represents the notification light settings of the notification. See https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#lightsettings. Durations are in seconds.
- A representation of a complete message that can be sent. It requires your project_id and an authentication JWT token (see https://cloud.google.com/docs/authentication/). For automatic handling of the JWT authentication, see the
oauth
feature and theMessage::with_oauth
function. - A message body as described in https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#resource:-message.
- Represents a basic template for notifications, which is equal across all platforms. See https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#notification. Use the
Notification::new
function, or initialize the struct yourself: - Represents all settings for web. See https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#webpushconfig.
- Additional Firebase options for web. See https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#webpushfcmoptions.
Enums
- Represents the Android message priority. See https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#androidmessagepriority.
- All possible errors that can occur in this crate.
- Represents a response by Firebase, whether it is successful or an error.
- Represents the priority of the notification. See https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#notificationpriority.
- Represents a receiver of your message. Can be a Token (for a single device), a Topic (for all devices that have subscribed to that topic), or a Condition (for all devices that meet the condition). See the fields in the documentation.
- Represents the visibility of the notification. See https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#visibility
Traits
- Trait to convert a value into a String.
- Trait to convert a struct into a FirebaseMap.