From a6138b37cb4274c1d3c9fff9d45ef641aaf48261 Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Mon, 10 Feb 2025 10:32:46 -0800 Subject: [PATCH] use seconds instead of ms for claims payload --- src/services/NotificationService.ts | 41 ++++++++++++++++------------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/src/services/NotificationService.ts b/src/services/NotificationService.ts index 66ec81d..46d9e68 100644 --- a/src/services/NotificationService.ts +++ b/src/services/NotificationService.ts @@ -54,17 +54,17 @@ export class NotificationService { "kid": keyId, }; - const now = Date.now(); + const nowMs = Date.now(); const claimsPayload = { "iss": teamId, - "iat": now, + "iat": Math.ceil(nowMs / 1000), // APNs requires number of seconds since Epoch }; this.apnsToken = jwt.sign(claimsPayload, privateKey, { algorithm: "ES256", header: tokenHeader }); - this._lastRefreshedTimeMs = now; + this._lastRefreshedTimeMs = nowMs; } private lastReloadedTimeForAPNsIsTooRecent() { @@ -109,25 +109,30 @@ export class NotificationService { "apns-priority": "10", "apns-topic": bundleId, }; - 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.` + try { + 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(); + }), + }); + const json = await response.json(); - if (response.status !== 200) { - console.error(`Notification failed for device ${deviceId}:`, json.reason); + if (response.status !== 200) { + console.error(`Notification failed for device ${deviceId}:`, json.reason); + return false; + } + return true; + } catch(e) { + console.error(e); return false; } - return true; } public static getAPNsFullUrlToUse(deviceId: string) {