fcm_v1/
lib.rs

1#![warn(missing_docs)]
2
3//! fcm_v1
4//! ======
5//!
6//! A type-safe way to call the Firebase Cloud Messaging (FCM) HTTP v1 API.
7//!
8//! OAuth 2.0 authentication is performed via the [yup-oauth2](yup_oauth2) crate.
9//! Currently, we request the `"https://www.googleapis.com/auth/firebase.messaging"` scope
10//! in order to send messages.
11
12/// Android-specific component of the message.
13pub mod android;
14/// iOS-specific component of the message.
15pub mod apns;
16/// OAuth2 authentication helpers.
17pub mod auth;
18mod client;
19/// Platform-independent component of the message.
20pub mod message;
21mod result;
22/// Web Push-specific component of the message.
23pub mod webpush;
24
25pub use client::Client;
26pub use result::{Error, Result};
27
28#[cfg(test)]
29mod tests {
30    use std::{env, time::Duration};
31
32    use super::*;
33
34    #[tokio::test]
35    async fn send_simple_noti() {
36        let helloworld = "\u{202e}\u{1d5db}\u{1d5f2}\u{1d5f9}\u{1d5f9}\u{1d5fc}, world!";
37
38        let creds_path = env::var("GOOGLE_APPLICATION_CREDENTIALS").unwrap();
39        let project_id = env::var("GOOGLE_PROJECT_ID").unwrap();
40        let registration_token = env::var("FCM_REGISTRATION_TOKEN").unwrap();
41        let authenticator = auth::Authenticator::service_account_from_file(creds_path)
42            .await
43            .unwrap();
44        let client = Client::new(authenticator, project_id, true, Duration::from_secs(10));
45
46        let mut test_noti = message::Notification::default();
47        test_noti.title = Some("test notification".to_owned());
48        test_noti.body = Some(helloworld.to_owned());
49
50        let mut test_message = message::Message::default();
51        test_message.notification = Some(test_noti);
52        test_message.token = Some(registration_token);
53
54        println!("sent:");
55        println!("{:#?}", test_message);
56        println!("received:");
57        println!("{:#?}", client.send(&test_message).await.unwrap());
58    }
59}