forked from expo/fyi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendNotificationToAPNS.js
64 lines (53 loc) · 1.22 KB
/
sendNotificationToAPNS.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
const jwt = require("jsonwebtoken");
const http2 = require("http2");
const fs = require("fs");
const token = jwt.sign(
{
iss: "APPLE-TEAM-ID"
iat: Math.round(new Date().getTime() / 1000),
},
fs.readFileSync("./myapp_apns_key.p8", "utf8"),
{
header: {
alg: "ES256",
kid: "P8-KEY-ID",
},
}
);
const client = http2.connect(
IS_PRODUCTION ? 'https://api.push.apple.com' : 'https://api.sandbox.push.apple.com'
);
const deviceToken = "device token grabbed cient-side";
headers = {
":method": "POST",
":scheme": "https",
"apns-topic": "YOUR-BUNDLE-IDENTIFIER", //your application bundle ID
":path": "/3/device/" + deviceToken,
authorization: `bearer ${token}`,
};
const request = client.request(headers);
request.setEncoding("utf8");
request.write(
JSON.stringify({
aps: {
alert: {
title: "\uD83D\uDCE7 You've got mail!",
body: "Hello world! \uD83C\uDF10",
},
},
})
);
request.on("response", (headers, flags) => {
for (const name in headers) {
console.log(`${name}: ${headers[name]}`);
}
});
let data = "";
request.on("data", (chunk) => {
data += chunk;
});
request.on("end", () => {
console.log(`\n${data}`);
client.close();
});
request.end();