11'use strict' ;
22
3- const applicationServerPublicKey = "BN4BnHEmqGkZN_Oi71tFEjTJILdAspeFWMIMvjP1ZHa-fWL-iRP3_OD0UZ9RJ4uxDDIOJhyoZu_P G9U6JJzo1AM" ;
4- var sub_token = '' ;
5- const pushButton = document . querySelector ( '.js-push-btn' ) ;
63
7- let isSubscribed = false ;
8- let swRegistration = null ;
94
105function urlB64ToUint8Array ( base64String ) {
116 const padding = '=' . repeat ( ( 4 - base64String . length % 4 ) % 4 ) ;
@@ -43,13 +38,11 @@ function updateSubscriptionOnServer(subscription) {
4338 // TODO: Send subscription to application server
4439
4540 const subscriptionJson = document . querySelector ( '.js-subscription-json' ) ;
46- var sub_token = document . querySelector ( '#sub_token' ) ;
4741 const subscriptionDetails =
4842 document . querySelector ( '.js-subscription-details' ) ;
4943
5044 if ( subscription ) {
5145 subscriptionJson . textContent = JSON . stringify ( subscription ) ;
52- sub_token . value = JSON . stringify ( subscription ) ;
5346 subscriptionDetails . classList . remove ( 'is-invisible' ) ;
5447 console . log ( "subscribe" , JSON . stringify ( subscription ) ) ;
5548 } else {
@@ -58,7 +51,7 @@ function updateSubscriptionOnServer(subscription) {
5851}
5952
6053function subscribeUser ( ) {
61- const applicationServerKey = urlB64ToUint8Array ( applicationServerPublicKey ) ;
54+ const applicationServerKey = urlB64ToUint8Array ( document . applicationServerPublicKey ) ;
6255 swRegistration . pushManager . subscribe ( {
6356 userVisibleOnly : true ,
6457 applicationServerKey : applicationServerKey
@@ -67,7 +60,6 @@ function subscribeUser() {
6760 console . log ( 'User is subscribed.' ) ;
6861
6962 updateSubscriptionOnServer ( subscription ) ;
70- sub_token = subscription ;
7163 isSubscribed = true ;
7264
7365 updateBtn ( ) ;
@@ -125,29 +117,27 @@ function initializeUI() {
125117 } ) ;
126118}
127119
128- if ( 'serviceWorker' in navigator && 'PushManager' in window ) {
129- console . log ( 'Service Worker and Push is supported ') ;
120+ document . init_push = function ( ) {
121+ const pushButton = document . querySelector ( '.js-push-btn ') ;
130122
131- navigator . serviceWorker . register ( "/static/sw.js" )
132- . then ( function ( swReg ) {
133- console . log ( 'Service Worker is registered' , swReg ) ;
123+ let isSubscribed = false ;
124+ let swRegistration = null ;
134125
135- swRegistration = swReg ;
136- initializeUI ( ) ;
137- } )
138- . catch ( function ( error ) {
139- console . error ( 'Service Worker Error' , error ) ;
140- } ) ;
141- } else {
142- console . warn ( 'Push messaging is not supported' ) ;
143- pushButton . textContent = 'Push Not Supported' ;
144- }
126+ if ( 'serviceWorker' in navigator && 'PushManager' in window ) {
127+ console . log ( 'Service Worker and Push is supported' ) ;
145128
146- function push_message ( ) {
147- console . log ( "sub_token" , sub_token ) ;
148- $ . ajax ( {
149- type : "POST" ,
150- url : "/push/" ,
151- data : JSON . stringify ( { 'token' :sub_token } ) ,
152- } ) ;
129+ navigator . serviceWorker . register ( "/static/sw.js" )
130+ . then ( function ( swReg ) {
131+ console . log ( 'Service Worker is registered' , swReg ) ;
132+
133+ swRegistration = swReg ;
134+ initializeUI ( ) ;
135+ } )
136+ . catch ( function ( error ) {
137+ console . error ( 'Service Worker Error' , error ) ;
138+ } ) ;
139+ } else {
140+ console . warn ( 'Push messaging is not supported' ) ;
141+ pushButton . textContent = 'Push Not Supported' ;
142+ }
153143}
0 commit comments