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 8349653305
commit 78e7e342a6
  1. BIN
      assets/audio/sonar.mp3
  2. BIN
      assets/audio/sonar.ogg
  3. 11
      index.html
  4. 7
      js/glowingbear.js
  5. 74
      js/notifications.js

Binary file not shown.

Binary file not shown.

@ -270,7 +270,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="settingsModal" class="gb-modal" data-state="hidden"> <div id="settingsModal" class="gb-modal" data-state="hidden">
<div class="backdrop" ng-click="closeModal($event)"></div> <div class="backdrop" ng-click="closeModal($event)"></div>
<div class="modal-dialog"> <div class="modal-dialog">
@ -390,16 +389,6 @@ $ openssl req -nodes -newkey rsa:4096 -keyout relay.pem -x509 -days 365 -out rel
</div> </div>
</form> </form>
</li> </li>
<li>
<form class="form-inline" role="form">
<div class="checkbox">
<label>
<input type="checkbox" ng-model="soundnotification">
Play sound on notification
</label>
</div>
</form>
</li>
</ul> </ul>
</div> </div>
<div class="modal-footer"> <div class="modal-footer">

@ -201,6 +201,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';
@ -260,8 +265,6 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout',
$store.bind($scope, "showtimestamp", showtimestamp); $store.bind($scope, "showtimestamp", showtimestamp);
// Save setting for showing seconds on timestamps // Save setting for showing seconds on timestamps
$store.bind($scope, "showtimestampSeconds", false); $store.bind($scope, "showtimestampSeconds", false);
// Save setting for playing sound on notification
$store.bind($scope, "soundnotification", false);
// Save setting for font family // Save setting for font family
$store.bind($scope, "fontfamily"); $store.bind($scope, "fontfamily");
// Save setting for theme // Save setting for theme

@ -21,6 +21,21 @@ weechat.factory('notifications', ['$rootScope', '$log', 'models', function($root
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);
}
};
}
}; };
@ -84,31 +99,44 @@ weechat.factory('notifications', ['$rootScope', '$log', 'models', function($root
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'
});
// Cancel notification automatically // Cancel notification automatically
var timeout = 15*1000; var timeout = 15*1000;
notification.onshow = function() { notification.onshow = function() {
setTimeout(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); };
}; } else if (window.plugin !== undefined && window.plugin.notification !== undefined && window.plugin.notification.local !== undefined) {
// Cordova local notification
// Click takes the user to the buffer // Calculate notification id from buffer ID
notification.onclick = function() { // Needs to be unique number, but we'll only ever have one per buffer
models.setActiveBuffer(buffer.id); var id = parseInt(buffer.id, 16);
window.focus();
notification.close(); // Cancel previous notification for buffer (if there was one)
}; window.plugin.notification.local.cancel(id);
if ($rootScope.soundnotification) { // Send new notification
// TODO fill in a sound file window.plugin.notification.local.add({
var audioFile = "assets/audio/sonar"; id: id,
var soundHTML = '<audio autoplay="autoplay"><source src="' + audioFile + '.ogg" type="audio/ogg" /><source src="' + audioFile + '.mp3" type="audio/mpeg" /></audio>'; message: body,
document.getElementById("soundNotification").innerHTML = soundHTML; title: title,
autoCancel: true,
json: JSON.stringify({ buffer: buffer.id }) // remember buffer id for when the notification is clicked
});
} }
}; };

Loading…
Cancel
Save