-
Notifications
You must be signed in to change notification settings - Fork 19
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
base: develop
Are you sure you want to change the base?
Changes from all commits
080368b
517924d
db719d5
7d16a5c
3a01657
c35a46e
2e26039
253b652
4922ba1
b697bd1
0bf9c64
f2edf61
6683770
55658b5
d6c61b7
e17cac1
8298d99
5f93fa1
f9fa87d
cdd46ca
39c2ec9
91607ea
f4c45c3
9b8908e
6a70641
dce8cc6
efe2ff9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -37,22 +41,58 @@ export default class NotificationHandler extends Array { | |
this.#account = account | ||
} | ||
|
||
setupWebPush (displayArgs) { | ||
/* | ||
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 | ||
|
@@ -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) => { | ||
|
@@ -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) | ||
} | ||
}) | ||
} | ||
|
||
|
@@ -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) | ||
} | ||
} | ||
}) | ||
}) | ||
|
@@ -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) | ||
} | ||
} | ||
}) | ||
} | ||
|
@@ -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) | ||
|
@@ -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) => { | ||
|
@@ -255,6 +336,12 @@ export default class NotificationHandler extends Array { | |
} | ||
}) | ||
this.#logger.error('Error subscribing: ' + error) | ||
if (softPromptCard) { | ||
softPromptCard.parentNode.removeChild(softPromptCard) | ||
} | ||
if (oldSoftPromptCard) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
@@ -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') | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.