From 8ae76a8dc3891d6697d84f1bfd0785e756dc0386 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Wed, 27 Sep 2023 11:24:43 +0700 Subject: [PATCH] Retry push notifications on registration failure --- Cargo.lock | 1 - Cargo.toml | 1 - src/common/pushnoti.rs | 47 +++++++++++++++++++++++++++--------------- 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d238b88..62309ef 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1697,7 +1697,6 @@ dependencies = [ "md5", "neolink_core", "regex", - "reqwest", "rumqttc", "serde", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index 86a0bcf..74d7a22 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,7 +32,6 @@ log = { version = "0.4.17", features = [ "release_max_level_debug" ] } md5 = "0.7.0" neolink_core = { path = "crates/core", version = "0.6.2" } regex = "1.7.3" -reqwest = "0.11.20" rumqttc = "0.22.0" serde = { version = "1.0.160", features = ["derive"] } serde_json = "1.0.96" diff --git a/src/common/pushnoti.rs b/src/common/pushnoti.rs index 3e9d9b9..aba90ac 100644 --- a/src/common/pushnoti.rs +++ b/src/common/pushnoti.rs @@ -5,10 +5,13 @@ use fcm_push_listener::*; use std::{fs, sync::Arc}; -use tokio::sync::{ - mpsc::Receiver as MpscReceiver, - oneshot::Sender as OneshotSender, - watch::{channel as watch, Receiver as WatchReceiver, Sender as WatchSender}, +use tokio::{ + sync::{ + mpsc::Receiver as MpscReceiver, + oneshot::Sender as OneshotSender, + watch::{channel as watch, Receiver as WatchReceiver, Sender as WatchSender}, + }, + time::{sleep, Duration}, }; use super::NeoInstance; @@ -47,6 +50,9 @@ impl PushNotiThread { pn_request_rx: &mut MpscReceiver, ) -> AnyResult<()> { loop { + // Short wait on start/retry + sleep(Duration::from_secs(3)).await; + let sender_id = "743639030586"; // andriod // let sender_id = "696841269229"; // ios @@ -64,20 +70,27 @@ impl PushNotiThread { registration } else { log::debug!("Registering new push notification token"); - let registration = fcm_push_listener::register(sender_id).await?; - let new_token = toml::to_string(®istration)?; - if let Some(Err(e)) = token_path - .as_ref() - .map(|token_path| fs::write(token_path, &new_token)) - { - log::warn!( - "Unable to save push notification details ({}) to {:#?} because of the error {:#?}", - new_token, - token_path, - e - ); + match fcm_push_listener::register(sender_id).await { + Ok(registration) => { + let new_token = toml::to_string(®istration)?; + if let Some(Err(e)) = token_path + .as_ref() + .map(|token_path| fs::write(token_path, &new_token)) + { + log::warn!( + "Unable to save push notification details ({}) to {:#?} because of the error {:#?}", + new_token, + token_path, + e + ); + } + registration + } + Err(e) => { + log::warn!("Issue connecting to push notifications server: {:?}", e); + continue; + } } - registration }; // Send registration.fcm_token to the server to allow it to send push messages to you.