Only show notifications when done with sync (#1507)
This prevents the parade of notifications if a machine wakes up from sleep. Basically covers situations that the loading screen doesn't already. When disabled, notifications will be cached until they are subsequently re-enabled, at which time all the pending notifications will be summarized. From the background page, notifications are disabled during connection attempts until an empty event. This means we can always safely call conversation.notify to queue a notification for the next batch, dropping some options from message and conversation model methods. We've also moved the calls to check window focus and draw attention to the window, which were previously included in the conversation model, but are now performed by the Notification system, because the time that the notification is displayed might be some time after the message is added by the conversation, so decisions about focus and attention should be made in that moment and not before. // FREEBIE
This commit is contained in:
parent
3b4fd2d0e0
commit
10a38297b8
4 changed files with 52 additions and 36 deletions
|
@ -179,6 +179,8 @@
|
||||||
retryCached: connectCount === 1,
|
retryCached: connectCount === 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Whisper.Notifications.disable(); // avoid notification flood until empty
|
||||||
|
|
||||||
// initialize the socket and start listening for messages
|
// initialize the socket and start listening for messages
|
||||||
messageReceiver = new textsecure.MessageReceiver(
|
messageReceiver = new textsecure.MessageReceiver(
|
||||||
SERVER_URL, USERNAME, PASSWORD, mySignalingKey, options
|
SERVER_URL, USERNAME, PASSWORD, mySignalingKey, options
|
||||||
|
@ -239,6 +241,8 @@
|
||||||
view.onEmpty();
|
view.onEmpty();
|
||||||
}
|
}
|
||||||
}, 500);
|
}, 500);
|
||||||
|
|
||||||
|
Whisper.Notifications.enable();
|
||||||
}
|
}
|
||||||
function onProgress(ev) {
|
function onProgress(ev) {
|
||||||
var count = ev.count;
|
var count = ev.count;
|
||||||
|
@ -509,9 +513,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
conversation.trigger('newmessage', message);
|
conversation.trigger('newmessage', message);
|
||||||
if (initialLoadComplete) {
|
conversation.notify(message);
|
||||||
conversation.notify(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ev.confirm) {
|
if (ev.confirm) {
|
||||||
ev.confirm();
|
ev.confirm();
|
||||||
|
|
|
@ -1073,14 +1073,9 @@
|
||||||
if (!message.isIncoming()) {
|
if (!message.isIncoming()) {
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
}
|
}
|
||||||
if (window.isFocused()) {
|
|
||||||
return Promise.resolve();
|
|
||||||
}
|
|
||||||
|
|
||||||
window.drawAttention();
|
|
||||||
var conversationId = this.id;
|
var conversationId = this.id;
|
||||||
|
|
||||||
ConversationController.getOrCreateAndWait(message.get('source'), 'private')
|
return ConversationController.getOrCreateAndWait(message.get('source'), 'private')
|
||||||
.then(function(sender) {
|
.then(function(sender) {
|
||||||
return sender.getNotificationIcon().then(function(iconUrl) {
|
return sender.getNotificationIcon().then(function(iconUrl) {
|
||||||
console.log('adding notification');
|
console.log('adding notification');
|
||||||
|
|
|
@ -326,10 +326,7 @@
|
||||||
this.send(promise);
|
this.send(promise);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
handleDataMessage: function(dataMessage, confirm, options) {
|
handleDataMessage: function(dataMessage, confirm) {
|
||||||
options = options || {};
|
|
||||||
_.defaults(options, {initialLoadComplete: true});
|
|
||||||
|
|
||||||
// This function is called from the background script in a few scenarios:
|
// This function is called from the background script in a few scenarios:
|
||||||
// 1. on an incoming message
|
// 1. on an incoming message
|
||||||
// 2. on a sent message sync'd from another device
|
// 2. on a sent message sync'd from another device
|
||||||
|
@ -496,7 +493,7 @@
|
||||||
// because we need to start expiration timers, etc.
|
// because we need to start expiration timers, etc.
|
||||||
message.markRead();
|
message.markRead();
|
||||||
}
|
}
|
||||||
if (message.get('unread') && options.initialLoadComplete) {
|
if (message.get('unread')) {
|
||||||
conversation.notify(message);
|
conversation.notify(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,25 +12,37 @@
|
||||||
MESSAGE : 'message'
|
MESSAGE : 'message'
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var enabled = false;
|
||||||
|
|
||||||
Whisper.Notifications = new (Backbone.Collection.extend({
|
Whisper.Notifications = new (Backbone.Collection.extend({
|
||||||
initialize: function() {
|
initialize: function() {
|
||||||
this.on('add', this.update);
|
this.on('add', this.update);
|
||||||
this.on('remove', this.onRemove);
|
this.on('remove', this.onRemove);
|
||||||
},
|
},
|
||||||
onclick: function() {
|
onClick: function(conversationId) {
|
||||||
var conversation;
|
var conversation = ConversationController.get(conversationId);
|
||||||
var last = this.last();
|
|
||||||
if (last) {
|
|
||||||
conversation = ConversationController.get(last.get('conversationId'));
|
|
||||||
}
|
|
||||||
this.trigger('click', conversation);
|
this.trigger('click', conversation);
|
||||||
this.clear();
|
|
||||||
},
|
},
|
||||||
update: function() {
|
update: function() {
|
||||||
console.log('updating notifications', this.length);
|
console.log(
|
||||||
|
'updating notifications - count:', this.length,
|
||||||
|
'focused:', window.isFocused(),
|
||||||
|
'enabled:', enabled
|
||||||
|
);
|
||||||
|
if (!enabled) {
|
||||||
|
return; // wait til we are re-enabled
|
||||||
|
}
|
||||||
if (this.length === 0) {
|
if (this.length === 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (window.isFocused()) {
|
||||||
|
// The window is focused. Consider yourself notified.
|
||||||
|
this.clear();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
window.drawAttention();
|
||||||
|
|
||||||
var audioNotification = storage.get('audio-notification') || false;
|
var audioNotification = storage.get('audio-notification') || false;
|
||||||
|
|
||||||
var setting = storage.get('notification-setting') || 'message';
|
var setting = storage.get('notification-setting') || 'message';
|
||||||
|
@ -59,7 +71,11 @@
|
||||||
iconUrl = last.get('iconUrl');
|
iconUrl = last.get('iconUrl');
|
||||||
break;
|
break;
|
||||||
case SETTINGS.MESSAGE:
|
case SETTINGS.MESSAGE:
|
||||||
title = last.get('title');
|
if (this.length === 1) {
|
||||||
|
title = last.get('title');
|
||||||
|
} else {
|
||||||
|
title = newMessageCount;
|
||||||
|
}
|
||||||
message = last.get('message');
|
message = last.get('message');
|
||||||
iconUrl = last.get('iconUrl');
|
iconUrl = last.get('iconUrl');
|
||||||
break;
|
break;
|
||||||
|
@ -70,26 +86,32 @@
|
||||||
tag : 'signal',
|
tag : 'signal',
|
||||||
silent : !audioNotification
|
silent : !audioNotification
|
||||||
});
|
});
|
||||||
notification.onclick = this.onclick.bind(this);
|
|
||||||
|
notification.onclick = this.onClick.bind(this, last.get('conversationId'));
|
||||||
|
|
||||||
|
// We don't want to notify the user about these same messages again
|
||||||
|
this.clear();
|
||||||
},
|
},
|
||||||
getSetting: function() {
|
getSetting: function() {
|
||||||
return storage.get('notification-setting') || 'message';
|
return storage.get('notification-setting') || SETTINGS.MESSAGE;
|
||||||
},
|
|
||||||
showMessage: function() {
|
|
||||||
return this.getSetting() === SETTINGS.MESSAGE;
|
|
||||||
},
|
|
||||||
showSender: function() {
|
|
||||||
var setting = this.getSetting();
|
|
||||||
return (setting === SETTINGS.MESSAGE || setting === SETTINGS.NAME);
|
|
||||||
},
|
},
|
||||||
onRemove: function() {
|
onRemove: function() {
|
||||||
console.log('remove notification');
|
console.log('remove notification');
|
||||||
if (this.length === 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
clear: function() {
|
clear: function() {
|
||||||
|
console.log('remove all notifications');
|
||||||
this.reset([]);
|
this.reset([]);
|
||||||
}
|
},
|
||||||
|
enable: function() {
|
||||||
|
var update = !enabled;
|
||||||
|
enabled = true;
|
||||||
|
if (update) {
|
||||||
|
this.update();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
disable: function() {
|
||||||
|
enabled = false;
|
||||||
|
},
|
||||||
|
|
||||||
}))();
|
}))();
|
||||||
})();
|
})();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue