You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
946 B

  1. // Handles browser quirks, based on
  2. // https://developer.mozilla.org/en-US/docs/Web/API/Notifications_API/Using_the_Notifications_API
  3. const checkNotificationPromise = () => {
  4. try {
  5. // eslint-disable-next-line promise/catch-or-return, promise/valid-params
  6. Notification.requestPermission().then();
  7. } catch(e) {
  8. return false;
  9. }
  10. return true;
  11. };
  12. const handlePermission = (permission, callback) => {
  13. // Whatever the user answers, we make sure Chrome stores the information
  14. if(!('permission' in Notification)) {
  15. Notification.permission = permission;
  16. }
  17. callback(Notification.permission);
  18. };
  19. export const requestNotificationPermission = (callback) => {
  20. if (checkNotificationPromise()) {
  21. Notification.requestPermission().then((permission) => handlePermission(permission, callback)).catch(console.warn);
  22. } else {
  23. Notification.requestPermission((permission) => handlePermission(permission, callback));
  24. }
  25. };