Skip to content

Commit

Permalink
resolved comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitniyerclevertap committed Jan 28, 2025
1 parent 3a01657 commit c35a46e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
10 changes: 9 additions & 1 deletion src/modules/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ export default class NotificationHandler extends Array {
}

push (...displayArgs) {
/*
To handle a potential race condition, two flags are stored in Local Storage:
- `webPushConfigResponseReceived`: Indicates if the backend's webPushConfig has been received (set during the initial API call without a session ID).
- `notificationPushCalled`: Tracks if `clevertap.notifications.push` was called before receiving the webPushConfig.
This ensures the soft prompt is rendered correctly:
- If `webPushConfigResponseReceived` is true, the soft prompt is processed immediately.
- Otherwise, `notificationPushCalled` is set to true, and the rendering is deferred until the webPushConfig is received.
*/
const isWebPushConfigPresent = StorageManager.readFromLSorCookie('webPushConfigResponseReceived')
setNotificationHandlerValues({
logger: this.#logger,
Expand Down Expand Up @@ -184,7 +193,6 @@ export default class NotificationHandler extends Array {
}
}).catch((error) => {
// unsubscribe from webpush if error
console.log('rejected subscription')
serviceWorkerRegistration.pushManager.getSubscription().then((subscription) => {
if (subscription !== null) {
subscription.unsubscribe().then((successful) => {
Expand Down
14 changes: 10 additions & 4 deletions src/modules/webPushPrompt/prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,15 @@ export const processWebPushConfig = (webPushConfig, logger, request) => {
enablePush(logger, null, request)
} else if (JSON.stringify(_pushConfig) !== JSON.stringify(webPushConfig)) {
updatePushConfig()
StorageManager.saveToLSorCookie('webPushConfigResponseReceived', true)
const isNotificationPushCalled = StorageManager.readFromLSorCookie('notificationPushCalled')
if (isNotificationPushCalled) {
try {
StorageManager.saveToLSorCookie('webPushConfigResponseReceived', true)
const isNotificationPushCalled = StorageManager.readFromLSorCookie('notificationPushCalled')
if (isNotificationPushCalled) {
processSoftPrompt()
}
} catch (error) {
logger?.error('Failed to process web push config:', error)
// Fallback: Attempt to process soft prompt anyway
processSoftPrompt()
}
}
Expand All @@ -47,7 +53,7 @@ export const processSoftPrompt = () => {
if (!(Object.keys(webPushConfig).length > 0)) {
notificationHandler.setApplicationServerKey(appServerKey)
notificationHandler.setupWebPush(displayArgs)
return 0
return
}
const { showBox, showBellIcon, boxType } = webPushConfig
const { serviceWorkerPath, skipDialog } = parseDisplayArgs(displayArgs)
Expand Down

0 comments on commit c35a46e

Please sign in to comment.