forked from microsoft/react-native-winrt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotifications.js
56 lines (48 loc) · 1.61 KB
/
Notifications.js
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
const Notifications = Windows.UI.Notifications;
const ToastTemplateType = Notifications.ToastTemplateType;
const ToastNotificationManager = Notifications.ToastNotificationManager;
const ToastNotification = Notifications.ToastNotification;
export function showNotification(notification) {
var type = ToastTemplateType.toastText01;
var obj = {};
if (typeof(notification) == 'string') {
obj['text'] = notification;
} else {
obj = notification;
}
if (obj.template != undefined) {
type = obj.template;
}
var xml = ToastNotificationManager.getTemplateContent(type);
for (var tagName in obj) {
var xmlElements = xml.getElementsByTagName(tagName);
var value = obj[tagName];
if (typeof(value) == 'string') {
fillXmlElements(xml, xmlElements, [value]);
} else if (Array.isArray(value)) {
fillXmlElements(xml, xmlElements, value);
} else if (typeof(value) == 'object') {
fillXmlElements(xml, xmlElements, [value]);
}
}
var toast = new ToastNotification(xml);
ToastNotificationManager.createToastNotifier().show(toast);
}
function fillXmlElements(xml, xmlElements, arr) {
var i = 0;
for (var arrValue of arr) {
var node = xmlElements[i++];
if (typeof(arrValue) == 'string') {
node.appendChild(xml.createTextNode(arrValue));
} else if (typeof(arrValue) == 'object') {
for (var attrName in arrValue) {
var attr = node.attributes.getNamedItem(attrName);
if (!attr) {
attr = xml.createAttribute(attrName);
node.attributes.setNamedItem(attr);
}
attr.nodeValue = arrValue[attrName];
}
}
}
}