notifications] adding native notifications
Summary: Adds a notification disapatcher to the redux store which triggers native notifications. Reviewed By: jknoxville Differential Revision: D10301490 fbshipit-source-id: d926d9a5378359ebb98a8b5816100f41db1e13e6
This commit is contained in:
committed by
Facebook Github Bot
parent
78252b2ef2
commit
069f2be335
@@ -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));
|
||||
|
||||
44
src/dispatcher/notifications.js
Normal file
44
src/dispatcher/notifications.js
Normal file
@@ -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<string> = 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);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user