11'use strict' ;
22
3+ const applicationServerPublicKey = "BNbxGYNMhEIi9zrneh7mqV4oUanjLUK3m+mYZBc62frMKrEoMk88r3Lk596T0ck9xlT+aok0fO1KXBLV4+XqxYM=" ;
4+ const pushButton = document . querySelector ( '.js-push-btn' ) ;
35
6+ let isSubscribed = false ;
7+ let swRegistration = null ;
48
59function urlB64ToUint8Array ( base64String ) {
610 const padding = '=' . repeat ( ( 4 - base64String . length % 4 ) % 4 ) ;
@@ -44,14 +48,14 @@ function updateSubscriptionOnServer(subscription) {
4448 if ( subscription ) {
4549 subscriptionJson . textContent = JSON . stringify ( subscription ) ;
4650 subscriptionDetails . classList . remove ( 'is-invisible' ) ;
47- console . log ( "subscribe" , JSON . stringify ( subscription ) ) ;
4851 } else {
4952 subscriptionDetails . classList . add ( 'is-invisible' ) ;
5053 }
5154}
5255
5356function subscribeUser ( ) {
54- const applicationServerKey = urlB64ToUint8Array ( document . applicationServerPublicKey ) ;
57+ // const applicationServerPublicKey = localStorage.getItem('applicationServerPublicKey');
58+ const applicationServerKey = urlB64ToUint8Array ( applicationServerPublicKey ) ;
5559 swRegistration . pushManager . subscribe ( {
5660 userVisibleOnly : true ,
5761 applicationServerKey : applicationServerKey
@@ -60,6 +64,7 @@ function subscribeUser() {
6064 console . log ( 'User is subscribed.' ) ;
6165
6266 updateSubscriptionOnServer ( subscription ) ;
67+ localStorage . setItem ( 'sub_token' , JSON . stringify ( subscription ) ) ;
6368 isSubscribed = true ;
6469
6570 updateBtn ( ) ;
@@ -68,7 +73,7 @@ function subscribeUser() {
6873 console . log ( 'Failed to subscribe the user: ' , err ) ;
6974 updateBtn ( ) ;
7075 } ) ;
71- } push_message
76+ }
7277
7378function unsubscribeUser ( ) {
7479 swRegistration . pushManager . getSubscription ( )
@@ -117,27 +122,48 @@ function initializeUI() {
117122 } ) ;
118123}
119124
120- document . init_push = function ( ) {
121- const pushButton = document . querySelector ( '.js-push-btn' ) ;
122-
123- let isSubscribed = false ;
124- let swRegistration = null ;
125+ if ( 'serviceWorker' in navigator && 'PushManager' in window ) {
126+ console . log ( 'Service Worker and Push is supported' ) ;
125127
126- if ( 'serviceWorker' in navigator && 'PushManager' in window ) {
127- console . log ( 'Service Worker and Push is supported' ) ;
128+ navigator . serviceWorker . register ( "/static/sw.js" )
129+ . then ( function ( swReg ) {
130+ console . log ( 'Service Worker is registered' , swReg ) ;
128131
129- navigator . serviceWorker . register ( "/static/sw.js" )
130- . then ( function ( swReg ) {
131- console . log ( 'Service Worker is registered' , swReg ) ;
132+ swRegistration = swReg ;
133+ initializeUI ( ) ;
134+ } )
135+ . catch ( function ( error ) {
136+ console . error ( 'Service Worker Error' , error ) ;
137+ } ) ;
138+ } else {
139+ console . warn ( 'Push meapplicationServerPublicKeyssaging is not supported' ) ;
140+ pushButton . textContent = 'Push Not Supported' ;
141+ }
132142
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- }
143+ function push_message ( ) {
144+ console . log ( "sub_token" , localStorage . getItem ( 'sub_token' ) ) ;
145+ $ . ajax ( {
146+ type : "POST" ,
147+ url : "/push_v1/" ,
148+ contentType : 'application/json; charset=utf-8' ,
149+ dataType :'json' ,
150+ data : JSON . stringify ( { 'sub_token' :localStorage . getItem ( 'sub_token' ) } ) ,
151+ success : function ( data ) {
152+ console . log ( "success" , data ) ;
153+ } ,
154+ error : function ( jqXhr , textStatus , errorThrown ) {
155+ console . log ( "error" , errorThrown ) ;
156+ }
157+ } ) ;
143158}
159+
160+ // $(document).ready(function(){
161+ // $.ajax({
162+ // type:"GET",
163+ // url:'/subscription/',
164+ // success:function(response){
165+ // console.log("response",response);
166+ // localStorage.setItem('applicationServerPublicKey',response.public_key);
167+ // }
168+ // })
169+ // });
0 commit comments