Skip to content

Commit 976d696

Browse files
committed
Add an api call for web config if no default url
There was an issue with legacy HTTP integrations where the defaultNotificationUrl was never saved in the DB. To account for this, we try to get the default URL from the app config on notification click and save it to the DB for the future use. Choosing between notification received and clicked to add this logic, decided on notification clicked to avoid doing extra api call when the user may never click the notification.
1 parent 71d5b29 commit 976d696

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

src/service-worker/ServiceWorker.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { NotificationReceived, NotificationClicked } from "../models/Notificatio
2828
import { cancelableTimeout } from "../helpers/sw/CancelableTimeout";
2929
import { DeviceRecord } from '../models/DeviceRecord';
3030
import { awaitableTimeout } from "../utils/AwaitableTimeout";
31+
import { AppConfig } from "src/models/AppConfig";
3132

3233
declare var self: ServiceWorkerGlobalScope & OSServiceWorkerFields;
3334

@@ -156,6 +157,11 @@ export class ServiceWorker {
156157
return appId;
157158
}
158159

160+
static async getAppConfig(): Promise<AppConfig> {
161+
const appId = await ServiceWorker.getAppId();
162+
return await ConfigHelper.getAppConfig({ appId }, OneSignalApiSW.downloadServerAppConfig);
163+
}
164+
159165
static setupMessageListeners() {
160166
ServiceWorker.workerMessenger.on(WorkerMessengerCommand.WorkerVersion, _ => {
161167
Log.debug('[Service Worker] Received worker version message.');
@@ -750,8 +756,33 @@ export class ServiceWorker {
750756

751757
// Use the user-provided default URL if one exists
752758
const { defaultNotificationUrl: dbDefaultNotificationUrl } = await Database.getAppState();
753-
if (dbDefaultNotificationUrl)
759+
if (dbDefaultNotificationUrl) {
754760
launchUrl = dbDefaultNotificationUrl;
761+
}
762+
else {
763+
// There was an issue with legacy HTTP integrations where the defaultNotificationUrl
764+
// was never saved to the DB. To account for this, we try to get the default URL
765+
// from the app config on notification click and save it to the DB for future use.
766+
// Choosing between notification received and notification clicked to do this logic,
767+
// we decided on notification clicked to avoid doing extra api call when the user
768+
// may never click the notification.
769+
try {
770+
const config = await ServiceWorker.getAppConfig();
771+
const defaultNotificationUrlFromConfig = config.origin;
772+
if (defaultNotificationUrlFromConfig) {
773+
launchUrl = defaultNotificationUrlFromConfig;
774+
}
775+
} catch (e) {
776+
Log.error("Failed to get app config to determine default notification url", e);
777+
}
778+
779+
if (!!launchUrl && !dbDefaultNotificationUrl) {
780+
// intentionally not awaiting this promise to not block notification click handling
781+
Database.setAppState({ defaultNotificationUrl: launchUrl }).catch(e => {
782+
Log.error("Failed to save default notification url to db", e);
783+
});
784+
}
785+
}
755786

756787
// If the user clicked an action button, use the URL provided by the action button
757788
// Unless the action button URL is null

0 commit comments

Comments
 (0)