forked from zetavg/react-native-system-notification
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
116 lines (97 loc) · 2.62 KB
/
index.js
File metadata and controls
116 lines (97 loc) · 2.62 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
* React Native Progress Notification
*/
import { NativeModules } from 'react-native';
const { NotificationModule } = NativeModules;
const RNPN = {
create(attrs = {}) {
return new Promise((resolve, reject) => {
NotificationModule.rnGetAppName((e) => {}, (appName) => {
// attrs.subject = appName;
attrs.appName = appName
attrs = encNativeNotification(attrs);
NotificationModule.rnSend(attrs.id, attrs, reject, (notification) => {
resolve(decNativeNotification(notification));
});
});
});
},
delete(id) {
return new Promise((resolve, reject) => {
NotificationModule.rnDelete(id, reject, (notification) => {
resolve(decNativeNotification(notification));
});
});
}
}
const methodsDecEnc = {
payload: (val, func) => {
if (typeof val === 'string') {
return val;
}
else if (func === 'dec') {
if (!val) {
return {};
} else {
return JSON.parse(val);
}
}
else {
if (!val) {
val = {};
}
return JSON.stringify(val);
}
},
smallIcon: val => !val ? 'ic_launcher' : val,
id: val => !val ? parseInt(Math.random() * 100000) : val,
action: val => !val ? 'DEFAULT' : val,
autoClear: val => !val ? true : val,
priority: val => val === undefined ? 1 : val,
sound: val => val === undefined ? 'default' : val,
vibrate: val => val === undefined ? 'default' : val,
lights: val => val === undefined ? 'default' : val,
isOngoing: val => val === true,
showAppName: val => val === true,
};
function normilizeAttrsLoop(attrs, func = 'enc') {
for (let key in attrs) {
if (methodsDecEnc[key]) {
attrs[key] = methodsDecEnc[key](attrs[key], func);
}
}
if (func === 'enc') {
// Set default values
for (let key in methodsDecEnc) {
attrs[key] = methodsDecEnc[key](attrs[key], func);
}
}
return attrs;
}
function decNativeNotification(attrs) {
attrs = normilizeAttrsLoop(attrs, 'dec');
if (attrs.progress) {
attrs.progress = attrs.progress / 1000;
}
return attrs;
}
function encNativeNotification(attrs) {
if (typeof attrs === 'string') {
attrs = JSON.parse(attrs);
}
attrs = normilizeAttrsLoop(attrs, 'enc');
if (attrs.tickerText === undefined) {
if (attrs.subject) {
attrs.tickerText = attrs.subject + ': ' + attrs.message;
} else {
attrs.tickerText = attrs.message;
}
}
attrs.delayed = (attrs.delay !== undefined);
attrs.scheduled = (attrs.schedule !== undefined);
if (attrs.progress) {
attrs.progress = attrs.progress * 1000;
}
return attrs;
}
module.exports = RNPN;