Skip to content

Commit b45d144

Browse files
committed
fix: allow setting of clientUrl for web push registrations
1 parent adf9fae commit b45d144

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

src/LeanplumInternal.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,8 @@ Use "npm update leanplum-sdk" or go to https://docs.leanplum.com/reference#javas
560560
* @return {Promise} Resolves if true, rejects if false.
561561
*/
562562
isWebPushSubscribed(): Promise<boolean> {
563-
return this._pushManager.isWebPushSubscribed()
563+
const clientUrl = this._webPushOptions?.clientUrl || ''
564+
return this._pushManager.isWebPushSubscribed(clientUrl)
564565
}
565566

566567
setWebPushOptions(options: WebPushOptions): void {
@@ -598,7 +599,8 @@ Use "npm update leanplum-sdk" or go to https://docs.leanplum.com/reference#javas
598599
* @return {Promise} Resolves on success, otherwise rejects.
599600
*/
600601
unregisterFromWebPush(): Promise<void> {
601-
return this._pushManager.unsubscribeUser()
602+
const clientUrl = this._webPushOptions?.clientUrl || ''
603+
return this._pushManager.unsubscribeUser(clientUrl)
602604
}
603605

604606
/**

src/PushManager.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ export default class PushManager {
5353
* Whether or not the browser is subscribed to web push notifications.
5454
* @return {Promise} True if subscribed, else false.
5555
*/
56-
public async isWebPushSubscribed(): Promise<boolean> {
56+
public async isWebPushSubscribed(clientUrl = ''): Promise<boolean> {
5757
if (!this.isWebPushSupported()) {
5858
return false
5959
}
6060

61-
const registration = await this.getServiceWorkerRegistration()
61+
const registration = await this.getServiceWorkerRegistration(clientUrl)
6262

6363
if (!registration) {
6464
return false
@@ -139,8 +139,8 @@ export default class PushManager {
139139
* Unsubscribe the user(browser) from push.
140140
* @return {Promise} Resolves if unsubscribed, otherwise rejects.
141141
*/
142-
public async unsubscribeUser(): Promise<void> {
143-
const subscribed = await this.isWebPushSubscribed()
142+
public async unsubscribeUser(clientUrl = ''): Promise<void> {
143+
const subscribed = await this.isWebPushSubscribed(clientUrl)
144144

145145
if (subscribed) {
146146
try {
@@ -161,9 +161,9 @@ export default class PushManager {
161161
* Retrieves the service worker registration object from browser.
162162
* @return {object} Returns the registration or null.
163163
*/
164-
private async getServiceWorkerRegistration(): Promise<ServiceWorkerRegistration> {
164+
private async getServiceWorkerRegistration(clientUrl = ''): Promise<ServiceWorkerRegistration> {
165165
if (!this.serviceWorkerRegistration) {
166-
this.serviceWorkerRegistration = await this.serviceWorker.getRegistration()
166+
this.serviceWorkerRegistration = await this.serviceWorker.getRegistration(clientUrl)
167167
}
168168

169169
return this.serviceWorkerRegistration

src/types/public.ts

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export type EventType = 'start' | 'resume' | 'track' | 'setUserAttribute' | 'adv
5555
export interface WebPushOptions {
5656
serviceWorkerUrl?: string;
5757
scope?: string;
58+
clientUrl?: string;
5859
}
5960

6061
export enum ActionParameterType {

test/specs/LeanplumInternal.test.ts

+17
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,23 @@ describe(LeanplumInternal, () => {
563563
expect(registerCall[0]).toEqual(serviceWorkerUrl)
564564
expect(registerCall[1]).toEqual({ scope })
565565
})
566+
567+
it('can set client url for push regsitration', async() => {
568+
pushManagerMock.isWebPushSupported.mockReturnValueOnce(true)
569+
570+
const clientUrl = '/products'
571+
572+
lp.setWebPushOptions({ clientUrl })
573+
574+
await lp.registerForWebPush()
575+
await lp.unregisterFromWebPush()
576+
577+
const isSubscribed = pushManagerMock.isWebPushSubscribed.mock.calls
578+
expect(isSubscribed.every(call => call[0] === clientUrl)).toBe(true);
579+
580+
const unsubscribeUser = pushManagerMock.unsubscribeUser.mock.calls
581+
expect(unsubscribeUser.every(call => call[0] === clientUrl)).toBe(true);
582+
})
566583
})
567584

568585
describe('unregisterFromWebPush', () => {

0 commit comments

Comments
 (0)