-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Add Browser Notifications for Web / Desktop #289
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7fe0bcc
Add Browser Notifications
marcaaron 335c86e
fix eslint thing
marcaaron 9e03313
Merge remote-tracking branch 'origin' into marcaaron-webNotifications
marcaaron e1e7a70
remove async await
marcaaron ac1806d
fix conflict;
marcaaron db53a89
requested changes
marcaaron File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,8 @@ | ||
| const CLOUDFRONT_URL = 'https://d2k5nsl2zxldvw.cloudfront.net'; | ||
|
|
||
| const CONST = { | ||
| CLOUDFRONT_URL: 'https://d2k5nsl2zxldvw.cloudfront.net', | ||
| CLOUDFRONT_URL, | ||
| EXPENSIFY_ICON_URL: `${CLOUDFRONT_URL}/images/favicon-2019.png`, | ||
| }; | ||
|
|
||
| export default CONST; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| // Web and desktop implementation only. Do not import for direct use. Use Notification. | ||
|
|
||
| import Str from '../Str'; | ||
| import CONST from '../../CONST'; | ||
| import * as ActiveClientManager from '../ActiveClientManager'; | ||
|
|
||
| const EXPENSIFY_ICON_URL = `${CONST.CLOUDFRONT_URL}/images/favicon-2019.png`; | ||
| const DEFAULT_DELAY = 4000; | ||
|
|
||
| /** | ||
| * Checks if the user has granted permission to show browser notifications | ||
| * | ||
| * @return {Promise} | ||
| */ | ||
| function canUseBrowserNotifications() { | ||
| return new Promise((resolve) => { | ||
| // They have no browser notifications so we can't use this feature | ||
| if (!window.Notification) { | ||
| return resolve(false); | ||
| } | ||
|
|
||
| // Check if they previously granted or denied us access to send a notification | ||
| const permissionGranted = Notification.permission === 'granted'; | ||
|
|
||
| if (permissionGranted || Notification.permisson === 'denied') { | ||
| return resolve(permissionGranted); | ||
| } | ||
|
|
||
| // Check their global preferences for browser notifications and ask permission if they have none | ||
| Notification.requestPermission() | ||
| .then((status) => { | ||
| resolve(status === 'granted'); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Light abstraction around browser push notifications. | ||
| * Checks for permission before determining whether to send. | ||
| * | ||
| * @param {Object} params | ||
| * @param {String} params.title | ||
| * @param {String} params.body | ||
| * @param {String} [params.icon] Default to Expensify logo | ||
| * @param {Number} [params.delay] | ||
| * @param {Function} [params.onClick] | ||
| * @param {String} [params.tag] | ||
| * | ||
| * @return {Promise} - resolves with Notification object or undefined | ||
| */ | ||
| function push({ | ||
| title, | ||
| body, | ||
| delay = DEFAULT_DELAY, | ||
| onClick = () => {}, | ||
| tag = '', | ||
| icon = EXPENSIFY_ICON_URL, | ||
| }) { | ||
| return new Promise((resolve) => { | ||
| if (!title || !body) { | ||
| throw new Error('BrowserNotification must include title and body parameter.'); | ||
| } | ||
|
|
||
| canUseBrowserNotifications().then((canUseNotifications) => { | ||
| if (!canUseNotifications) { | ||
| resolve(); | ||
| return; | ||
| } | ||
|
|
||
| const notification = new Notification(title, { | ||
| body, | ||
| icon, | ||
| tag, | ||
| }); | ||
|
|
||
| // If we pass in a delay param greater than 0 the notification | ||
| // will auto-close after the specified time. | ||
| if (delay > 0) { | ||
| setTimeout(notification.close.bind(notification), delay); | ||
| } | ||
|
|
||
| notification.onclick = (event) => { | ||
| event.preventDefault(); | ||
| onClick(); | ||
| window.parent.focus(); | ||
| window.focus(); | ||
| notification.close(); | ||
| }; | ||
|
|
||
| resolve(notification); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * BrowserNotification | ||
| * @namespace | ||
| */ | ||
| export default { | ||
| /** | ||
| * Create a report comment notification | ||
| * | ||
| * @param {Object} params | ||
| * @param {Object} params.reportAction | ||
| * @param {Function} params.onClick | ||
| */ | ||
| pushReportCommentNotification({reportAction, onClick}) { | ||
| if (!ActiveClientManager.isClientTheLeader()) { | ||
| return; | ||
| } | ||
|
|
||
| const {person, message} = reportAction; | ||
|
|
||
| const plainTextPerson = Str.htmlDecode(person.map(f => f.text).join()); | ||
|
|
||
| // Specifically target the comment part of the message | ||
| const plainTextMessage = Str.htmlDecode((message.find(f => f.type === 'COMMENT') || {}).text); | ||
|
|
||
| push({ | ||
| title: `New message from ${plainTextPerson}`, | ||
| body: plainTextMessage, | ||
| delay: 0, | ||
| onClick, | ||
| }); | ||
| }, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import BrowserNotifications from './BrowserNotifications'; | ||
|
|
||
| function showCommentNotification({reportAction, onClick}) { | ||
| BrowserNotifications.pushReportCommentNotification({reportAction, onClick}); | ||
| } | ||
|
|
||
| export default { | ||
| showCommentNotification, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| // Browser Notifications are not supported on mobile so we'll just noop here. | ||
| export default { | ||
| showCommentNotification: () => {}, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should move this to
CONST.jsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah we probably should I'll fix this up.