Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show notification on received message
- Loading branch information
Showing
2 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,38 @@ | ||
import dispatcher from './dispatcher'; | ||
import Notify from 'notifyjs'; | ||
|
||
export function requestPermission() { | ||
if (Notify.needsPermission && Notify.isSupported()) { | ||
Notify.requestPermission(() => console.log('granted notification permissions'), | ||
() => console.log('notification permission denied')); | ||
} | ||
} | ||
|
||
function closeAndFocus(event) { | ||
if (window.parent) { | ||
window.parent.focus(); | ||
} | ||
window.focus(); | ||
window.location.href = '/'; | ||
event.target.close(); | ||
} | ||
|
||
function closeAfterTimeout(event) { | ||
setTimeout(() => { | ||
event.target.close(); | ||
}, 5000); | ||
} | ||
|
||
dispatcher.register((data) => { | ||
if (data.type === 'ONE_MESSAGE') { | ||
const msg = data.payload; | ||
|
||
const notify = new Notify(msg.title, { | ||
body: msg.message, | ||
icon: '/static/favicon.ico', | ||
notifyClick: closeAndFocus, | ||
notifyShow: closeAfterTimeout, | ||
}); | ||
notify.show(); | ||
} | ||
}); |