1const jwt = require("jsonwebtoken");
2const http2 = require("http2");
3const fs = require("fs");
4
5const token = jwt.sign(
6  {
7    iss: "APPLE-TEAM-ID"
8    iat: Math.round(new Date().getTime() / 1000),
9  },
10  fs.readFileSync("./myapp_apns_key.p8", "utf8"),
11  {
12    header: {
13      alg: "ES256",
14      kid: "P8-KEY-ID",
15    },
16  }
17);
18
19const client = http2.connect(
20  IS_PRODUCTION ? 'https://api.push.apple.com' : 'https://api.sandbox.push.apple.com'
21);
22
23const deviceToken = "device token grabbed cient-side";
24
25headers = {
26  ":method": "POST",
27  ":scheme": "https",
28  "apns-topic": "YOUR-BUNDLE-IDENTIFIER", //your application bundle ID
29  ":path": "/3/device/" + deviceToken,
30  authorization: `bearer ${token}`,
31};
32
33const request = client.request(headers);
34
35request.setEncoding("utf8");
36
37request.write(
38  JSON.stringify({
39    aps: {
40      alert: {
41        title: "\uD83D\uDCE7 You've got mail!",
42        body: "Hello world! \uD83C\uDF10",
43      },
44    },
45  })
46);
47
48request.on("response", (headers, flags) => {
49  for (const name in headers) {
50    console.log(`${name}: ${headers[name]}`);
51  }
52});
53
54let data = "";
55request.on("data", (chunk) => {
56  data += chunk;
57});
58
59request.on("end", () => {
60  console.log(`\n${data}`);
61  client.close();
62});
63
64request.end();