Skip to content
forked from rj76/fcm-rust

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

License

Notifications You must be signed in to change notification settings

reown-com/fcm-rust

 
 

Repository files navigation

fcm-rust

Cargo tests Coverage Status

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" }

Then, you need to 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::Client::new();

Construct a message

let message = fcm::Message {
    data: None,
    notification: Some(Notification {
        title: Some("I'm high".to_string()),
        body: Some(format!("it's {}", chrono::Utc::now())),
        ..Default::default()
    }),
    target: Target::Token(device_token),
    fcm_options: Some(FcmOptions {
        analytics_label: "analytics_label".to_string(),
    }),
    android: Some(AndroidConfig {
        priority: Some(fcm::AndroidMessagePriority::High),
        notification: Some(AndroidNotification {
            title: Some("I'm Android high".to_string()),
            body: Some(format!("Hi Android, it's {}", chrono::Utc::now())),
            ..Default::default()
        }),
        ..Default::default()
    }),
    apns: Some(ApnsConfig { ..Default::default() }),
    webpush: Some(WebpushConfig { ..Default::default() }),
}

Send the message

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

Credentials

This library expects the Google credentials JSON location to be defined as GOOGLE_APPLICATION_CREDENTIALS in the .env file. Please follow the instructions in the Firebase Documentation to create a service account.

Examples

For a complete usage example, you may check out the simple_sender example.

To run the example, first of all clone the .env.example file to .env and fill in the required values.

You can find info about the required credentials in the Credentials section.

Then run the example with cargo run --example simple_sender -- -t <device_token>

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 97.9%
  • Nix 2.1%