-
Notifications
You must be signed in to change notification settings - Fork 690
/
Copy pathindex.js
109 lines (89 loc) · 2.49 KB
/
index.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
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
"use strict";
/**
* Create a notification
* @constructor
*/
function Notification (payload) {
this.encoding = "utf8";
this.payload = {};
this.compiled = false;
this.aps = {};
this.expiry = -1;
this.priority = 10;
if (payload) {
for(let key in payload) {
if (payload.hasOwnProperty(key)) {
this[key] = payload[key];
}
}
}
}
Notification.prototype = require("./apsProperties");
// Create setter methods for properties
["payload", "expiry", "priority", "alert", "body", "locKey",
"locArgs", "title", "subtitle", "titleLocKey", "titleLocArgs", "action",
"actionLocKey", "launchImage", "badge", "sound", "contentAvailable",
"mutableContent", "mdm", "urlArgs", "category", "threadId"].forEach( propName => {
const methodName = "set" + propName[0].toUpperCase() + propName.slice(1);
Notification.prototype[methodName] = function (value) {
this[propName] = value;
return this;
};
});
Notification.prototype.headers = function headers() {
let headers = {};
if (this.priority !== 10) {
headers["apns-priority"] = this.priority;
}
if (this.id) {
headers["apns-id"] = this.id;
}
if (this.expiry >= 0) {
headers["apns-expiration"] = this.expiry;
}
if (this.topic) {
headers["apns-topic"] = this.topic;
}
if (this.collapseId) {
headers["apns-collapse-id"] = this.collapseId;
}
if (this.pushType) {
headers["apns-push-type"] = this.pushType;
}
return headers;
};
/**
* Compile a notification down to its JSON format. Compilation is final, changes made to the notification after this method is called will not be reflected in further calls.
* @returns {String} JSON payload for the notification.
* @since v1.3.0
*/
Notification.prototype.compile = function () {
if(!this.compiled) {
this.compiled = JSON.stringify(this);
}
return this.compiled;
};
/**
* @returns {Number} Byte length of the notification payload
* @since v1.2.0
*/
Notification.prototype.length = function () {
return Buffer.byteLength(this.compile(), this.encoding || "utf8");
};
/**
* @private
*/
Notification.prototype.apsPayload = function() {
var aps = this.aps;
return Object.keys(aps).find( key => aps[key] !== undefined ) ? aps : undefined;
};
Notification.prototype.toJSON = function () {
if (this.rawPayload != null) {
return this.rawPayload;
}
if (typeof this._mdm === "string") {
return { "mdm": this._mdm };
}
return Object.assign({}, this.payload, {aps: this.apsPayload()});
};
module.exports = Notification;