Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

removing enable api testing #338

Open
wants to merge 27 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
080368b
removing enable api testing
rohitniyerclevertap Jan 21, 2025
517924d
Removed console
rohitniyerclevertap Jan 21, 2025
db719d5
check for swerviceworkerpath and skipdialog
rohitniyerclevertap Jan 22, 2025
7d16a5c
Testing, removed card when ebell icon rejects browser native
rohitniyerclevertap Jan 22, 2025
3a01657
[WEB-3414]: reverted to old api
rohitniyerclevertap Jan 28, 2025
c35a46e
resolved comments
rohitniyerclevertap Jan 28, 2025
2e26039
Merge branch 'master' of github.com:CleverTap/clevertap-web-sdk into …
rohitniyerclevertap Jan 29, 2025
253b652
Fixed appilcationServerKey, subscriptionCallback, rejectCallback, okC…
rohitniyerclevertap Jan 29, 2025
4922ba1
removed console
rohitniyerclevertap Jan 29, 2025
b697bd1
Code rabbit review comments, fixed changes
rohitniyerclevertap Jan 30, 2025
0bf9c64
Added comments
rohitniyerclevertap Jan 30, 2025
f2edf61
renmaed notificationPushCalled variable
rohitniyerclevertap Feb 3, 2025
6683770
Added apnsWebPushId and apnsWebPushServiceUrl
rohitniyerclevertap Feb 4, 2025
55658b5
Merge branch 'develop' of github.com:CleverTap/clevertap-web-sdk into…
rohitniyerclevertap Feb 6, 2025
d6c61b7
took pull from develop
rohitniyerclevertap Feb 6, 2025
e17cac1
Merge branch 'develop' of github.com:CleverTap/clevertap-web-sdk into…
rohitniyerclevertap Feb 6, 2025
8298d99
Created new build
rohitniyerclevertap Feb 6, 2025
5f93fa1
Merge branch 'develop' of github.com:CleverTap/clevertap-web-sdk into…
rohitniyerclevertap Feb 6, 2025
f9fa87d
Created new build
rohitniyerclevertap Feb 6, 2025
cdd46ca
Added apns migration and fcm cases
rohitniyerclevertap Feb 6, 2025
39c2ec9
New build for apns migration and fcm public key
rohitniyerclevertap Feb 6, 2025
91607ea
Fix for safari browser block
rohitniyerclevertap Feb 7, 2025
f4c45c3
debugginf kunal's website
rohitniyerclevertap Feb 21, 2025
9b8908e
soft prompt clevertap inti issue
rohitniyerclevertap Feb 24, 2025
6a70641
Merge branch 'develop' of github.com:CleverTap/clevertap-web-sdk into…
rohitniyerclevertap Feb 24, 2025
dce8cc6
rollup config not found error
rohitniyerclevertap Feb 24, 2025
efe2ff9
minor identation changes
rohitniyerclevertap Feb 24, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
330 changes: 290 additions & 40 deletions clevertap.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion clevertap.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions clevertap.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion src/clevertap.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ import {
WZRK_FETCH,
WEBINBOX_CONFIG,
TIMER_FOR_NOTIF_BADGE_UPDATE,
ACCOUNT_ID
ACCOUNT_ID,
APPLICATION_SERVER_KEY_RECEIVED
} from './util/constants'
import { EMBED_ERROR } from './util/messages'
import { StorageManager, $ct } from './util/storage'
Expand Down Expand Up @@ -562,6 +563,11 @@ export default class CleverTap {
api.enableWebPush = (enabled, applicationServerKey) => {
setServerKey(applicationServerKey)
this.notifications._enableWebPush(enabled, applicationServerKey)
try {
StorageManager.saveToLSorCookie(APPLICATION_SERVER_KEY_RECEIVED, true)
} catch (error) {
this.#logger.error('Could not read value from local storage', error)
}
}
api.tr = (msg) => {
_tr(msg, {
Expand Down
112 changes: 101 additions & 11 deletions src/modules/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ import {
NOTIF_LAST_TIME,
ACCOUNT_ID,
POPUP_LOADING,
OLD_SOFT_PROMPT_SELCTOR_ID
OLD_SOFT_PROMPT_SELCTOR_ID,
APPLICATION_SERVER_KEY_RECEIVED,
NOTIFICATION_PUSH_METHOD_DEFERRED,
WEBPUSH_CONFIG_RECEIVED
} from '../util/constants'
import {
urlBase64ToUint8Array
} from '../util/encoder'
import { enablePush } from './webPushPrompt/prompt'
import { setNotificationHandlerValues, processSoftPrompt } from './webPushPrompt/prompt'

import { isChrome, isFirefox, isSafari } from '../util/helpers'

export default class NotificationHandler extends Array {
Expand All @@ -37,22 +41,58 @@ export default class NotificationHandler extends Array {
this.#account = account
}

setupWebPush (displayArgs) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are exposing new function in a public API, please check if it can be avoided, in case there are some contraints try adding them in the comments.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying to segregate the functionality of handling old/new soft prompt in a new function in the file prompt.js.
In processSoftPrompt function which is in prompt.js we create an instance of notificationHandler class as we need to call setupwebpushnotification method as well as a private method #setupWebPush .

Since we can't access private methods I had to create a method setupWebPush in notification.js which can be accessed from prompt.js file. This method internally calls the private method this.#setupWebPush.

Even if I try to move the logic of rendering old/nwe soft prompt inside of a private method in notificationhandler class, there is a method processWebpushConfig which is called in tr.js and code is written in prompt.js. Then again there will be a problem to call this new private method which takes care of handling of old/new soft prompt from prompt.js as since it will be a private method in notification.js

We were anyway exposing push as a public method before too which calls this.#setupwebpush, I just created another public api setupwebpush which calls this.#setupWebpush.

/*
A method in notification.js which can be accessed in prompt.js file to call the
private method this.#setUpWebPush
*/
this.#setUpWebPush(displayArgs)
}

push (...displayArgs) {
console.log('StorageManager.readFromLSorCookie(ACCOUNT_ID)', StorageManager.readFromLSorCookie(ACCOUNT_ID))
if (StorageManager.readFromLSorCookie(ACCOUNT_ID)) {
this.#setUpWebPush(displayArgs)
return 0
/*
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).
- `NOTIFICATION_PUSH_METHOD_DEFERRED`: 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, `NOTIFICATION_PUSH_METHOD_DEFERRED` is set to true, and the rendering is deferred until the webPushConfig is received.
*/
const isWebPushConfigPresent = StorageManager.readFromLSorCookie(WEBPUSH_CONFIG_RECEIVED)
const isApplicationServerKeyReceived = StorageManager.readFromLSorCookie(APPLICATION_SERVER_KEY_RECEIVED)
setNotificationHandlerValues({
logger: this.#logger,
account: this.#account,
request: this.#request,
displayArgs,
fcmPublicKey: this.#fcmPublicKey
})
if (isWebPushConfigPresent && isApplicationServerKeyReceived) {
processSoftPrompt()
} else {
StorageManager.saveToLSorCookie(NOTIFICATION_PUSH_METHOD_DEFERRED, true)
}
} else {
this.#logger.error('Account ID is not set')
}
}

enable (options = {}) {
const { swPath, skipDialog } = options
enablePush(this.#logger, this.#account, this.#request, swPath, skipDialog, this.#fcmPublicKey)
}

_processOldValues () {
if (this.#oldValues) {
if (Array.isArray(this.#oldValues) && this.#oldValues.length > 0) {
setNotificationHandlerValues({
logger: this.#logger,
account: this.#account,
request: this.#request,
displayArgs: this.#oldValues.slice(),
fcmPublicKey: this.#fcmPublicKey
})
StorageManager.saveToLSorCookie(NOTIFICATION_PUSH_METHOD_DEFERRED, true)
}

this.#setUpWebPush(this.#oldValues)
}
this.#oldValues = null
Expand Down Expand Up @@ -86,6 +126,8 @@ export default class NotificationHandler extends Array {
}

#setUpSafariNotifications (subscriptionCallback, apnsWebPushId, apnsServiceUrl, serviceWorkerPath) {
const softPromptCard = document.getElementById('pnWrapper')
const oldSoftPromptCard = document.getElementById('wzrk_wrapper')
if (this.#isNativeWebPushSupported() && this.#fcmPublicKey != null) {
StorageManager.setMetaProp(VAPID_MIGRATION_PROMPT_SHOWN, true)
navigator.serviceWorker.register(serviceWorkerPath).then((registration) => {
Expand Down Expand Up @@ -118,6 +160,12 @@ export default class NotificationHandler extends Array {
if (existingBellWrapper) {
existingBellWrapper.parentNode.removeChild(existingBellWrapper)
}
if (softPromptCard) {
softPromptCard.parentNode.removeChild(softPromptCard)
}
if (oldSoftPromptCard) {
oldSoftPromptCard.parentNode.removeChild(oldSoftPromptCard)
}
})
}

Expand All @@ -134,6 +182,14 @@ export default class NotificationHandler extends Array {
}
})
}
} else if (permission === 'denied') {
this.#logger.info('Error subscribing to Safari web push')
if (softPromptCard) {
softPromptCard.parentNode.removeChild(softPromptCard)
}
if (oldSoftPromptCard) {
oldSoftPromptCard.parentNode.removeChild(oldSoftPromptCard)
}
}
})
})
Expand All @@ -158,13 +214,28 @@ export default class NotificationHandler extends Array {
apnsWebPushId
}))
this.#logger.info('Subscription Data Received: ' + JSON.stringify(subscription))

const existingBellWrapper = document.getElementById('bell_wrapper')
if (existingBellWrapper) {
existingBellWrapper.parentNode.removeChild(existingBellWrapper)
}
if (softPromptCard) {
softPromptCard.parentNode.removeChild(softPromptCard)
}
if (oldSoftPromptCard) {
oldSoftPromptCard.parentNode.removeChild(oldSoftPromptCard)
}
StorageManager.saveToLSorCookie(PUSH_SUBSCRIPTION_DATA, subscriptionData)

this.#request.registerToken(subscriptionData)
this.#logger.info('Safari Web Push registered. Device Token: ' + subscription.deviceToken)
} else if (subscription.permission === 'denied') {
this.#logger.info('Error subscribing to Safari web push')
if (softPromptCard) {
softPromptCard.parentNode.removeChild(softPromptCard)
}
if (oldSoftPromptCard) {
oldSoftPromptCard.parentNode.removeChild(oldSoftPromptCard)
}
}
})
}
Expand Down Expand Up @@ -211,6 +282,9 @@ export default class NotificationHandler extends Array {
subscribeObj.applicationServerKey = urlBase64ToUint8Array(this.#fcmPublicKey)
}

const softPromptCard = document.getElementById('pnWrapper')
const oldSoftPromptCard = document.getElementById('wzrk_wrapper')

serviceWorkerRegistration.pushManager.subscribe(subscribeObj)
.then((subscription) => {
this.#logger.info('Service Worker registered. Endpoint: ' + subscription.endpoint)
Expand All @@ -235,9 +309,16 @@ export default class NotificationHandler extends Array {
subscriptionCallback()
}
const existingBellWrapper = document.getElementById('bell_wrapper')

if (existingBellWrapper) {
existingBellWrapper.parentNode.removeChild(existingBellWrapper)
}
if (softPromptCard) {
softPromptCard.parentNode.removeChild(softPromptCard)
}
if (oldSoftPromptCard) {
oldSoftPromptCard.parentNode.removeChild(oldSoftPromptCard)
}
}).catch((error) => {
// unsubscribe from webpush if error
serviceWorkerRegistration.pushManager.getSubscription().then((subscription) => {
Expand All @@ -255,6 +336,12 @@ export default class NotificationHandler extends Array {
}
})
this.#logger.error('Error subscribing: ' + error)
if (softPromptCard) {
softPromptCard.parentNode.removeChild(softPromptCard)
}
if (oldSoftPromptCard) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add version Bump and Changelog.

oldSoftPromptCard.parentNode.removeChild(oldSoftPromptCard)
}
})
}).catch((err) => {
this.#logger.error('error registering service worker: ' + err)
Expand Down Expand Up @@ -512,10 +599,13 @@ export default class NotificationHandler extends Array {
if (applicationServerKey != null) {
this.setApplicationServerKey(applicationServerKey)
}
const isNotificationPushCalled = StorageManager.readFromLSorCookie(NOTIFICATION_PUSH_METHOD_DEFERRED)
if (isNotificationPushCalled) {
return
}
if ($ct.webPushEnabled && $ct.notifApi.notifEnabledFromApi) {
this.#handleNotificationRegistration($ct.notifApi.displayArgs)
} else if (!$ct.webPushEnabled && $ct.notifApi.notifEnabledFromApi) {
this.#logger.error('Ensure that web push notifications are fully enabled and integrated before requesting them')
}
}
}
Loading
Loading