use node.js http2 module instead of fetch

This commit is contained in:
2025-02-10 13:21:48 -08:00
parent 2d92370254
commit da224c36ed

View File

@@ -3,6 +3,7 @@ import jwt from "jsonwebtoken";
import fs from "fs";
import { TupleKey } from "../types/TupleKey";
import { IEta } from "../entities/entities";
import * as http2 from "node:http2";
export interface ScheduledNotificationData {
deviceId: string;
@@ -103,31 +104,57 @@ export class NotificationService {
}
const headers = {
authorization: `bearer ${this.apnsToken}`,
':method': 'POST',
':path': `/3/device/${deviceId}`,
'authorization': `bearer ${this.apnsToken}`,
"apns-push-type": "alert",
"apns-expiration": "0",
"apns-priority": "10",
"apns-topic": bundleId,
};
try {
const response = await fetch(url, {
method: "POST",
headers,
body: JSON.stringify({
const client = http2.connect('https://api.development.push.apple.com');
const req = client.request(headers);
req.setEncoding('utf8');
await new Promise<void>((resolve, reject) => {
req.on('response', (headers, flags) => {
if (headers[":status"] !== 200) {
console.error(`APNs request failed with status ${headers[":status"]}`);
reject();
}
resolve();
});
req.write(JSON.stringify({
aps: {
alert: {
title: "Shuttle is arriving",
body: `Shuttle is approaching ${stop.name} in ${Math.ceil(eta.secondsRemaining / 60)} minutes.`
}
}
}),
}));
req.end();
});
const json = await response.json();
if (response.status !== 200) {
console.error(`Notification failed for device ${deviceId}:`, json.reason);
return false;
}
// const response = await fetch(url, {
// method: "POST",
// headers,
// body: JSON.stringify({
// aps: {
// alert: {
// title: "Shuttle is arriving",
// body: `Shuttle is approaching ${stop.name} in ${Math.ceil(eta.secondsRemaining / 60)} minutes.`
// }
// }
// }),
// });
// const json = await response.json();
// if (response.status !== 200) {
// console.error(`Notification failed for device ${deviceId}:`, json.reason);
// return false;
// }
return true;
} catch(e) {
console.error(e);