diff --git a/src/status_im/chat/events.cljs b/src/status_im/chat/events.cljs index 0fd767224d..6a0a342905 100644 --- a/src/status_im/chat/events.cljs +++ b/src/status_im/chat/events.cljs @@ -105,8 +105,11 @@ [re-frame/trim-v] (fn [{:keys [db] :as cofx} [envelope-hash status]] (let [{:keys [chat-id message-id]} (get-in db [:transport/message-envelopes envelope-hash]) - message (get-in db [:chats chat-id :messages message-id])] - (models.message/update-message-status message status cofx)))) + message (get-in db [:chats chat-id :messages message-id]) + {:keys [fcm-token]} (get-in db [:contacts/contacts chat-id])] + (handlers-macro/merge-fx cofx + (models.message/update-message-status message status) + (models.message/send-push-notification fcm-token status))))) ;; Change status of messages which are still in "sending" status to "not-sent" ;; (If signal from status-go has not been received) diff --git a/src/status_im/chat/models/message.cljs b/src/status_im/chat/models/message.cljs index 7ad95b6e7d..6ad26aba50 100644 --- a/src/status_im/chat/models/message.cljs +++ b/src/status_im/chat/models/message.cljs @@ -169,19 +169,13 @@ (defn- send [chat-id message-id send-record {{:contacts/keys [contacts] :keys [network-status current-public-key]} :db :as cofx}] - (let [{:keys [dapp? fcm-token]} (get contacts chat-id)] + (let [{:keys [dapp?]} (get contacts chat-id)] (if dapp? (send-dapp-message! cofx chat-id send-record) (if (= network-status :offline) {:dispatch-later [{:ms 10000 :dispatch [:update-message-status chat-id message-id current-public-key :not-sent]}]} - (if fcm-token - (handlers-macro/merge-fx cofx - {:send-notification {:message "message" - :payload {:title "Status" :body "You have a new message"} - :tokens [fcm-token]}} - (transport/send send-record chat-id)) - (transport/send send-record chat-id cofx)))))) + (transport/send send-record chat-id cofx))))) (defn add-message-type [message {:keys [chat-id group-chat public?]}] (cond-> message @@ -217,6 +211,12 @@ (add-message chat-id message-with-id true) (send chat-id message-id send-record)))) +(defn send-push-notification [fcm-token status cofx] + (when (and fcm-token (= status :sent)) + {:send-notification {:message "message" + :payload {:title "Status" :body "You have a new message"} + :tokens [fcm-token]}})) + (defn update-message-status [{:keys [chat-id message-id from] :as message} status {:keys [db]}] (let [updated-message (assoc-in message [:user-statuses from] status)] {:db (assoc-in db [:chats chat-id :messages message-id] updated-message) diff --git a/src/status_im/ios/core.cljs b/src/status_im/ios/core.cljs index f7724a14fb..6a96ba0862 100644 --- a/src/status_im/ios/core.cljs +++ b/src/status_im/ios/core.cljs @@ -13,6 +13,9 @@ [status-im.utils.instabug :as instabug] [status-im.utils.snoopy :as snoopy])) +(defn app-state-change-handler [state] + (dispatch [:app-state-change state])) + (defn app-root [] (let [keyboard-height (subscribe [:get :keyboard-height])] (reagent/create-class @@ -29,14 +32,16 @@ "keyboardWillHide" #(when-not (= 0 @keyboard-height) (dispatch [:set :keyboard-height 0]))) - (.hide react/splash-screen)) + (.hide react/splash-screen) + (.addEventListener react/app-state "change" app-state-change-handler)) :component-did-mount (fn [] (notifications/on-refresh-fcm-token) (notifications/on-notification)) :component-will-unmount (fn [] - (.stop react/http-bridge)) + (.stop react/http-bridge) + (.removeEventListener react/app-state "change" app-state-change-handler)) :display-name "root" :reagent-render views/main}))) diff --git a/src/status_im/network/events.cljs b/src/status_im/network/events.cljs index b60163e00f..511fd41a8d 100644 --- a/src/status_im/network/events.cljs +++ b/src/status_im/network/events.cljs @@ -28,13 +28,13 @@ (handlers/register-handler-fx ::update-connection-status [re-frame/trim-v] - (fn [{:keys [db] :as cofx} [is-connected?]] - (let [previous-status (:network-status db) - back-online? (and (= previous-status :offline) - is-connected?)] + (fn [{{:keys [network-status mailserver-status] :as db} :db :as cofx} [is-connected?]] + (let [should-recover? (and (= network-status :offline) + is-connected? + (not= mailserver-status :connecting))] (cond-> (handlers-macro/merge-fx cofx {:db (assoc db :network-status (if is-connected? :online :offline))} - (inbox/recover-offline-inbox back-online?)) + (inbox/recover-offline-inbox should-recover?)) is-connected? (assoc :drain-mixpanel-events nil))))) diff --git a/src/status_im/transport/inbox.cljs b/src/status_im/transport/inbox.cljs index e8ffe7983c..6fea0d7013 100644 --- a/src/status_im/transport/inbox.cljs +++ b/src/status_im/transport/inbox.cljs @@ -64,18 +64,20 @@ (log/info "offline inbox: initialize " wnode) (when wnode {:async-flow (initialize-offline-inbox-flow) + :db (assoc db :mailserver-status :connecting) ::add-peer {:wnode wnode}})))) (defn recover-offline-inbox "Recover offline inbox connection after being offline because of connectivity loss" - [back-online? {:keys [db]}] - (when config/offline-inbox-enabled? + [should-recover? {:keys [db]}] + (when (and config/offline-inbox-enabled? + should-recover?) (let [wnode (get-current-wnode-address db)] - (when (and back-online? - wnode + (when (and wnode (:account/account db)) (log/info "offline inbox: recover" wnode) - {:async-flow (recover-offline-inbox-flow)})))) + {:db (assoc db :mailserver-status :connecting) + :async-flow (recover-offline-inbox-flow)})))) (defn add-peer [enode success-fn error-fn] (status/add-peer enode (response-handler error-fn success-fn))) diff --git a/src/status_im/ui/screens/events.cljs b/src/status_im/ui/screens/events.cljs index aa394a86e4..25db87656d 100644 --- a/src/status_im/ui/screens/events.cljs +++ b/src/status_im/ui/screens/events.cljs @@ -34,6 +34,7 @@ [status-im.i18n :as i18n] [status-im.js-dependencies :as dependencies] [status-im.transport.core :as transport] + [status-im.transport.inbox :as inbox] [status-im.ui.screens.db :refer [app-db]] [status-im.utils.datetime :as time] [status-im.utils.ethereum.core :as ethereum] @@ -206,6 +207,11 @@ (fn [_] (status/close-application))) +(re-frame/reg-fx + ::app-state-change-fx + (fn [state] + (status/app-state-change state))) + ;;;; Handlers (handlers/register-handler-db @@ -407,8 +413,14 @@ (handlers/register-handler-fx :app-state-change - (fn [_ [_ state]] - (status/app-state-change state))) + (fn [{{:keys [network-status mailserver-status]} :db :as cofx} [_ state]] + (let [app-coming-from-background? (= state "active") + should-recover? (and app-coming-from-background? + (= network-status :online) + (not= mailserver-status :connecting))] + (handlers-macro/merge-fx cofx + {::app-state-change-fx state} + (inbox/recover-offline-inbox should-recover?))))) (handlers/register-handler-fx :request-permissions