diff --git a/services/tunnelbroker/src/notifs/fcm/firebase_message.rs b/services/tunnelbroker/src/notifs/fcm/firebase_message.rs new file mode 100644 index 000000000..f69fccb6b --- /dev/null +++ b/services/tunnelbroker/src/notifs/fcm/firebase_message.rs @@ -0,0 +1,48 @@ +use serde::{Deserialize, Serialize}; +use serde_json::Value; + +#[derive(Debug, Serialize)] +pub struct FCMMessageWrapper { + pub message: FCMMessage, +} + +/// Message to send by Firebase Cloud Messaging Service. +#[derive(Debug, Serialize)] +pub struct FCMMessage { + /// Arbitrary key/value payload, which must be UTF-8 encoded. + pub data: Value, + /// Target to send a notification/message to. + pub token: String, + /// Android specific options for messages sent through FCM connection server. + pub android: AndroidConfig, +} + +#[derive(Debug, Serialize)] +pub struct AndroidConfig { + /// Message priority. Can take "normal" and "high" values. + pub priority: AndroidMessagePriority, +} + +#[derive(Serialize, Deserialize, Debug)] +#[serde(rename_all = "UPPERCASE")] +pub enum AndroidMessagePriority { + /// Default priority for data messages. Normal priority messages won't + /// open network connections on a sleeping device, and their delivery + /// may be delayed to conserve the battery. + Normal, + /// Default priority for notification messages. FCM attempts to deliver + /// high priority messages immediately, allowing the FCM service to wake + /// a sleeping device when possible and open a network connection to + /// your app server. + High, +} + +impl AndroidMessagePriority { + pub fn from_str(value: &str) -> Option { + match value { + "NORMAL" => Some(AndroidMessagePriority::Normal), + "HIGH" => Some(AndroidMessagePriority::High), + _ => None, + } + } +} diff --git a/services/tunnelbroker/src/notifs/fcm/mod.rs b/services/tunnelbroker/src/notifs/fcm/mod.rs index 9f743e7f1..6e9def31c 100644 --- a/services/tunnelbroker/src/notifs/fcm/mod.rs +++ b/services/tunnelbroker/src/notifs/fcm/mod.rs @@ -1,31 +1,32 @@ use crate::notifs::fcm::config::FCMConfig; use crate::notifs::fcm::token::FCMToken; use std::time::Duration; pub mod config; mod error; +mod firebase_message; mod token; #[derive(Clone)] pub struct FCMClient { http_client: reqwest::Client, config: FCMConfig, token: FCMToken, } impl FCMClient { pub fn new(config: &FCMConfig) -> Result { let http_client = reqwest::Client::builder().build()?; // Token must be a short-lived token (60 minutes) and in a reasonable // timeframe. let token_ttl = Duration::from_secs(60 * 55); let token = FCMToken::new(&config.clone(), token_ttl)?; Ok(FCMClient { http_client, config: config.clone(), token, }) } }