Merge pull request #43 from brendan-ch/feat/shuttle-id-notification-payload

[INT-53] feat/shuttle-id-notification-payload
This commit is contained in:
2025-04-22 11:20:18 -07:00
committed by GitHub
5 changed files with 27 additions and 8 deletions

View File

@@ -71,6 +71,7 @@ export class InterchangeSystem {
shuttleRepository, shuttleRepository,
notificationRepository, notificationRepository,
new AppleNotificationSender(), new AppleNotificationSender(),
args.id,
); );
notificationScheduler.startListeningForUpdates(); notificationScheduler.startListeningForUpdates();
@@ -115,6 +116,7 @@ export class InterchangeSystem {
shuttleRepository, shuttleRepository,
notificationRepository, notificationRepository,
new AppleNotificationSender(false), new AppleNotificationSender(false),
args.id,
); );
notificationScheduler.startListeningForUpdates(); notificationScheduler.startListeningForUpdates();

View File

@@ -13,7 +13,8 @@ export class ETANotificationScheduler {
constructor( constructor(
private shuttleRepository: ShuttleGetterRepository, private shuttleRepository: ShuttleGetterRepository,
private notificationRepository: NotificationRepository = new InMemoryNotificationRepository(), private notificationRepository: NotificationRepository = new InMemoryNotificationRepository(),
private appleNotificationSender = new AppleNotificationSender() private appleNotificationSender = new AppleNotificationSender(),
private interchangeSystemId: string,
) { ) {
this.etaSubscriberCallback = this.etaSubscriberCallback.bind(this); this.etaSubscriberCallback = this.etaSubscriberCallback.bind(this);
this.sendEtaNotificationImmediately = this.sendEtaNotificationImmediately.bind(this); this.sendEtaNotificationImmediately = this.sendEtaNotificationImmediately.bind(this);
@@ -45,6 +46,11 @@ export class ETANotificationScheduler {
const notificationAlertArguments: NotificationAlertArguments = { const notificationAlertArguments: NotificationAlertArguments = {
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.`,
customKeys: {
shuttleId,
stopId,
systemId: this.interchangeSystemId,
},
} }
return this.appleNotificationSender.sendNotificationImmediately(deviceId, notificationAlertArguments); return this.appleNotificationSender.sendNotificationImmediately(deviceId, notificationAlertArguments);
} }

View File

@@ -10,6 +10,7 @@ interface APNsUrl {
export interface NotificationAlertArguments { export interface NotificationAlertArguments {
title: string; title: string;
body: string; body: string;
customKeys?: any,
} }
export class AppleNotificationSender { export class AppleNotificationSender {
@@ -106,12 +107,21 @@ export class AppleNotificationSender {
resolve(); resolve();
}); });
req.write(JSON.stringify({ const customKeys = {
...notificationAlertArguments.customKeys,
}
delete notificationAlertArguments.customKeys;
// See https://developer.apple.com/documentation/usernotifications/generating-a-remote-notification
// for notification payload examples
const payload = JSON.stringify({
aps: { aps: {
alert: notificationAlertArguments, alert: notificationAlertArguments,
sound: "default" sound: "default"
} },
})); customKeys,
});
req.write(payload);
req.end(); req.end();
}); });
return true; return true;

View File

@@ -40,7 +40,8 @@ describe("ETANotificationScheduler", () => {
notificationService = new ETANotificationScheduler( notificationService = new ETANotificationScheduler(
shuttleRepository, shuttleRepository,
notificationRepository, notificationRepository,
appleNotificationSender appleNotificationSender,
"1",
); );
notificationService.startListeningForUpdates(); notificationService.startListeningForUpdates();
}); });
@@ -127,7 +128,8 @@ describe("ETANotificationScheduler", () => {
notificationService = new ETANotificationScheduler( notificationService = new ETANotificationScheduler(
shuttleRepository, shuttleRepository,
notificationRepository, notificationRepository,
updatedNotificationSender updatedNotificationSender,
"1",
); );
notificationService.startListeningForUpdates(); notificationService.startListeningForUpdates();

View File

@@ -5,7 +5,6 @@ import {
AppleNotificationSender, AppleNotificationSender,
NotificationAlertArguments NotificationAlertArguments
} from "../../../src/notifications/senders/AppleNotificationSender"; } from "../../../src/notifications/senders/AppleNotificationSender";
import { ETANotificationScheduler } from "../../../src/notifications/schedulers/ETANotificationScheduler";
jest.mock("http2"); jest.mock("http2");
@@ -13,7 +12,7 @@ const sampleKeyBase64 = "LS0tLS1CRUdJTiBQUklWQVRFIEtFWS0tLS0tCk1JR1RBZ0VBTUJNR0J
function mockHttp2Connect(status: number) { function mockHttp2Connect(status: number) {
class MockClient extends EventEmitter { class MockClient extends EventEmitter {
request = jest.fn((headers: any) => { request = jest.fn((_) => {
const mockRequest: any = new EventEmitter(); const mockRequest: any = new EventEmitter();
mockRequest.setEncoding = jest.fn(); mockRequest.setEncoding = jest.fn();
mockRequest.write = jest.fn(); mockRequest.write = jest.fn();