Code Monkey home page Code Monkey logo

fcm-rust's Introduction

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.

fcm-rust's People

Contributors

jutuon avatar panicbit avatar rj76 avatar pimeys avatar adnanjpg avatar yannleretaille avatar fedott avatar jasperav1994 avatar jonas-schievink avatar stringhandler avatar github-actions[bot] avatar fliegendewurst avatar jasperav avatar kate-shine avatar vishy1618 avatar krial057 avatar lucasmerlin avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.