Created
April 5, 2014 16:18
-
-
Save azu/9993939 to your computer and use it in GitHub Desktop.
Web Notifications as promise
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
"use strict"; | |
function notifyMessage(message) { | |
return new Promise(function (resolve, reject) { | |
if (Notification.permission === "granted") { | |
var notification = new Notification(message); | |
return resolve(notification); | |
} else if (Notification) { | |
Notification.requestPermission(function (status) { | |
if (Notification.permission !== status) { | |
Notification.permission = status; | |
} | |
if (status === "granted") { | |
var notification = new Notification(message); | |
return resolve(notification); | |
} else { | |
reject(new Error("user denied")); | |
} | |
}); | |
} else { | |
reject(new Error("doesn't support Notification API")); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment