-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpushNotification.js
More file actions
105 lines (86 loc) · 2.71 KB
/
pushNotification.js
File metadata and controls
105 lines (86 loc) · 2.71 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
const fs = require("fs");
const http2 = require("http2");
const jwt = require("jsonwebtoken");
async function sendApplePushNotification(deviceToken, payload = {}) {
const tokenData = {
header: {
alg: "ES256",
kid: process.env.APPLE_KEY_ID, // Key ID
},
payload: {
iss: process.env.APPLE_TEAM_ID, // Issuer key
iat: Math.floor(Date.now() / 1000), // Issued at time (EPOCH)
},
};
const privateKey = fs.readFileSync("./.certificates/AuthKey_ABC123DEF1.p8");
const options = {
cert: fs.readFileSync("./.certificates/signerCert.pem"),
key: fs.readFileSync("./.certificates/signerKey.pem"),
ca: fs.readFileSync("./.certificates/wwdr.pem"),
passphrase: process.env.PASS_TYPE_IDENTIFIER,
};
const encodedToken = jwt.sign(tokenData.payload, privateKey, {
algorithm: "ES256",
header: tokenData.header,
});
const headers = {
":method": "POST",
":path": `/3/device/${deviceToken}`,
authorization: `bearer ${encodedToken}`,
"apns-push-type": "background",
"apns-expiration": "0",
"apns-priority": "5",
"apns-topic": process.env.APPLE_TOPIC, // the topic must be the same as you used on `passTypeIdentifier` to create the pass
"Content-Type": "application/json",
};
const client = http2.connect("https://api.push.apple.com");
client.on("error", (error) => {
console.error("Client connection error:", error);
});
const connectPromise = new Promise((resolve, reject) => {
client.on("connect", () => {
resolve();
});
client.on("error", (error) => {
reject(error);
});
});
await connectPromise;
const req = client.request(headers, options);
req.setEncoding("utf8");
req.write(JSON.stringify(payload)); // Empty payload as necessary, see apple documentation.
req.end();
const responsePromise = new Promise((resolve, reject) => {
req.on("response", (responseHeaders) => {
resolve();
});
req.on("data", (chunk) => {});
req.on("end", () => {
client.close();
});
req.on("error", (error) => {
console.error("Request error:", error);
reject(error);
});
req.setTimeout(10000, () => {
console.error("Request timed out");
req.close();
reject(new Error("Request timed out"));
});
req.on("socket", (socket) => {
socket.on("timeout", () => {
console.error("Socket timeout");
req.close();
reject(new Error("Socket timeout"));
});
socket.on("error", (error) => {
console.error("Socket error:", error);
reject(error);
});
socket.on("close", () => {});
});
});
await responsePromise;
client.on("close", () => {});
}
module.exports = { sendApplePushNotification };