Skip to content

rj76/fcm-rust

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fcm-rust

Cargo tests

v1 API

This fork is a rewrite to use Google's HTTP v1 API.

Getting started

Installation

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.

Usage

For a complete usage example, you may check the Examples section.

Import

use fcm;

Create a client instance

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();

Construct a message

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,
};

Send the message

let response = client.send(message).await.unwrap();

Credentials

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.

Examples

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.

About

An API to talk to FCM (Firebase Cloud Messaging) in Rust

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Rust 100.0%