@@ -28,6 +28,7 @@ import { NotificationReceived, NotificationClicked } from "../models/Notificatio
2828import { cancelableTimeout } from "../helpers/sw/CancelableTimeout" ;
2929import { DeviceRecord } from '../models/DeviceRecord' ;
3030import { awaitableTimeout } from "../utils/AwaitableTimeout" ;
31+ import { AppConfig } from "src/models/AppConfig" ;
3132
3233declare 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,30 @@ 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+ if ( ! dbDefaultNotificationUrl ) {
776+ await Database . setAppState ( { defaultNotificationUrl : defaultNotificationUrlFromConfig } ) ;
777+ }
778+ }
779+ } catch ( e ) {
780+ Log . error ( "Failed to update notification in the database" , e ) ;
781+ }
782+ }
755783
756784 // If the user clicked an action button, use the URL provided by the action button
757785 // Unless the action button URL is null
0 commit comments