diff --git a/src/dispatcher/index.js b/src/dispatcher/index.js index dc00ff00b..a02f060d4 100644 --- a/src/dispatcher/index.js +++ b/src/dispatcher/index.js @@ -11,6 +11,7 @@ import windowsDevice from './windowsDevice'; import application from './application'; import tracking from './tracking'; import server from './server'; +import notifications from './notifications'; import type Logger from '../fb-stubs/Logger.js'; import type {Store} from '../reducers/index.js'; @@ -23,4 +24,5 @@ export default (store: Store, logger: Logger) => windowsDevice, tracking, server, + notifications, ].forEach(fn => fn(store, logger)); diff --git a/src/dispatcher/notifications.js b/src/dispatcher/notifications.js new file mode 100644 index 000000000..f953d76c8 --- /dev/null +++ b/src/dispatcher/notifications.js @@ -0,0 +1,44 @@ +/** + * Copyright 2018-present Facebook. + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * @format + */ + +import type {Store} from '../reducers/index.js'; +import type Logger from '../fb-stubs/Logger.js'; +import type {PluginNotification} from '../reducers/notifications'; + +import {selectPlugin} from '../reducers/connections'; +import {textContent} from '../utils/index'; + +export default (store: Store, logger: Logger) => { + const knownNotifications: Set = new Set(); + store.subscribe(() => { + const { + activeNotifications, + blacklistedPlugins, + } = store.getState().notifications; + + activeNotifications.forEach((n: PluginNotification) => { + if ( + !knownNotifications.has(n.notification.id) && + blacklistedPlugins.indexOf(n.pluginId) === -1 + ) { + const notification = new window.Notification(n.notification.title, { + body: textContent(n.notification.message), + }); + logger.track('usage', 'native-notification', n.notification); + notification.onclick = () => + store.dispatch( + selectPlugin({ + selectedPlugin: n.pluginId, + selectedApp: n.client, + deepLinkPayload: n.notification.action, + }), + ); + knownNotifications.add(n.notification.id); + } + }); + }); +};