From a04bf7f1e0b20ee840ac0af5bd1ff2fb149d685e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20H=C3=BCbschle-Schneider?= Date: Thu, 13 Mar 2014 20:31:32 +0000 Subject: [PATCH] Show cordova local notifications instead of just vibrating This is really cool :D --- js/glowingbear.js | 45 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/js/glowingbear.js b/js/glowingbear.js index bf21aee..ca2ae3d 100644 --- a/js/glowingbear.js +++ b/js/glowingbear.js @@ -573,6 +573,23 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout', window.webkitNotifications.requestPermission(); } } + + // Add cordova local notification click handler + if (window.plugin !== undefined && window.plugin.notification !== undefined && window.plugin.notification.local !== undefined) { + window.plugin.notification.local.onclick = function (id, state, json) { + // Parse payload + var data = JSON.parse(json); + var buffer = data.buffer; + if (buffer) { + // Hide sidebar, open notification buffer + $scope.showSidebar = false; + models.setActiveBuffer(buffer); + } + + // Cancel notification + window.plugin.notification.local.cancel(id); + }; + } }; @@ -774,6 +791,11 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout', //models.reinitialize(); $rootScope.$emit('notificationChanged'); $scope.connectbutton = 'Connect'; + + // Clear all cordova notifications + if (window.plugin !== undefined && window.plugin.notification !== undefined && window.plugin.notification.local !== undefined) { + window.plugin.notification.local.cancelAll(); + } }); $scope.connectbutton = 'Connect'; @@ -1070,6 +1092,22 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout', window.focus(); notification.close(); }; + } else if (window.plugin !== undefined && window.plugin.notification !== undefined && window.plugin.notification.local !== undefined) { + // Cordova local notification + // Calculate notification id from buffer ID + // Needs to be unique number, but we'll only ever have one per buffer + var id = parseInt(buffer.id, 16); + + // Cancel previous notification for buffer (if there was one) + window.plugin.notification.local.cancel(id); + + // Send new notification + window.plugin.notification.local.add({ + id: id, + message: body, + title: title, + json: JSON.stringify({ buffer: buffer.id }) // remember buffer id for when the notification is clicked + }); } if ($scope.soundnotification) { @@ -1078,13 +1116,6 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout', var soundHTML = ''; document.getElementById("soundNotification").innerHTML = soundHTML; } - - if (navigator.notification !== undefined) { - console.log('vibrating!'); - navigator.notification.vibrate(500); - } else { - console.log('no notification api :(', navigator.notification); - } }; $scope.hasUnread = function(buffer) {