Merge pull request #3422 from riking/notifications-patches

FIX: Don't ask for notification permission until first one
This commit is contained in:
Sam 2015-05-04 10:10:23 +10:00
commit 9723b77b6d

View File

@ -1,6 +1,7 @@
let primaryTab = false; let primaryTab = false;
let liveEnabled = false; let liveEnabled = false;
let havePermission = null;
let mbClientId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; let mbClientId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
let lastAction = -1; let lastAction = -1;
@ -15,25 +16,37 @@ let notificationTagName; // "discourse-notification-popup-" + Discourse.SiteSett
function init(messageBus) { function init(messageBus) {
liveEnabled = false; liveEnabled = false;
mbClientId = messageBus.clientId; mbClientId = messageBus.clientId;
requestPermission().then(function() {
try { if (!Discourse.User.current()) {
localStorage.getItem(focusTrackerKey); return;
} catch (e) { }
Em.Logger.info('Discourse desktop notifications are disabled - localStorage denied.');
return; try {
} localStorage.getItem(focusTrackerKey);
liveEnabled = true; } catch (e) {
Em.Logger.info('Discourse desktop notifications are enabled.'); Em.Logger.info('Discourse desktop notifications are disabled - localStorage denied.');
try { return;
// Permission is granted, continue with setup }
setupNotifications();
} catch (e) { if (!Notification) {
Em.Logger.error(e); Em.Logger.info('Discourse desktop notifications are disabled - not supported by browser');
} return;
}).catch(function() { }
liveEnabled = false;
//Em.Logger.debug('Discourse desktop notifications are disabled - permission denied.'); if (Notification.permission === "granted") {
}); havePermission = true;
} else if (Notification.permission === "denied") {
havePermission = false;
return;
}
liveEnabled = true;
try {
// Preliminary checks passed, continue with setup
setupNotifications();
} catch (e) {
Em.Logger.error(e);
}
} }
// This function is only called if permission was granted // This function is only called if permission was granted
@ -139,27 +152,29 @@ function onNotification(currentUser) {
const notificationBody = bodyParts.join("\n"); const notificationBody = bodyParts.join("\n");
const notificationIcon = Discourse.SiteSettings.logo_small_url || Discourse.SiteSettings.logo_url; const notificationIcon = Discourse.SiteSettings.logo_small_url || Discourse.SiteSettings.logo_url;
// This shows the notification! requestPermission().then(function() {
const notification = new Notification(notificationTitle, { // This shows the notification!
body: notificationBody, const notification = new Notification(notificationTitle, {
icon: notificationIcon, body: notificationBody,
tag: notificationTagName icon: notificationIcon,
tag: notificationTagName
});
const firstUnseen = unseen[0];
function clickEventHandler() {
Discourse.URL.routeTo(_notificationUrl(firstUnseen));
// Cannot delay this until the page renders
// due to trigger-based permissions
window.focus();
}
notification.addEventListener('click', clickEventHandler);
setTimeout(function() {
notification.close();
notification.removeEventListener('click', clickEventHandler);
}, 10 * 1000);
}); });
const firstUnseen = unseen[0];
function clickEventHandler() {
Discourse.URL.routeTo(_notificationUrl(firstUnseen));
// Cannot delay this until the page renders :(
// due to trigger-based permissions
window.focus();
}
notification.addEventListener('click', clickEventHandler);
setTimeout(function() {
notification.close();
notification.removeEventListener('click', clickEventHandler);
}, 10 * 1000);
}); });
} }
} }
@ -191,15 +206,21 @@ function updateSeenNotificationDatesFrom(notifications) {
// Utility function // Utility function
// Wraps Notification.requestPermission in a Promise // Wraps Notification.requestPermission in a Promise
function requestPermission() { function requestPermission() {
return new Ember.RSVP.Promise(function(resolve, reject) { if (havePermission === true) {
Notification.requestPermission(function(status) { return Ember.RSVP.resolve();
if (status === "granted") { } else if (havePermission === false) {
resolve(); return Ember.RSVP.reject();
} else { } else {
reject(); return new Ember.RSVP.Promise(function(resolve, reject) {
} Notification.requestPermission(function(status) {
if (status === "granted") {
resolve();
} else {
reject();
}
});
}); });
}); }
} }
function i18nKey(notification) { function i18nKey(notification) {