@@ -89,7 +89,7 @@ export class WebPushClient extends BaseClient {
8989 async start ( ) {
9090 await this . _resolveSDKState ( ) ;
9191
92- if ( ! isSupportedBrowser ( ) ) {
92+ if ( ! this . _isSupportedBrowser ( ) ) {
9393 return this ;
9494 }
9595
@@ -135,49 +135,10 @@ export class WebPushClient extends BaseClient {
135135 return RegistrationState . PERMISSION_PROMPT_REQUIRED ;
136136 }
137137
138- async setUserId ( userId , tokenProvider ) {
139- await this . _resolveSDKState ( ) ;
140-
141- if ( ! isSupportedBrowser ( ) ) {
142- return ;
143- }
144-
145- if ( this . _deviceId === null ) {
146- const error = new Error ( '.start must be called before .setUserId' ) ;
147- return Promise . reject ( error ) ;
148- }
149- if ( typeof userId !== 'string' ) {
150- throw new Error ( `User ID must be a string (was ${ userId } )` ) ;
151- }
152- if ( userId === '' ) {
153- throw new Error ( 'User ID cannot be the empty string' ) ;
154- }
155- if ( this . _userId !== null && this . _userId !== userId ) {
156- throw new Error ( 'Changing the `userId` is not allowed.' ) ;
157- }
158-
159- const path = `${ this . _baseURL } /device_api/v1/instances/${ encodeURIComponent (
160- this . instanceId
161- ) } /devices/web/${ this . _deviceId } /user`;
162-
163- const { token : beamsAuthToken } = await tokenProvider . fetchToken ( userId ) ;
164- const options = {
165- method : 'PUT' ,
166- path,
167- headers : {
168- Authorization : `Bearer ${ beamsAuthToken } ` ,
169- } ,
170- } ;
171- await doRequest ( options ) ;
172-
173- this . _userId = userId ;
174- return this . _deviceStateStore . setUserId ( userId ) ;
175- }
176-
177138 async stop ( ) {
178139 await this . _resolveSDKState ( ) ;
179140
180- if ( ! isSupportedBrowser ( ) ) {
141+ if ( ! this . _isSupportedBrowser ( ) ) {
181142 return ;
182143 }
183144
@@ -195,7 +156,7 @@ export class WebPushClient extends BaseClient {
195156 }
196157
197158 async clearAllState ( ) {
198- if ( ! isSupportedBrowser ( ) ) {
159+ if ( ! this . _isSupportedBrowser ( ) ) {
199160 return ;
200161 }
201162
@@ -243,6 +204,32 @@ export class WebPushClient extends BaseClient {
243204 } ,
244205 } ) ;
245206 }
207+
208+ /**
209+ * Modified from https://stackoverflow.com/questions/4565112
210+ */
211+ _isSupportedBrowser ( ) {
212+ const winNav = window . navigator ;
213+ const vendorName = winNav . vendor ;
214+
215+ const isChromium =
216+ window . chrome !== null && typeof window . chrome !== 'undefined' ;
217+ const isOpera = winNav . userAgent . indexOf ( 'OPR' ) > - 1 ;
218+ const isEdge = winNav . userAgent . indexOf ( 'Edg' ) > - 1 ;
219+ const isFirefox = winNav . userAgent . indexOf ( 'Firefox' ) > - 1 ;
220+
221+ const isChrome =
222+ isChromium && vendorName === 'Google Inc.' && ! isEdge && ! isOpera ;
223+
224+ const isSupported = isChrome || isOpera || isFirefox || isEdge ;
225+
226+ if ( ! isSupported ) {
227+ console . warn (
228+ 'Pusher Web Push Notifications supports Chrome, Firefox, Edge and Opera.'
229+ ) ;
230+ }
231+ return isSupported ;
232+ }
246233}
247234
248235async function getServiceWorkerRegistration ( ) {
@@ -279,29 +266,3 @@ function urlBase64ToUInt8Array(base64String) {
279266 const rawData = window . atob ( base64 ) ;
280267 return Uint8Array . from ( [ ...rawData ] . map ( char => char . charCodeAt ( 0 ) ) ) ;
281268}
282-
283- /**
284- * Modified from https://stackoverflow.com/questions/4565112
285- */
286- function isSupportedBrowser ( ) {
287- const winNav = window . navigator ;
288- const vendorName = winNav . vendor ;
289-
290- const isChromium =
291- window . chrome !== null && typeof window . chrome !== 'undefined' ;
292- const isOpera = winNav . userAgent . indexOf ( 'OPR' ) > - 1 ;
293- const isEdge = winNav . userAgent . indexOf ( 'Edg' ) > - 1 ;
294- const isFirefox = winNav . userAgent . indexOf ( 'Firefox' ) > - 1 ;
295-
296- const isChrome =
297- isChromium && vendorName === 'Google Inc.' && ! isEdge && ! isOpera ;
298-
299- const isSupported = isChrome || isOpera || isFirefox || isEdge ;
300-
301- if ( ! isSupported ) {
302- console . warn (
303- 'Pusher Web Push Notifications supports Chrome, Firefox, Edge and Opera.'
304- ) ;
305- }
306- return isSupported ;
307- }
0 commit comments