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:
Scott Nonnenberg 2017-09-29 09:15:28 -07:00 committed by GitHub
parent 3b4fd2d0e0
commit 10a38297b8
4 changed files with 52 additions and 36 deletions

View file

@ -179,6 +179,8 @@
retryCached: connectCount === 1,
};
Whisper.Notifications.disable(); // avoid notification flood until empty
// initialize the socket and start listening for messages
messageReceiver = new textsecure.MessageReceiver(
SERVER_URL, USERNAME, PASSWORD, mySignalingKey, options
@ -239,6 +241,8 @@
view.onEmpty();
}
}, 500);
Whisper.Notifications.enable();
}
function onProgress(ev) {
var count = ev.count;
@ -509,9 +513,7 @@
}
conversation.trigger('newmessage', message);
if (initialLoadComplete) {
conversation.notify(message);
}
conversation.notify(message);
if (ev.confirm) {
ev.confirm();

View file

@ -1073,14 +1073,9 @@
if (!message.isIncoming()) {
return Promise.resolve();
}
if (window.isFocused()) {
return Promise.resolve();
}
window.drawAttention();
var conversationId = this.id;
ConversationController.getOrCreateAndWait(message.get('source'), 'private')
return ConversationController.getOrCreateAndWait(message.get('source'), 'private')
.then(function(sender) {
return sender.getNotificationIcon().then(function(iconUrl) {
console.log('adding notification');

View file

@ -326,10 +326,7 @@
this.send(promise);
}
},
handleDataMessage: function(dataMessage, confirm, options) {
options = options || {};
_.defaults(options, {initialLoadComplete: true});
handleDataMessage: function(dataMessage, confirm) {
// This function is called from the background script in a few scenarios:
// 1. on an incoming message
// 2. on a sent message sync'd from another device
@ -496,7 +493,7 @@
// because we need to start expiration timers, etc.
message.markRead();
}
if (message.get('unread') && options.initialLoadComplete) {
if (message.get('unread')) {
conversation.notify(message);
}

View file

@ -12,25 +12,37 @@
MESSAGE : 'message'
};
var enabled = false;
Whisper.Notifications = new (Backbone.Collection.extend({
initialize: function() {
this.on('add', this.update);
this.on('remove', this.onRemove);
},
onclick: function() {
var conversation;
var last = this.last();
if (last) {
conversation = ConversationController.get(last.get('conversationId'));
}
onClick: function(conversationId) {
var conversation = ConversationController.get(conversationId);
this.trigger('click', conversation);
this.clear();
},
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) {
return;
}
if (window.isFocused()) {
// The window is focused. Consider yourself notified.
this.clear();
return;
}
window.drawAttention();
var audioNotification = storage.get('audio-notification') || false;
var setting = storage.get('notification-setting') || 'message';
@ -59,7 +71,11 @@
iconUrl = last.get('iconUrl');
break;
case SETTINGS.MESSAGE:
title = last.get('title');
if (this.length === 1) {
title = last.get('title');
} else {
title = newMessageCount;
}
message = last.get('message');
iconUrl = last.get('iconUrl');
break;
@ -70,26 +86,32 @@
tag : 'signal',
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() {
return storage.get('notification-setting') || 'message';
},
showMessage: function() {
return this.getSetting() === SETTINGS.MESSAGE;
},
showSender: function() {
var setting = this.getSetting();
return (setting === SETTINGS.MESSAGE || setting === SETTINGS.NAME);
return storage.get('notification-setting') || SETTINGS.MESSAGE;
},
onRemove: function() {
console.log('remove notification');
if (this.length === 0) {
return;
}
},
clear: function() {
console.log('remove all notifications');
this.reset([]);
}
},
enable: function() {
var update = !enabled;
enabled = true;
if (update) {
this.update();
}
},
disable: function() {
enabled = false;
},
}))();
})();