diff --git a/src/common/instance.rs b/src/common/instance.rs index 43e8ab7..d72ef26 100644 --- a/src/common/instance.rs +++ b/src/common/instance.rs @@ -280,7 +280,7 @@ impl NeoInstance { .await { Ok(pn) => { - log::debug!("Sending push notification: {:?} about {}", *pn, uid); + log::debug!("Forwarding push notification about {}", uid); let _ = fwatch_tx.send_replace(pn.clone()); } Err(e) => { diff --git a/src/common/pushnoti.rs b/src/common/pushnoti.rs index 0d03a94..c480632 100644 --- a/src/common/pushnoti.rs +++ b/src/common/pushnoti.rs @@ -8,7 +8,7 @@ use fcm_push_listener::*; use std::{fs, sync::Arc}; use tokio::{ sync::{ - mpsc::Receiver as MpscReceiver, + mpsc::{Receiver as MpscReceiver, Sender as MpscSender}, oneshot::Sender as OneshotSender, watch::{channel as watch, Receiver as WatchReceiver, Sender as WatchSender}, }, @@ -21,6 +21,7 @@ use crate::AnyResult; pub(crate) struct PushNotiThread { pn_watcher: Arc>>, registed_cameras: Vec, + received_ids: Vec, } // The push notification @@ -38,6 +39,9 @@ pub(crate) enum PnRequest { instance: NeoInstance, sender: OneshotSender>, }, + AddPushID { + id: String, + }, } impl PushNotiThread { @@ -47,11 +51,13 @@ impl PushNotiThread { Ok(PushNotiThread { pn_watcher: Arc::new(pn_watcher), registed_cameras: vec![], + received_ids: vec![], }) } pub(crate) async fn run( &mut self, + sender: &MpscSender, pn_request_rx: &mut MpscReceiver, ) -> AnyResult<()> { loop { @@ -111,12 +117,16 @@ impl PushNotiThread { let mut listener = FcmPushListener::create( registration, |message: FcmMessage| { + log::debug!("Got FCM Message: {message:?}"); thread_pn_watcher.send_replace(Some(PushNoti { message: message.payload_json, id: message.persistent_id, })); + if let Some(id) = message.persistent_id { + let _ = sender.try_send(PnRequest::AddPushID { id }); // Don't await not worth it + } }, - vec![], + self.received_ids.clone(), ); for instance in self.registed_cameras.iter() { @@ -131,7 +141,7 @@ impl PushNotiThread { Box::pin(async move { let r = camera.send_pushinfo_android(&fcm_token, &uid).await; log::debug!( - "Registered {} for pushnnotifications: {:?}", + "Registered {} for push notifications: {:?}", camera.uid().await?, r ); @@ -166,6 +176,8 @@ impl PushNotiThread { sleep(Duration::from_secs(30)).await; } } + } else { + log::debug!("Push notification listener reported normal shutdown"); } } => v, v = async { @@ -185,7 +197,7 @@ impl PushNotiThread { Box::pin(async move { let r = camera.send_pushinfo_android(&fcm_token, &uid).await; log::debug!( - "Registered {} for pushnnotifications: {:?}", + "Registered {} for push notifications: {:?}", camera.uid().await?, r ); @@ -196,6 +208,9 @@ impl PushNotiThread { let _ = sender.send(r); }); } + PnRequest::AddPushID{id} => { + self.received_ids.push(id); + } } } } => { diff --git a/src/common/reactor.rs b/src/common/reactor.rs index 2565667..ae921d2 100644 --- a/src/common/reactor.rs +++ b/src/common/reactor.rs @@ -116,6 +116,7 @@ impl NeoReactor { // Push notification client let cancel1 = cancel.clone(); + let pn_tx = push_noti.clone(); set.spawn(async move { tokio::select! { _ = cancel1.cancelled() => AnyResult::Ok(()), @@ -123,10 +124,12 @@ impl NeoReactor { let mut pn = PushNotiThread::new().await?; loop { - let r = pn.run(&mut pn_rx).await; + let r = pn.run(&pn_tx, &mut pn_rx).await; if r.is_err() { log::debug!("Issue with push notifier: {r:?}"); sleep(Duration::from_secs(5)).await; + } else { + log::debug!("Push notifier reported normal shutdown. Restarting"); } } } => v,