Define react-native-firebase relatives only for mobile platforms
Signed-off-by: Dmitry Novotochinov <dmitry.novot@gmail.com>
This commit is contained in:
parent
e2daa89af1
commit
85aad1d2a2
|
@ -43,7 +43,6 @@
|
||||||
"react-native-image-crop-picker",
|
"react-native-image-crop-picker",
|
||||||
"react-native-securerandom",
|
"react-native-securerandom",
|
||||||
"react-native-webview-bridge",
|
"react-native-webview-bridge",
|
||||||
"react-native-fcm",
|
|
||||||
"homoglyph-finder",
|
"homoglyph-finder",
|
||||||
"web3",
|
"web3",
|
||||||
"chance",
|
"chance",
|
||||||
|
|
|
@ -51,7 +51,6 @@
|
||||||
"react-native-config": "git+https://github.com/status-im/react-native-config.git",
|
"react-native-config": "git+https://github.com/status-im/react-native-config.git",
|
||||||
"react-native-crypto": "2.1.1",
|
"react-native-crypto": "2.1.1",
|
||||||
"react-native-dialogs": "0.0.20",
|
"react-native-dialogs": "0.0.20",
|
||||||
"react-native-fcm": "10.0.3",
|
|
||||||
"react-native-fetch-polyfill": "1.1.2",
|
"react-native-fetch-polyfill": "1.1.2",
|
||||||
"react-native-fs": "git+https://github.com/status-im/react-native-fs.git",
|
"react-native-fs": "git+https://github.com/status-im/react-native-fs.git",
|
||||||
"react-native-http": "github:tradle/react-native-http#834492d",
|
"react-native-http": "github:tradle/react-native-http#834492d",
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
(def EventEmmiter (js/require "react-native/Libraries/vendor/emitter/EventEmitter"))
|
(def EventEmmiter (js/require "react-native/Libraries/vendor/emitter/EventEmitter"))
|
||||||
(def fetch (.-default (js/require "react-native-fetch-polyfill")))
|
(def fetch (.-default (js/require "react-native-fetch-polyfill")))
|
||||||
(def i18n (js/require "react-native-i18n"))
|
(def i18n (js/require "react-native-i18n"))
|
||||||
(def react-native-firebase (js/require "react-native-firebase"))
|
(def react-native-firebase #js {})
|
||||||
(def camera #js {:constants {:Aspect "Portrait"}})
|
(def camera #js {:constants {:Aspect "Portrait"}})
|
||||||
(def dialogs #js {})
|
(def dialogs #js {})
|
||||||
(def dismiss-keyboard #js {})
|
(def dismiss-keyboard #js {})
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
[status-im.utils.config :as config]
|
[status-im.utils.config :as config]
|
||||||
[status-im.utils.keychain.core :as keychain]
|
[status-im.utils.keychain.core :as keychain]
|
||||||
[status-im.utils.notifications :as notifications]
|
[status-im.utils.notifications :as notifications]
|
||||||
|
[status-im.utils.platform :as platform]
|
||||||
[status-im.utils.universal-links.core :as universal-links]))
|
[status-im.utils.universal-links.core :as universal-links]))
|
||||||
|
|
||||||
;;;; FX
|
;;;; FX
|
||||||
|
@ -133,4 +134,4 @@
|
||||||
(when (not= view-id :create-account)
|
(when (not= view-id :create-account)
|
||||||
[[:navigate-to-clean :home]
|
[[:navigate-to-clean :home]
|
||||||
(universal-links/stored-url-event cofx)
|
(universal-links/stored-url-event cofx)
|
||||||
(notifications/stored-event address cofx)])]})
|
(when-not platform/desktop? (notifications/stored-event address cofx))])]})
|
||||||
|
|
|
@ -4,7 +4,6 @@
|
||||||
[status-im.utils.handlers-macro :as handlers-macro]
|
[status-im.utils.handlers-macro :as handlers-macro]
|
||||||
[status-im.utils.handlers :as handlers]
|
[status-im.utils.handlers :as handlers]
|
||||||
[status-im.react-native.js-dependencies :as rn]
|
[status-im.react-native.js-dependencies :as rn]
|
||||||
[status-im.utils.platform :as platform]
|
|
||||||
[status-im.ui.components.react :refer [copy-to-clipboard]]
|
[status-im.ui.components.react :refer [copy-to-clipboard]]
|
||||||
[taoensso.timbre :as log]
|
[taoensso.timbre :as log]
|
||||||
[status-im.utils.platform :as platform]))
|
[status-im.utils.platform :as platform]))
|
||||||
|
@ -12,175 +11,177 @@
|
||||||
;; Work in progress namespace responsible for push notifications and interacting
|
;; Work in progress namespace responsible for push notifications and interacting
|
||||||
;; with Firebase Cloud Messaging.
|
;; with Firebase Cloud Messaging.
|
||||||
|
|
||||||
(handlers/register-handler-db
|
(when-not platform/desktop?
|
||||||
:update-fcm-token
|
|
||||||
(fn [db [_ fcm-token]]
|
|
||||||
(assoc-in db [:notifications :fcm-token] fcm-token)))
|
|
||||||
|
|
||||||
(handlers/register-handler-fx
|
(handlers/register-handler-db
|
||||||
:request-notifications-granted
|
:update-fcm-token
|
||||||
(fn [_ _]
|
(fn [db [_ fcm-token]]
|
||||||
(re-frame/dispatch [:show-mainnet-is-default-alert])))
|
(assoc-in db [:notifications :fcm-token] fcm-token)))
|
||||||
|
|
||||||
(handlers/register-handler-fx
|
(handlers/register-handler-fx
|
||||||
:request-notifications-denied
|
:request-notifications-granted
|
||||||
(fn [_ _]
|
(fn [_ _]
|
||||||
(re-frame/dispatch [:show-mainnet-is-default-alert])))
|
(re-frame/dispatch [:show-mainnet-is-default-alert])))
|
||||||
|
|
||||||
(def firebase (object/get rn/react-native-firebase "default"))
|
(handlers/register-handler-fx
|
||||||
|
:request-notifications-denied
|
||||||
|
(fn [_ _]
|
||||||
|
(re-frame/dispatch [:show-mainnet-is-default-alert])))
|
||||||
|
|
||||||
;; NOTE: Only need to explicitly request permissions on iOS.
|
(def firebase (object/get rn/react-native-firebase "default"))
|
||||||
(defn request-permissions []
|
|
||||||
(if platform/desktop?
|
|
||||||
(re-frame/dispatch [:request-notifications-granted {}])
|
|
||||||
(-> (.requestPermission (.messaging firebase))
|
|
||||||
(.then
|
|
||||||
(fn [_]
|
|
||||||
(log/debug "notifications-granted")
|
|
||||||
(re-frame/dispatch [:request-notifications-granted {}]))
|
|
||||||
(fn [_]
|
|
||||||
(log/debug "notifications-denied")
|
|
||||||
(re-frame/dispatch [:request-notifications-denied {}]))))))
|
|
||||||
|
|
||||||
(defn get-fcm-token []
|
;; NOTE: Only need to explicitly request permissions on iOS.
|
||||||
(-> (.getToken (.messaging firebase))
|
(defn request-permissions []
|
||||||
(.then (fn [x]
|
(if platform/desktop?
|
||||||
(log/debug "get-fcm-token: " x)
|
(re-frame/dispatch [:request-notifications-granted {}])
|
||||||
(re-frame/dispatch [:update-fcm-token x])))))
|
(-> (.requestPermission (.messaging firebase))
|
||||||
|
(.then
|
||||||
|
(fn [_]
|
||||||
|
(log/debug "notifications-granted")
|
||||||
|
(re-frame/dispatch [:request-notifications-granted {}]))
|
||||||
|
(fn [_]
|
||||||
|
(log/debug "notifications-denied")
|
||||||
|
(re-frame/dispatch [:request-notifications-denied {}]))))))
|
||||||
|
|
||||||
(defn on-refresh-fcm-token []
|
(defn get-fcm-token []
|
||||||
(.onTokenRefresh (.messaging firebase)
|
(-> (.getToken (.messaging firebase))
|
||||||
(fn [x]
|
(.then (fn [x]
|
||||||
(log/debug "on-refresh-fcm-token: " x)
|
(log/debug "get-fcm-token: " x)
|
||||||
(re-frame/dispatch [:update-fcm-token x]))))
|
(re-frame/dispatch [:update-fcm-token x])))))
|
||||||
|
|
||||||
;; TODO(oskarth): Only called in background on iOS right now.
|
(defn on-refresh-fcm-token []
|
||||||
;; NOTE(oskarth): Hardcoded data keys :sum and :msg in status-go right now.
|
(.onTokenRefresh (.messaging firebase)
|
||||||
(defn on-notification []
|
(fn [x]
|
||||||
(.onNotification (.notifications firebase)
|
(log/debug "on-refresh-fcm-token: " x)
|
||||||
(fn [event-js]
|
(re-frame/dispatch [:update-fcm-token x]))))
|
||||||
(let [event (js->clj event-js :keywordize-keys true)
|
|
||||||
data (select-keys event [:sum :msg])
|
|
||||||
aps (:aps event)]
|
|
||||||
(log/debug "on-notification event: " (pr-str event))
|
|
||||||
(log/debug "on-notification aps: " (pr-str aps))
|
|
||||||
(log/debug "on-notification data: " (pr-str data))))))
|
|
||||||
|
|
||||||
(def channel-id "status-im")
|
;; TODO(oskarth): Only called in background on iOS right now.
|
||||||
(def channel-name "Status")
|
;; NOTE(oskarth): Hardcoded data keys :sum and :msg in status-go right now.
|
||||||
(def sound-name "message.wav")
|
(defn on-notification []
|
||||||
(def group-id "im.status.ethereum.MESSAGE")
|
(.onNotification (.notifications firebase)
|
||||||
(def icon "ic_stat_status_notification")
|
(fn [event-js]
|
||||||
|
(let [event (js->clj event-js :keywordize-keys true)
|
||||||
|
data (select-keys event [:sum :msg])
|
||||||
|
aps (:aps event)]
|
||||||
|
(log/debug "on-notification event: " (pr-str event))
|
||||||
|
(log/debug "on-notification aps: " (pr-str aps))
|
||||||
|
(log/debug "on-notification data: " (pr-str data))))))
|
||||||
|
|
||||||
(defn create-notification-channel []
|
(def channel-id "status-im")
|
||||||
(let [channel (firebase.notifications.Android.Channel. channel-id
|
(def channel-name "Status")
|
||||||
channel-name
|
(def sound-name "message.wav")
|
||||||
firebase.notifications.Android.Importance.Max)]
|
(def group-id "im.status.ethereum.MESSAGE")
|
||||||
(.setSound channel sound-name)
|
(def icon "ic_stat_status_notification")
|
||||||
(.setShowBadge channel true)
|
|
||||||
(.enableVibration channel true)
|
(defn create-notification-channel []
|
||||||
|
(let [channel (firebase.notifications.Android.Channel. channel-id
|
||||||
|
channel-name
|
||||||
|
firebase.notifications.Android.Importance.Max)]
|
||||||
|
(.setSound channel sound-name)
|
||||||
|
(.setShowBadge channel true)
|
||||||
|
(.enableVibration channel true)
|
||||||
|
(.. firebase
|
||||||
|
notifications
|
||||||
|
-android
|
||||||
|
(createChannel channel)
|
||||||
|
(then #(log/debug "Notification channel created:" channel-id)
|
||||||
|
#(log/error "Notification channel creation error:" channel-id %)))))
|
||||||
|
|
||||||
|
(defn store-event [{:keys [from to]} {:keys [db] :as cofx}]
|
||||||
|
(let [{:keys [address photo-path name]} (->> (get-in cofx [:db :accounts/accounts])
|
||||||
|
vals
|
||||||
|
(filter #(= (:public-key %) to))
|
||||||
|
first)]
|
||||||
|
(when address
|
||||||
|
{:db (assoc-in db [:push-notifications/stored to] from)
|
||||||
|
:dispatch [:open-login address photo-path name]})))
|
||||||
|
|
||||||
|
(defn process-initial-push-notification [{:keys [initial?]} {:keys [db]}]
|
||||||
|
(when initial?
|
||||||
|
{:db (assoc db :push-notifications/initial? true)}))
|
||||||
|
|
||||||
|
(defn process-push-notification [{:keys [from to] :as event} {:keys [db] :as cofx}]
|
||||||
|
(let [current-public-key (get-in cofx [:db :current-public-key])]
|
||||||
|
(if current-public-key
|
||||||
|
(when (= to current-public-key)
|
||||||
|
{:db (update db :push-notifications/stored dissoc to)
|
||||||
|
:dispatch [:navigate-to-chat from]})
|
||||||
|
(store-event event cofx))))
|
||||||
|
|
||||||
|
(defn handle-push-notification [cofx [_ event]]
|
||||||
|
(handlers-macro/merge-fx cofx
|
||||||
|
(process-initial-push-notification event)
|
||||||
|
(process-push-notification event)))
|
||||||
|
|
||||||
|
(defn stored-event [address cofx]
|
||||||
|
(let [to (get-in cofx [:db :accounts/accounts address :public-key])
|
||||||
|
from (get-in cofx [:db :push-notifications/stored to])]
|
||||||
|
(when from
|
||||||
|
[:handle-push-notification {:from from
|
||||||
|
:to to}])))
|
||||||
|
|
||||||
|
(defn parse-notification-payload [s]
|
||||||
|
(try
|
||||||
|
(js/JSON.parse s)
|
||||||
|
(catch :default _
|
||||||
|
#js {})))
|
||||||
|
|
||||||
|
(defn handle-notification-event [event {:keys [initial?]}]
|
||||||
|
(let [msg (object/get (.. event -notification -data) "msg")
|
||||||
|
data (parse-notification-payload msg)
|
||||||
|
from (object/get data "from")
|
||||||
|
to (object/get data "to")]
|
||||||
|
(log/debug "on notification" (pr-str msg))
|
||||||
|
(when (and from to)
|
||||||
|
(re-frame/dispatch [:handle-push-notification {:from from
|
||||||
|
:to to
|
||||||
|
:initial? initial?}]))))
|
||||||
|
|
||||||
|
(defn on-notification-opened []
|
||||||
(.. firebase
|
(.. firebase
|
||||||
notifications
|
notifications
|
||||||
-android
|
(onNotificationOpened handle-notification-event)))
|
||||||
(createChannel channel)
|
|
||||||
(then #(log/debug "Notification channel created:" channel-id)
|
|
||||||
#(log/error "Notification channel creation error:" channel-id %)))))
|
|
||||||
|
|
||||||
(defn store-event [{:keys [from to]} {:keys [db] :as cofx}]
|
(def notification (firebase.notifications.Notification.))
|
||||||
(let [{:keys [address photo-path name]} (->> (get-in cofx [:db :accounts/accounts])
|
|
||||||
vals
|
|
||||||
(filter #(= (:public-key %) to))
|
|
||||||
first)]
|
|
||||||
(when address
|
|
||||||
{:db (assoc-in db [:push-notifications/stored to] from)
|
|
||||||
:dispatch [:open-login address photo-path name]})))
|
|
||||||
|
|
||||||
(defn process-initial-push-notification [{:keys [initial?]} {:keys [db]}]
|
;; API reference https://rnfirebase.io/docs/v4.2.x/notifications/reference/AndroidNotification
|
||||||
(when initial?
|
(defn display-notification [{:keys [title body from to]}]
|
||||||
{:db (assoc db :push-notifications/initial? true)}))
|
(.. notification
|
||||||
|
(setTitle title)
|
||||||
|
(setBody body)
|
||||||
|
(setData (js/JSON.stringify #js {:from from
|
||||||
|
:to to}))
|
||||||
|
(setSound sound-name)
|
||||||
|
(-android.setChannelId channel-id)
|
||||||
|
(-android.setAutoCancel true)
|
||||||
|
(-android.setPriority firebase.notifications.Android.Priority.Max)
|
||||||
|
(-android.setGroup group-id)
|
||||||
|
(-android.setGroupSummary true)
|
||||||
|
(-android.setSmallIcon icon))
|
||||||
|
(.. firebase
|
||||||
|
notifications
|
||||||
|
(displayNotification notification)
|
||||||
|
(then #(log/debug "Display Notification" title body))
|
||||||
|
(then #(log/debug "Display Notification error" title body))))
|
||||||
|
|
||||||
(defn process-push-notification [{:keys [from to] :as event} {:keys [db] :as cofx}]
|
(re-frame/reg-fx :display-notification-fx display-notification)
|
||||||
(let [current-public-key (get-in cofx [:db :current-public-key])]
|
|
||||||
(if current-public-key
|
|
||||||
(when (= to current-public-key)
|
|
||||||
{:db (update db :push-notifications/stored dissoc to)
|
|
||||||
:dispatch [:navigate-to-chat from]})
|
|
||||||
(store-event event cofx))))
|
|
||||||
|
|
||||||
(defn handle-push-notification [cofx [_ event]]
|
(handlers/register-handler-fx :handle-push-notification handle-push-notification)
|
||||||
(handlers-macro/merge-fx cofx
|
|
||||||
(process-initial-push-notification event)
|
|
||||||
(process-push-notification event)))
|
|
||||||
|
|
||||||
(defn stored-event [address cofx]
|
(re-frame/reg-fx
|
||||||
(let [to (get-in cofx [:db :accounts/accounts address :public-key])
|
:handle-initial-push-notification-fx
|
||||||
from (get-in cofx [:db :push-notifications/stored to])]
|
(fn [{:keys [push-notifications/initial?]}]
|
||||||
(when from
|
(when-not initial?
|
||||||
[:handle-push-notification {:from from
|
(.. firebase
|
||||||
:to to}])))
|
notifications
|
||||||
|
getInitialNotification
|
||||||
|
(then (fn [event]
|
||||||
|
(when event
|
||||||
|
(handle-notification-event event {:initial? true}))))))))
|
||||||
|
|
||||||
(defn parse-notification-payload [s]
|
(defn init []
|
||||||
(try
|
(on-refresh-fcm-token)
|
||||||
(js/JSON.parse s)
|
(on-notification)
|
||||||
(catch :default _
|
(on-notification-opened)
|
||||||
#js {})))
|
(when platform/android?
|
||||||
|
(create-notification-channel))))
|
||||||
(defn handle-notification-event [event {:keys [initial?]}]
|
|
||||||
(let [msg (object/get (.. event -notification -data) "msg")
|
|
||||||
data (parse-notification-payload msg)
|
|
||||||
from (object/get data "from")
|
|
||||||
to (object/get data "to")]
|
|
||||||
(log/debug "on notification" (pr-str msg))
|
|
||||||
(when (and from to)
|
|
||||||
(re-frame/dispatch [:handle-push-notification {:from from
|
|
||||||
:to to
|
|
||||||
:initial? initial?}]))))
|
|
||||||
|
|
||||||
(defn on-notification-opened []
|
|
||||||
(.. firebase
|
|
||||||
notifications
|
|
||||||
(onNotificationOpened handle-notification-event)))
|
|
||||||
|
|
||||||
(def notification (firebase.notifications.Notification.))
|
|
||||||
|
|
||||||
;; API reference https://rnfirebase.io/docs/v4.2.x/notifications/reference/AndroidNotification
|
|
||||||
(defn display-notification [{:keys [title body from to]}]
|
|
||||||
(.. notification
|
|
||||||
(setTitle title)
|
|
||||||
(setBody body)
|
|
||||||
(setData (js/JSON.stringify #js {:from from
|
|
||||||
:to to}))
|
|
||||||
(setSound sound-name)
|
|
||||||
(-android.setChannelId channel-id)
|
|
||||||
(-android.setAutoCancel true)
|
|
||||||
(-android.setPriority firebase.notifications.Android.Priority.Max)
|
|
||||||
(-android.setGroup group-id)
|
|
||||||
(-android.setGroupSummary true)
|
|
||||||
(-android.setSmallIcon icon))
|
|
||||||
(.. firebase
|
|
||||||
notifications
|
|
||||||
(displayNotification notification)
|
|
||||||
(then #(log/debug "Display Notification" title body))
|
|
||||||
(then #(log/debug "Display Notification error" title body))))
|
|
||||||
|
|
||||||
(re-frame/reg-fx :display-notification-fx display-notification)
|
|
||||||
|
|
||||||
(handlers/register-handler-fx :handle-push-notification handle-push-notification)
|
|
||||||
|
|
||||||
(re-frame/reg-fx
|
|
||||||
:handle-initial-push-notification-fx
|
|
||||||
(fn [{:keys [push-notifications/initial?]}]
|
|
||||||
(when-not initial?
|
|
||||||
(.. firebase
|
|
||||||
notifications
|
|
||||||
getInitialNotification
|
|
||||||
(then (fn [event]
|
|
||||||
(when event
|
|
||||||
(handle-notification-event event {:initial? true}))))))))
|
|
||||||
|
|
||||||
(defn init []
|
|
||||||
(on-refresh-fcm-token)
|
|
||||||
(on-notification)
|
|
||||||
(on-notification-opened)
|
|
||||||
(when platform/android?
|
|
||||||
(create-notification-channel)))
|
|
||||||
|
|
Loading…
Reference in New Issue