Show cordova local notifications instead of just vibrating

This is really cool :D
Lorenz Hübschle-Schneider 12 years ago
parent a614e83f18
commit a04bf7f1e0
  1. 45
      js/glowingbear.js

@ -573,6 +573,23 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout',
window.webkitNotifications.requestPermission(); 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(); //models.reinitialize();
$rootScope.$emit('notificationChanged'); $rootScope.$emit('notificationChanged');
$scope.connectbutton = 'Connect'; $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'; $scope.connectbutton = 'Connect';
@ -1070,6 +1092,22 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout',
window.focus(); window.focus();
notification.close(); 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) { if ($scope.soundnotification) {
@ -1078,13 +1116,6 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout',
var soundHTML = '<audio autoplay="autoplay"><source src="' + audioFile + '.ogg" type="audio/ogg" /><source src="' + audioFile + '.mp3" type="audio/mpeg" /></audio>'; var soundHTML = '<audio autoplay="autoplay"><source src="' + audioFile + '.ogg" type="audio/ogg" /><source src="' + audioFile + '.mp3" type="audio/mpeg" /></audio>';
document.getElementById("soundNotification").innerHTML = 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) { $scope.hasUnread = function(buffer) {

Loading…
Cancel
Save