Implement system notifications with cordova

Removes our manual sound notification, this is the platform's job in an app.
Also don't expect Notification to exist, mobile Chrome doesn't know it
Lorenz Hübschle-Schneider 12 years ago
parent 319557c9d3
commit e761a359ee
  1. BIN
      assets/audio/sonar.mp3
  2. BIN
      assets/audio/sonar.ogg
  3. 1
      index.html
  4. 5
      js/glowingbear.js
  5. 41
      js/notifications.js

Binary file not shown.

Binary file not shown.

@ -287,7 +287,6 @@ $ openssl req -nodes -newkey rsa:4096 -keyout relay.pem -x509 -days 365 -out rel
<div input-bar input-id="sendMessage" command="command"></div>
</div>
</div>
<div id="soundNotification"></div>
<div id="reconnect" class="alert alert-danger" ng-click="reconnect()" ng-show="reconnecting">
<p><strong>Connection to WeeChat lost</strong></p>
<i class="glyphicon glyphicon-refresh"></i>

@ -232,6 +232,11 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout',
$rootScope.$emit('notificationChanged');
$scope.connectbutton = 'Connect';
$scope.connectbuttonicon = 'glyphicon-chevron-right';
// 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.connectbuttonicon = 'glyphicon-chevron-right';

@ -33,6 +33,21 @@ weechat.factory('notifications', ['$rootScope', '$log', 'models', 'settings', fu
$log.info('Service Worker err:', err);
});
}
document.addEventListener('deviceready', function() {
// Add cordova local notification click handler
if (cordova !== null && cordova.plugins !== undefined && cordova.plugins.notification !== undefined &&
cordova.plugins.notification.local !== undefined) {
cordova.plugins.notification.local.on("click", function (notification) {
// go to buffer
var data = JSON.parse(notification.data);
models.setActiveBuffer(data.buffer);
window.focus();
// clear this notification
cordova.plugins.notification.local.clear(notification.id);
});
}
});
};
var showNotification = function(buffer, title, body) {
@ -65,8 +80,7 @@ weechat.factory('notifications', ['$rootScope', '$log', 'models', 'settings', fu
toastNotifier.show(toast);
} else {
} else if (typeof Notification !== 'undefined') {
var notification = new Notification(title, {
body: body,
icon: 'assets/img/favicon.png'
@ -95,6 +109,22 @@ weechat.factory('notifications', ['$rootScope', '$log', 'models', 'settings', fu
notification.onclose = function() {
delete notifications[this.id];
};
} else if (cordova !== undefined && cordova.plugins !== undefined && cordova.plugins.notification !== undefined && cordova.plugins.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)
cordova.plugins.notification.local.clear(id);
// Send new notification
cordova.plugins.notification.local.schedule({
id: id,
text: body,
title: title,
data: { buffer: buffer.id } // remember buffer id for when the notification is clicked
});
}
@ -161,13 +191,6 @@ weechat.factory('notifications', ['$rootScope', '$log', 'models', 'settings', fu
title += buffer.shortName + " (" + buffer.server + ")";
showNotification(buffer, title, body);
if (settings.soundnotification) {
// TODO fill in a sound file
var audioFile = "assets/audio/sonar";
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;
}
};
var cancelAll = function() {

Loading…
Cancel
Save