Improve notification behavior
Only allow one notification at a time. Use a basic notification for normal messages, and image notification for image messages, and a list notification when there are multiple unread messages. // FREEBIE
This commit is contained in:
parent
59313b5177
commit
e8edbe53bc
4 changed files with 133 additions and 27 deletions
|
@ -170,6 +170,49 @@
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (chrome) {
|
||||||
|
chrome.notifications.onClicked.addListener(function() {
|
||||||
|
chrome.notifications.clear('signal');
|
||||||
|
Whisper.Notifications.onclick();
|
||||||
|
});
|
||||||
|
chrome.notifications.onButtonClicked.addListener(function() {
|
||||||
|
chrome.notifications.clear('signal');
|
||||||
|
Whisper.Notifications.clear();
|
||||||
|
getInboxCollection().each(function(model) {
|
||||||
|
if (model.get('unreadCount') > 0) {
|
||||||
|
model.markRead();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
chrome.notifications.onClosed.addListener(function(id, byUser) {
|
||||||
|
if (byUser) {
|
||||||
|
Whisper.Notifications.clear();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
extension.notify = function(options) {
|
||||||
|
if (chrome) {
|
||||||
|
chrome.notifications.clear('signal');
|
||||||
|
chrome.notifications.create('signal', {
|
||||||
|
type : options.type,
|
||||||
|
title : options.title,
|
||||||
|
message : options.message || '', // required
|
||||||
|
iconUrl : options.iconUrl,
|
||||||
|
items : options.items,
|
||||||
|
buttons : options.buttons
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
var notification = new Notification(options.title, {
|
||||||
|
body : options.message,
|
||||||
|
icon : options.iconUrl,
|
||||||
|
tag : 'signal'
|
||||||
|
});
|
||||||
|
notification.onclick = function() {
|
||||||
|
Whisper.Notifications.onclick();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
if (chrome.runtime.onInstalled) {
|
if (chrome.runtime.onInstalled) {
|
||||||
chrome.runtime.onInstalled.addListener(function(options) {
|
chrome.runtime.onInstalled.addListener(function(options) {
|
||||||
if (options.reason === 'install') {
|
if (options.reason === 'install') {
|
||||||
|
|
|
@ -61,9 +61,20 @@
|
||||||
|
|
||||||
return this.get('body');
|
return this.get('body');
|
||||||
},
|
},
|
||||||
|
getNotificationText: function() {
|
||||||
|
var description = this.getDescription();
|
||||||
|
if (description) {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
if (this.get('attachments').length > 0) {
|
||||||
|
return 'Media message';
|
||||||
|
}
|
||||||
|
|
||||||
|
return '';
|
||||||
|
},
|
||||||
updateImageUrl: function() {
|
updateImageUrl: function() {
|
||||||
this.revokeImageUrl();
|
this.revokeImageUrl();
|
||||||
var attachment = message.get('attachments')[0];
|
var attachment = this.get('attachments')[0];
|
||||||
if (attachment) {
|
if (attachment) {
|
||||||
var blob = new Blob([attachment.data], {
|
var blob = new Blob([attachment.data], {
|
||||||
type: attachment.contentType
|
type: attachment.contentType
|
||||||
|
|
|
@ -5,20 +5,73 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
window.Whisper = window.Whisper || {};
|
window.Whisper = window.Whisper || {};
|
||||||
|
|
||||||
Whisper.Notifications = {
|
Whisper.Notifications = new (Backbone.Collection.extend({
|
||||||
isEnabled: function(callback) {
|
initialize: function() {
|
||||||
return Notification.permission === 'granted' &&
|
this.on('add', this.update);
|
||||||
!storage.get('disable-notifications');
|
},
|
||||||
},
|
isEnabled: function(callback) {
|
||||||
enable: function(callback) {
|
return Notification.permission === 'granted' &&
|
||||||
storage.remove('disable-notifications');
|
!storage.get('disable-notifications');
|
||||||
Notification.requestPermission(function(status) {
|
},
|
||||||
callback(status);
|
enable: function(callback) {
|
||||||
});
|
storage.remove('disable-notifications');
|
||||||
},
|
Notification.requestPermission(function(status) {
|
||||||
disable: function() {
|
callback(status);
|
||||||
storage.put('disable-notifications', true);
|
});
|
||||||
}
|
},
|
||||||
};
|
disable: function() {
|
||||||
|
storage.put('disable-notifications', true);
|
||||||
|
},
|
||||||
|
onclick: function() {
|
||||||
|
var last = this.last();
|
||||||
|
if (!last) {
|
||||||
|
openInbox();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var conversation = ConversationController.create({
|
||||||
|
id: last.get('conversationId')
|
||||||
|
});
|
||||||
|
openConversation(conversation);
|
||||||
|
this.clear();
|
||||||
|
},
|
||||||
|
update: function(options) {
|
||||||
|
if (this.length > 1) {
|
||||||
|
var iconUrl = 'images/icon_128.png';
|
||||||
|
var conversationIds = _.uniq(this.map(function(m) {
|
||||||
|
return m.get('conversationId');
|
||||||
|
}));
|
||||||
|
if (conversationIds.length === 1) {
|
||||||
|
iconUrl = this.at(0).get('iconUrl');
|
||||||
|
}
|
||||||
|
extension.notify({
|
||||||
|
type : 'list',
|
||||||
|
iconUrl : iconUrl,
|
||||||
|
title : '' + this.length + ' new messages',
|
||||||
|
message : 'Most recent from ' + this.last().get('title'),
|
||||||
|
items : this.map(function(m) {
|
||||||
|
return {
|
||||||
|
title : m.get('title'),
|
||||||
|
message : m.get('message')
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
buttons : [{
|
||||||
|
title : 'Mark all as read',
|
||||||
|
iconUrl : 'images/check.png'
|
||||||
|
}]
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
var m = this.at(0);
|
||||||
|
extension.notify({
|
||||||
|
type : 'basic',
|
||||||
|
title : m.get('title'),
|
||||||
|
message : m.get('message'),
|
||||||
|
iconUrl : m.get('iconUrl'),
|
||||||
|
imageUrl : m.get('imageUrl')
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
clear: function() {
|
||||||
|
this.reset([]);
|
||||||
|
}
|
||||||
|
}))();
|
||||||
})();
|
})();
|
||||||
|
|
|
@ -93,15 +93,14 @@
|
||||||
var sender = ConversationController.create({id: message.get('source')});
|
var sender = ConversationController.create({id: message.get('source')});
|
||||||
conversation.fetch().then(function() {
|
conversation.fetch().then(function() {
|
||||||
sender.fetch().then(function() {
|
sender.fetch().then(function() {
|
||||||
sender.getNotificationIcon().then(function(icon) {
|
sender.getNotificationIcon().then(function(iconUrl) {
|
||||||
var notification = new Notification(sender.getTitle(), {
|
Whisper.Notifications.add({
|
||||||
body: message.getDescription(),
|
title : sender.getTitle(),
|
||||||
icon: icon,
|
message : message.getNotificationText(),
|
||||||
tag: conversation.id
|
iconUrl : iconUrl,
|
||||||
|
imageUrl : message.getImageUrl(),
|
||||||
|
conversationId: conversation.id
|
||||||
});
|
});
|
||||||
notification.onclick = function() {
|
|
||||||
openConversation(conversation);
|
|
||||||
};
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -152,7 +151,7 @@
|
||||||
};
|
};
|
||||||
|
|
||||||
var open;
|
var open;
|
||||||
function openConversation(conversation) {
|
window.openConversation = function(conversation) {
|
||||||
if (inboxOpened === true) {
|
if (inboxOpened === true) {
|
||||||
var appWindow = chrome.app.window.get(inboxWindowId);
|
var appWindow = chrome.app.window.get(inboxWindowId);
|
||||||
appWindow.contentWindow.openConversation(conversation);
|
appWindow.contentWindow.openConversation(conversation);
|
||||||
|
@ -160,7 +159,7 @@
|
||||||
open = conversation;
|
open = conversation;
|
||||||
openInbox();
|
openInbox();
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
window.getOpenConversation = function() {
|
window.getOpenConversation = function() {
|
||||||
var o = open;
|
var o = open;
|
||||||
open = null;
|
open = null;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue