Permalink
Browse files

Chrome Desktop Notification support

  • Loading branch information...
1 parent 246d624 commit da7af0d8cd159a0bb417917020339e3024c70385 @maikelwever maikelwever committed Jun 15, 2013
Showing with 42 additions and 1 deletion.
  1. +40 −1 assets/js/models.js
  2. +2 −0 views/templates.jade
View
@@ -68,7 +68,46 @@ var ChatWindow = Backbone.Model.extend({
signal = true;
}
// All PMs & mentions
- if (signal) this.trigger('forMe', 'message');
+ if (signal) {
+ this.trigger('forMe', 'message');
+
+ // Chrome Desktop Notfication support.
+ // Written by Maikel Wever (GitHub @maikelwever)
+ // Only send notification if Chrome supports it,
+ // and the page is hidden (to prevent annoyingness)
+ if (window.webkitNotifications &&
+ (document.webkitHidden == true || document.webkitHidden == undefined)
+ ) {
+ // Permissions should be granted in the settings window @ home
+ // This should be done only once per domain
+ if (window.webkitNotifications.checkPermission() == 0) {
+
+ // This builds the message title, according to the type of message.
+ var messageTitle = "Mention in ";
+ if (msg.attributes.type == "pm") {
+ messageTitle = "PM from ";
+ }
+ messageTitle += msg.collection.channel.attributes.name;
+
+ // Create a webkit notification, with the subway logo,
+ // the above generated message title and
+ // the actual message send to the user
+ var notification = window.webkitNotifications.createNotification(
+ '/assets/images/subway.png',
+ messageTitle,
+ msg.attributes.sender + " says " + msg.attributes.text
+ );
+ // Notifications API requires an explicit show to show a notification
+ notification.show();
+
+ // Programatically close the notification after 5 seconds.
+ setTimeout(function() {
+ notification.close();
+ }, 5000);
+ }
+ }
+ // END Chrome Desktop Notification support
+ }
}
});
View
@@ -101,6 +101,8 @@ script(id="overview_settings", type="text/html")
#home_parent
a.overview_button#home Home
p#settings-soon Coming soon.
+ p#settings-chrome-notifications
+ a(id="chrome-notifications-allow-button", class="btn btn-success", onclick="javascript:window.webkitNotifications.requestPermission();") Enable chrome desktop notifications (will ask for permission)
script(id="overview_login", type="text/html")
#home_parent

0 comments on commit da7af0d

Please sign in to comment.