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 11 years ago
parent c47ef051d4
commit 41e59e2fb8
  1. BIN
      assets/audio/sonar.mp3
  2. BIN
      assets/audio/sonar.ogg
  3. 1
      index.html
  4. 5
      js/glowingbear.js
  5. 93
      js/notifications.js

Binary file not shown.

Binary file not shown.

@ -277,7 +277,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 input-bar input-id="sendMessage" command="command"></div>
</div> </div>
</div> </div>
<div id="soundNotification"></div>
<div id="reconnect" class="alert alert-danger" ng-click="reconnect()" ng-show="reconnecting"> <div id="reconnect" class="alert alert-danger" ng-click="reconnect()" ng-show="reconnecting">
<p><strong>Connection to WeeChat lost</strong></p> <p><strong>Connection to WeeChat lost</strong></p>
<i class="glyphicon glyphicon-refresh"></i> <i class="glyphicon glyphicon-refresh"></i>

@ -239,6 +239,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';

@ -22,6 +22,21 @@ weechat.factory('notifications', ['$rootScope', '$log', 'models', 'settings', fu
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
// TODO does this work?
$rootScope.hideSidebar();
models.setActiveBuffer(buffer);
}
};
}
}; };
@ -85,40 +100,54 @@ weechat.factory('notifications', ['$rootScope', '$log', 'models', 'settings', fu
title += buffer.shortName; title += buffer.shortName;
title += buffer.fullName.replace(/irc.([^\.]+)\..+/, " ($1)"); title += buffer.fullName.replace(/irc.([^\.]+)\..+/, " ($1)");
var notification = new Notification(title, { // Chrome for Android doesn't know this
body: body, if (typeof Notification !== 'undefined') {
icon: 'assets/img/favicon.png' var notification = new Notification(title, {
}); body: body,
icon: 'assets/img/favicon.png'
// Save notification, so we can close all outstanding ones when disconnecting });
notification.id = notifications.length;
notifications.push(notification);
// Cancel notification automatically // Save notification, so we can close all outstanding ones when disconnecting
var timeout = 15*1000; notification.id = notifications.length;
notification.onshow = function() { notifications.push(notification);
setTimeout(function() {
// Cancel notification automatically
var timeout = 15*1000;
notification.onshow = function() {
setTimeout(function() {
notification.close();
}, timeout);
};
// Click takes the user to the buffer
notification.onclick = function() {
models.setActiveBuffer(buffer.id);
window.focus();
notification.close(); notification.close();
}, timeout); };
};
// Remove from list of active notifications
// Click takes the user to the buffer notification.onclose = function() {
notification.onclick = function() { delete notifications[this.id];
models.setActiveBuffer(buffer.id); };
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
// Remove from list of active notifications // Needs to be unique number, but we'll only ever have one per buffer
notification.onclose = function() { var id = parseInt(buffer.id, 16);
delete notifications[this.id];
}; // Cancel previous notification for buffer (if there was one)
window.plugin.notification.local.cancel(id);
if (settings.soundnotification) {
// TODO fill in a sound file // Send new notification
var audioFile = "assets/audio/sonar"; window.plugin.notification.local.add({
var soundHTML = '<audio autoplay="autoplay"><source src="' + audioFile + '.ogg" type="audio/ogg" /><source src="' + audioFile + '.mp3" type="audio/mpeg" /></audio>'; id: id,
document.getElementById("soundNotification").innerHTML = soundHTML; message: body,
title: title,
autoCancel: true,
json: JSON.stringify({ buffer: buffer.id }) // remember buffer id for when the notification is clicked
});
} }
}; };

Loading…
Cancel
Save