-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(notifications): use fcm client
- Loading branch information
1 parent
f548dc8
commit 3af088f
Showing
10 changed files
with
1,225 additions
and
77 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |
Oops, something went wrong.