mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 16:00:32 +00:00
use node.js http2 module instead of fetch
This commit is contained in:
@@ -3,6 +3,7 @@ import jwt from "jsonwebtoken";
|
|||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
import { TupleKey } from "../types/TupleKey";
|
import { TupleKey } from "../types/TupleKey";
|
||||||
import { IEta } from "../entities/entities";
|
import { IEta } from "../entities/entities";
|
||||||
|
import * as http2 from "node:http2";
|
||||||
|
|
||||||
export interface ScheduledNotificationData {
|
export interface ScheduledNotificationData {
|
||||||
deviceId: string;
|
deviceId: string;
|
||||||
@@ -103,31 +104,57 @@ export class NotificationService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const headers = {
|
const headers = {
|
||||||
authorization: `bearer ${this.apnsToken}`,
|
':method': 'POST',
|
||||||
|
':path': `/3/device/${deviceId}`,
|
||||||
|
'authorization': `bearer ${this.apnsToken}`,
|
||||||
"apns-push-type": "alert",
|
"apns-push-type": "alert",
|
||||||
"apns-expiration": "0",
|
"apns-expiration": "0",
|
||||||
"apns-priority": "10",
|
"apns-priority": "10",
|
||||||
"apns-topic": bundleId,
|
"apns-topic": bundleId,
|
||||||
};
|
};
|
||||||
try {
|
try {
|
||||||
const response = await fetch(url, {
|
const client = http2.connect('https://api.development.push.apple.com');
|
||||||
method: "POST",
|
const req = client.request(headers);
|
||||||
headers,
|
req.setEncoding('utf8');
|
||||||
body: JSON.stringify({
|
|
||||||
|
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: {
|
aps: {
|
||||||
alert: {
|
alert: {
|
||||||
title: "Shuttle is arriving",
|
title: "Shuttle is arriving",
|
||||||
body: `Shuttle is approaching ${stop.name} in ${Math.ceil(eta.secondsRemaining / 60)} minutes.`
|
body: `Shuttle is approaching ${stop.name} in ${Math.ceil(eta.secondsRemaining / 60)} minutes.`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}),
|
}));
|
||||||
|
req.end();
|
||||||
});
|
});
|
||||||
const json = await response.json();
|
|
||||||
|
|
||||||
if (response.status !== 200) {
|
// const response = await fetch(url, {
|
||||||
console.error(`Notification failed for device ${deviceId}:`, json.reason);
|
// method: "POST",
|
||||||
return false;
|
// 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;
|
return true;
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
|
|||||||
Reference in New Issue
Block a user