From b974a946b0d7b1c84eebd25b33b60dcaead5d712 Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Fri, 10 Oct 2014 16:08:59 +0000 Subject: [PATCH] Added support for desktop notifications --- js/script.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/js/script.js b/js/script.js index 8039164..682c263 100644 --- a/js/script.js +++ b/js/script.js @@ -12,6 +12,7 @@ // Some global vars to improve performances var selectedConversation = null; var curPhoneNumber = null; +var curContactName = ''; var lastMsgDate = 0; var unreadCount = 0; var originalTitle = document.title; @@ -49,6 +50,7 @@ var refreshConversation = function() { if (document.hasFocus() == false) { unreadCount += fmt[0]; document.title = originalTitle + " (" + unreadCount + ")"; + desktopNotify(unreadCount + " unread message(s) in conversation with " + curContactName); } } @@ -86,10 +88,12 @@ function fetchConversation(phoneNumber) { conversationBuf += '
'; if (typeof jsondata['contactName'] == 'undefined') { $('#ocsms-phone-label').html(phoneNumberLabel); + curContactName = phoneNumberLabel; $('#ocsms-phone-opt-number').html(''); } else { $('#ocsms-phone-label').html(jsondata['contactName']); + curContactName = jsondata['contactName']; $('#ocsms-phone-opt-number').html(phoneNumberLabel); } @@ -197,6 +201,33 @@ function fetchInitialPeerList(jsondata) { } } +function initDesktopNotifies() { + Notification.requestPermission(function (permission) { + if(!('permission' in Notification)) { + Notification.permission = permission; + } + }); +} + +function desktopNotify(msg) { + if (!("Notification" in window)) { + return; + } + else if (Notification.permission === "granted") { + new Notification("ownCloud SMS - " + msg); + } + else if (Notification.permission !== 'denied') { + Notification.requestPermission(function (permission) { + if(!('permission' in Notification)) { + Notification.permission = permission; + } + if (permission === "granted") { + new Notification("ownCloud SMS - " + msg); + } + }); + } +} + (function ($, OC) { $(document).ready(function () { // Register real title @@ -241,6 +272,7 @@ function fetchInitialPeerList(jsondata) { } }); + initDesktopNotifies(); setInterval(refreshConversation, 10000); });