-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotification.js
More file actions
83 lines (67 loc) · 2.19 KB
/
notification.js
File metadata and controls
83 lines (67 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
'use strict';
class Notificacao {
constructor(options = {}) {
let defaults = {
titulo: "Titulo Padrão",
body: "Mensagem Padrão",
icon: "https://pbs.twimg.com/profile_images/686359431664238592/VEc23RGt.png",
status: 1,
action: ""
}
let settings = Object.assign({}, defaults, options);
this.settings = settings;
this.definirStatus(settings.status);
this.pedirPermissao();
}
getAttr(){
console.log(this.settings);
}
definirStatus(status) {
if (this.settings.icon === ""){
switch (status){
case 1:
this.settings.icon = "/static/admin/plugins/notificacao/images/ICON_CHECK.png";
break;
case 2:
this.settings.icon = "/static/admin/plugins/notificacao/images/ICON_ERROR.png";
default:
this.settings.icon = "/static/admin/plugins/notificacao/images/ICON_CHECK.png";
}
}
}
pedirPermissao(){
if (Notification.permission !== "granted"){
Notification.requestPermission();
}
}
verificaPermissao(){
if (!Notification){
alert('Não é possivel utilizar notificações no seu navegador. Tente Google Chrome.');
return false;
}
if (Notification.permission !== "granted"){
Notification.requestPermission();
return false;
}else{
return true;
}
}
callAction(notification){
if (this.settings.action){
notification.onclick = function () {
window.open(this.settings.action);
}
}
}
notificar(body="", icon=""){
if (this.verificaPermissao()) {
let notification = new Notification(this.settings.titulo, {
icon: (icon ? icon : this.settings.icon),
body: (body ? body : this.settings.body),
});
this.callAction(notification);
}else{
console.log('Permissões não foram ativadas')
}
}
}