2014-05-14 22:13:25 -07:00
|
|
|
'use strict';
|
|
|
|
|
2014-06-18 12:02:31 -07:00
|
|
|
angular.module('copayApp.services').
|
2016-05-31 14:56:02 -07:00
|
|
|
factory('notification', function($timeout, platformInfo) {
|
2014-05-14 22:13:25 -07:00
|
|
|
|
2016-05-31 14:56:02 -07:00
|
|
|
var isCordova = platformInfo.isCordova;
|
2014-12-01 06:19:18 -08:00
|
|
|
var notifications = [];
|
|
|
|
|
2015-03-06 07:00:10 -08:00
|
|
|
/*
|
2014-12-04 10:45:50 -08:00
|
|
|
ls.getItem('notifications', function(err, data) {
|
|
|
|
if (data) {
|
|
|
|
notifications = JSON.parse(data);
|
|
|
|
}
|
|
|
|
});
|
2015-03-06 07:00:10 -08:00
|
|
|
*/
|
2014-12-01 06:19:18 -08:00
|
|
|
|
|
|
|
var queue = [];
|
2014-05-14 22:13:25 -07:00
|
|
|
var settings = {
|
2014-06-24 08:36:32 -07:00
|
|
|
info: {
|
2014-10-23 18:54:23 -07:00
|
|
|
duration: 6000,
|
2014-06-24 08:36:32 -07:00
|
|
|
enabled: true
|
|
|
|
},
|
|
|
|
funds: {
|
2014-10-22 14:54:44 -07:00
|
|
|
duration: 7000,
|
2014-06-24 08:36:32 -07:00
|
|
|
enabled: true
|
|
|
|
},
|
2014-06-27 11:32:52 -07:00
|
|
|
version: {
|
|
|
|
duration: 60000,
|
|
|
|
enabled: true
|
|
|
|
},
|
2014-06-24 08:36:32 -07:00
|
|
|
warning: {
|
2014-10-22 14:54:44 -07:00
|
|
|
duration: 7000,
|
2014-06-24 08:36:32 -07:00
|
|
|
enabled: true
|
|
|
|
},
|
|
|
|
error: {
|
2014-10-23 18:54:23 -07:00
|
|
|
duration: 7000,
|
2014-06-24 08:36:32 -07:00
|
|
|
enabled: true
|
|
|
|
},
|
|
|
|
success: {
|
2014-07-24 13:36:44 -07:00
|
|
|
duration: 5000,
|
2014-06-24 08:36:32 -07:00
|
|
|
enabled: true
|
|
|
|
},
|
|
|
|
progress: {
|
|
|
|
duration: 0,
|
|
|
|
enabled: true
|
|
|
|
},
|
|
|
|
custom: {
|
|
|
|
duration: 35000,
|
|
|
|
enabled: true
|
|
|
|
},
|
2014-05-14 22:13:25 -07:00
|
|
|
details: true,
|
|
|
|
localStorage: false,
|
2014-12-09 19:02:12 -08:00
|
|
|
html5Mode: false,
|
2014-05-16 07:17:22 -07:00
|
|
|
html5DefaultIcon: 'img/favicon.ico'
|
2014-05-14 22:13:25 -07:00
|
|
|
};
|
|
|
|
|
2014-06-24 08:36:32 -07:00
|
|
|
function html5Notify(icon, title, content, ondisplay, onclose) {
|
2014-10-24 05:36:28 -07:00
|
|
|
if (window.webkitNotifications && window.webkitNotifications.checkPermission() === 0) {
|
2014-06-24 08:36:32 -07:00
|
|
|
if (!icon) {
|
2014-05-16 07:17:22 -07:00
|
|
|
icon = 'img/favicon.ico';
|
2014-05-14 22:13:25 -07:00
|
|
|
}
|
|
|
|
var noti = window.webkitNotifications.createNotification(icon, title, content);
|
2014-06-24 08:36:32 -07:00
|
|
|
if (typeof ondisplay === 'function') {
|
2014-05-14 22:13:25 -07:00
|
|
|
noti.ondisplay = ondisplay;
|
|
|
|
}
|
2014-06-24 08:36:32 -07:00
|
|
|
if (typeof onclose === 'function') {
|
2014-05-14 22:13:25 -07:00
|
|
|
noti.onclose = onclose;
|
|
|
|
}
|
|
|
|
noti.show();
|
2014-06-24 08:36:32 -07:00
|
|
|
} else {
|
2014-05-14 22:13:25 -07:00
|
|
|
settings.html5Mode = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
|
|
/* ========== SETTINGS RELATED METHODS =============*/
|
|
|
|
|
2014-06-24 08:36:32 -07:00
|
|
|
disableHtml5Mode: function() {
|
2014-05-14 22:13:25 -07:00
|
|
|
settings.html5Mode = false;
|
|
|
|
},
|
|
|
|
|
2014-06-24 08:36:32 -07:00
|
|
|
disableType: function(notificationType) {
|
2014-05-14 22:13:25 -07:00
|
|
|
settings[notificationType].enabled = false;
|
|
|
|
},
|
|
|
|
|
2014-06-24 08:36:32 -07:00
|
|
|
enableHtml5Mode: function() {
|
2014-05-14 22:13:25 -07:00
|
|
|
// settings.html5Mode = true;
|
|
|
|
settings.html5Mode = this.requestHtml5ModePermissions();
|
|
|
|
},
|
|
|
|
|
2014-06-24 08:36:32 -07:00
|
|
|
enableType: function(notificationType) {
|
2014-05-14 22:13:25 -07:00
|
|
|
settings[notificationType].enabled = true;
|
|
|
|
},
|
|
|
|
|
2014-06-24 08:36:32 -07:00
|
|
|
getSettings: function() {
|
2014-05-14 22:13:25 -07:00
|
|
|
return settings;
|
|
|
|
},
|
|
|
|
|
2014-06-24 08:36:32 -07:00
|
|
|
toggleType: function(notificationType) {
|
2014-05-14 22:13:25 -07:00
|
|
|
settings[notificationType].enabled = !settings[notificationType].enabled;
|
|
|
|
},
|
|
|
|
|
2014-06-24 08:36:32 -07:00
|
|
|
toggleHtml5Mode: function() {
|
2014-05-14 22:13:25 -07:00
|
|
|
settings.html5Mode = !settings.html5Mode;
|
|
|
|
},
|
|
|
|
|
2014-06-24 08:36:32 -07:00
|
|
|
requestHtml5ModePermissions: function() {
|
|
|
|
if (window.webkitNotifications) {
|
2014-05-14 22:13:25 -07:00
|
|
|
if (window.webkitNotifications.checkPermission() === 0) {
|
|
|
|
return true;
|
2014-06-24 08:36:32 -07:00
|
|
|
} else {
|
|
|
|
window.webkitNotifications.requestPermission(function() {
|
|
|
|
if (window.webkitNotifications.checkPermission() === 0) {
|
2014-05-14 22:13:25 -07:00
|
|
|
settings.html5Mode = true;
|
2014-06-24 08:36:32 -07:00
|
|
|
} else {
|
2014-05-14 22:13:25 -07:00
|
|
|
settings.html5Mode = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-24 08:36:32 -07:00
|
|
|
} else {
|
2014-05-14 22:13:25 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
/* ============ QUERYING RELATED METHODS ============*/
|
|
|
|
|
2014-06-24 08:36:32 -07:00
|
|
|
getAll: function() {
|
2014-05-14 22:13:25 -07:00
|
|
|
// Returns all notifications that are currently stored
|
|
|
|
return notifications;
|
|
|
|
},
|
|
|
|
|
2014-06-24 08:36:32 -07:00
|
|
|
getQueue: function() {
|
2014-05-14 22:13:25 -07:00
|
|
|
return queue;
|
|
|
|
},
|
|
|
|
|
|
|
|
/* ============== NOTIFICATION METHODS ==============*/
|
|
|
|
|
2014-06-24 08:36:32 -07:00
|
|
|
info: function(title, content, userData) {
|
2015-03-06 07:00:10 -08:00
|
|
|
return this.awesomeNotify('info', 'fi-info', title, content, userData);
|
2014-05-14 22:13:25 -07:00
|
|
|
},
|
2014-06-24 08:36:32 -07:00
|
|
|
|
|
|
|
funds: function(title, content, userData) {
|
2015-03-06 07:00:10 -08:00
|
|
|
return this.awesomeNotify('funds', 'icon-receive', title, content, userData);
|
2014-05-20 10:31:00 -07:00
|
|
|
},
|
2014-05-14 22:13:25 -07:00
|
|
|
|
2014-06-27 11:32:52 -07:00
|
|
|
version: function(title, content, severe) {
|
2015-03-06 07:00:10 -08:00
|
|
|
return this.awesomeNotify('version', severe ? 'fi-alert' : 'fi-flag', title, content);
|
2014-06-27 11:32:52 -07:00
|
|
|
},
|
|
|
|
|
2014-06-24 08:36:32 -07:00
|
|
|
error: function(title, content, userData) {
|
2015-03-06 07:00:10 -08:00
|
|
|
return this.awesomeNotify('error', 'fi-x', title, content, userData);
|
2014-05-14 22:13:25 -07:00
|
|
|
},
|
|
|
|
|
2014-06-24 08:36:32 -07:00
|
|
|
success: function(title, content, userData) {
|
2015-03-06 07:00:10 -08:00
|
|
|
return this.awesomeNotify('success', 'fi-check', title, content, userData);
|
2014-05-14 22:13:25 -07:00
|
|
|
},
|
|
|
|
|
2014-06-24 08:36:32 -07:00
|
|
|
warning: function(title, content, userData) {
|
2015-03-06 07:00:10 -08:00
|
|
|
return this.awesomeNotify('warning', 'fi-alert', title, content, userData);
|
|
|
|
},
|
|
|
|
|
|
|
|
new: function(title, content, userData) {
|
|
|
|
return this.awesomeNotify('warning', 'fi-plus', title, content, userData);
|
|
|
|
},
|
|
|
|
|
|
|
|
sent: function(title, content, userData) {
|
|
|
|
return this.awesomeNotify('warning', 'icon-paperplane', title, content, userData);
|
2014-05-14 22:13:25 -07:00
|
|
|
},
|
|
|
|
|
2014-06-24 08:36:32 -07:00
|
|
|
awesomeNotify: function(type, icon, title, content, userData) {
|
2014-05-14 22:13:25 -07:00
|
|
|
/**
|
|
|
|
* Supposed to wrap the makeNotification method for drawing icons using font-awesome
|
|
|
|
* rather than an image.
|
|
|
|
*
|
|
|
|
* Need to find out how I'm going to make the API take either an image
|
|
|
|
* resource, or a font-awesome icon and then display either of them.
|
|
|
|
* Also should probably provide some bits of color, could do the coloring
|
|
|
|
* through classes.
|
|
|
|
*/
|
|
|
|
// image = '<i class="icon-' + image + '"></i>';
|
|
|
|
return this.makeNotification(type, false, icon, title, content, userData);
|
|
|
|
},
|
|
|
|
|
2014-06-24 08:36:32 -07:00
|
|
|
notify: function(image, title, content, userData) {
|
2014-05-14 22:13:25 -07:00
|
|
|
// Wraps the makeNotification method for displaying notifications with images
|
|
|
|
// rather than icons
|
|
|
|
return this.makeNotification('custom', image, true, title, content, userData);
|
|
|
|
},
|
|
|
|
|
2014-06-24 08:36:32 -07:00
|
|
|
makeNotification: function(type, image, icon, title, content, userData) {
|
2014-05-14 22:13:25 -07:00
|
|
|
var notification = {
|
|
|
|
'type': type,
|
|
|
|
'image': image,
|
|
|
|
'icon': icon,
|
|
|
|
'title': title,
|
|
|
|
'content': content,
|
|
|
|
'timestamp': +new Date(),
|
|
|
|
'userData': userData
|
|
|
|
};
|
2014-10-28 14:06:32 -07:00
|
|
|
|
2014-05-14 22:13:25 -07:00
|
|
|
notifications.push(notification);
|
|
|
|
|
2014-06-24 08:36:32 -07:00
|
|
|
if (settings.html5Mode) {
|
|
|
|
html5Notify(image, title, content, function() {
|
2014-06-03 14:38:56 -07:00
|
|
|
// inner on display function
|
2014-06-24 08:36:32 -07:00
|
|
|
}, function() {
|
2014-06-03 14:38:56 -07:00
|
|
|
// inner on close function
|
2014-05-14 22:13:25 -07:00
|
|
|
});
|
2014-10-28 14:06:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//this is done because html5Notify() changes the variable settings.html5Mode
|
|
|
|
if (!settings.html5Mode) {
|
2014-05-14 22:13:25 -07:00
|
|
|
queue.push(notification);
|
2014-06-24 08:36:32 -07:00
|
|
|
$timeout(function removeFromQueueTimeout() {
|
2014-05-14 22:13:25 -07:00
|
|
|
queue.splice(queue.indexOf(notification), 1);
|
|
|
|
}, settings[type].duration);
|
2014-07-17 12:52:32 -07:00
|
|
|
}
|
2014-05-14 22:13:25 -07:00
|
|
|
|
2014-08-19 14:41:55 -07:00
|
|
|
// Mobile notification
|
|
|
|
if (window && window.navigator && window.navigator.vibrate) {
|
2014-10-28 14:06:32 -07:00
|
|
|
window.navigator.vibrate([200, 100, 200]);
|
2014-08-19 14:41:55 -07:00
|
|
|
};
|
|
|
|
|
2016-05-24 07:18:06 -07:00
|
|
|
if (document.hidden && (type == 'info' || type == 'funds') && !isCordova) {
|
2014-10-28 14:06:32 -07:00
|
|
|
new window.Notification(title, {
|
|
|
|
body: content,
|
|
|
|
icon: 'img/notification.png'
|
|
|
|
});
|
2014-05-14 22:13:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
this.save();
|
|
|
|
return notification;
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
/* ============ PERSISTENCE METHODS ============ */
|
|
|
|
|
2014-06-24 08:36:32 -07:00
|
|
|
save: function() {
|
2014-05-14 22:13:25 -07:00
|
|
|
// Save all the notifications into localStorage
|
2014-06-24 08:36:32 -07:00
|
|
|
if (settings.localStorage) {
|
2014-06-18 09:01:50 -07:00
|
|
|
localStorage.setItem('notifications', JSON.stringify(notifications));
|
2014-05-14 22:13:25 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-06-24 08:36:32 -07:00
|
|
|
restore: function() {
|
2014-05-14 22:13:25 -07:00
|
|
|
// Load all notifications from localStorage
|
|
|
|
},
|
|
|
|
|
2014-06-24 08:36:32 -07:00
|
|
|
clear: function() {
|
2014-05-14 22:13:25 -07:00
|
|
|
notifications = [];
|
|
|
|
this.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
2014-06-24 08:36:32 -07:00
|
|
|
}
|
2016-08-23 08:10:48 -07:00
|
|
|
);
|