Skip to content

Commit

Permalink
chore(notifications): use fcm client
Browse files Browse the repository at this point in the history
  • Loading branch information
thevaibhav-dixit committed Feb 5, 2024
1 parent f548dc8 commit 3af088f
Show file tree
Hide file tree
Showing 10 changed files with 1,225 additions and 77 deletions.
319 changes: 281 additions & 38 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@ tonic-build = { version = "0.10.2", features = ["prost"] }
tonic-health = "0.10.2"
prost = "0.12"
rust-i18n = "3"
google-fcm1 = "5.0.3"

1 change: 1 addition & 0 deletions core/notifications/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ galoy_rust_bin(
"//third-party/rust:tonic-health",
"//third-party/rust:prost",
"//third-party/rust:rust-i18n",
"//third-party/rust:google-fcm1",
],
extra_tests = [
"//lib/tracing-rs:tracing",
Expand Down
1 change: 1 addition & 0 deletions core/notifications/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ tonic = { workspace = true }
tonic-health = { workspace = true }
prost = { workspace = true }
rust-i18n = { workspace = true }
google-fcm1 = { workspace = true }

[build-dependencies]
tonic-build = { workspace = true }
37 changes: 37 additions & 0 deletions core/notifications/src/fcm/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use google_fcm1::{
hyper::{client::HttpConnector, Client},
hyper_rustls::{HttpsConnector, HttpsConnectorBuilder},
oauth2, FirebaseCloudMessaging,
};
use std::{env, fs::File, io::BufReader};

pub struct FcmExecutor {
pub client: FirebaseCloudMessaging<HttpsConnector<HttpConnector>>,
}

impl FcmExecutor {
pub async fn new() -> Self {
let credentials_path = env::var("GOOGLE_APPLICATION_CREDENTIALS")
.expect("GOOGLE_APPLICATION_CREDENTIALS should be set");
let file = File::open(credentials_path)
.expect("Failed to open the GOOGLE_APPLICATION_CREDENTIALS file");
let reader = BufReader::new(file);

let secret: oauth2::ServiceAccountKey = serde_json::from_reader(reader)
.expect("Failed to parse the GOOGLE_APPLICATION_CREDENTIALS file");
let auth = oauth2::ServiceAccountAuthenticator::builder(secret)
.build()
.await
.expect("Failed to create the authenticator");

let hyper_client = Client::builder().build(
HttpsConnectorBuilder::new()
.with_native_roots()
.https_or_http()
.enable_http1()
.build(),
);
let client = FirebaseCloudMessaging::new(hyper_client, auth);
Self { client }
}
}
1 change: 1 addition & 0 deletions core/notifications/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod messages;

pub mod cli;
pub mod executor;
pub mod fcm;
pub mod graphql;
pub mod grpc;
pub mod notification_event;
Expand Down
30 changes: 30 additions & 0 deletions core/notifications/tests/fcm_client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use google_fcm1::api::{Message, Notification, SendMessageRequest};

use lib_notifications::fcm::*;

#[tokio::test]
#[ignore]
async fn fcm() -> anyhow::Result<()> {
let fcm = FcmExecutor::new().await;

let message = Message {
notification: Some(Notification {
title: Some("title".to_string()),
body: Some("body".to_string()),
..Default::default()
}),
token: Some("some-device-token".to_string()),
..Default::default()
};
let req = SendMessageRequest {
message: Some(message),
..Default::default()
};
let _response = fcm
.client
.projects()
.messages_send(req, "parent") // parent refers to the {projects/project_id} part of the URL
.doit()
.await?;
Ok(())
}
Loading

0 comments on commit 3af088f

Please sign in to comment.