mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
extract code to open and close the apns connection
This commit is contained in:
@@ -86,7 +86,9 @@ export class AppleNotificationSender {
|
|||||||
throw new Error("APNS_BUNDLE_ID environment variable is not set correctly");
|
throw new Error("APNS_BUNDLE_ID environment variable is not set correctly");
|
||||||
}
|
}
|
||||||
|
|
||||||
const { path, host } = AppleNotificationSender.getAPNsFullUrlToUse(deviceId);
|
this.openConnectionIfNoneExists();
|
||||||
|
|
||||||
|
const { path } = AppleNotificationSender.getAPNsFullUrlToUse(deviceId);
|
||||||
|
|
||||||
const headers = {
|
const headers = {
|
||||||
':method': 'POST',
|
':method': 'POST',
|
||||||
@@ -98,9 +100,7 @@ export class AppleNotificationSender {
|
|||||||
"apns-topic": bundleId,
|
"apns-topic": bundleId,
|
||||||
};
|
};
|
||||||
try {
|
try {
|
||||||
if (!this.client) {
|
if (!this.client) { return false }
|
||||||
this.client = http2.connect(host);
|
|
||||||
}
|
|
||||||
const client = this.client;
|
const client = this.client;
|
||||||
const req = client.request(headers);
|
const req = client.request(headers);
|
||||||
req.setEncoding('utf8');
|
req.setEncoding('utf8');
|
||||||
@@ -137,15 +137,21 @@ export class AppleNotificationSender {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static getAPNsFullUrlToUse(deviceId: string): APNsUrl {
|
private openConnectionIfNoneExists() {
|
||||||
// Construct the fetch request
|
const host = AppleNotificationSender.getAPNsHostToUse();
|
||||||
const devBaseUrl = "https://api.development.push.apple.com"
|
|
||||||
const prodBaseUrl = "https://api.push.apple.com"
|
|
||||||
|
|
||||||
let hostToUse = devBaseUrl;
|
if (!this.client) {
|
||||||
if (process.env.APNS_IS_PRODUCTION === "1") {
|
this.client = http2.connect(host);
|
||||||
hostToUse = prodBaseUrl;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private closeConnectionIfExists() {
|
||||||
|
this.client?.close();
|
||||||
|
this.client = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static getAPNsFullUrlToUse(deviceId: string): APNsUrl {
|
||||||
|
let hostToUse = this.getAPNsHostToUse();
|
||||||
|
|
||||||
const path = "/3/device/" + deviceId;
|
const path = "/3/device/" + deviceId;
|
||||||
const fullUrl = hostToUse + path;
|
const fullUrl = hostToUse + path;
|
||||||
@@ -157,4 +163,15 @@ export class AppleNotificationSender {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static getAPNsHostToUse() {
|
||||||
|
// Construct the fetch request
|
||||||
|
const devBaseUrl = "https://api.development.push.apple.com"
|
||||||
|
const prodBaseUrl = "https://api.push.apple.com"
|
||||||
|
|
||||||
|
let hostToUse = devBaseUrl;
|
||||||
|
if (process.env.APNS_IS_PRODUCTION === "1") {
|
||||||
|
hostToUse = prodBaseUrl;
|
||||||
|
}
|
||||||
|
return hostToUse;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user