Skip to content

Commit d948a07

Browse files
committed
api for push button
1 parent 31092b5 commit d948a07

File tree

4 files changed

+38
-13
lines changed

4 files changed

+38
-13
lines changed

main.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,23 @@ def push_to_all_users():
4444
print("error",e)
4545

4646
return redirect(url_for('main.index'))
47+
48+
49+
@main.route("/push_v1/",methods=['POST'])
50+
def push_v1():
51+
message = "Push Test v1"
52+
print("is_json",request.is_json)
53+
54+
if not request.json or not request.json.get('sub_token'):
55+
return jsonify({'failed':1})
56+
57+
print("request.json",request.json)
58+
59+
token = request.json.get('sub_token')
60+
try:
61+
send_web_push(json.loads(token), message)
62+
return jsonify({'success':1})
63+
except Exception as e:
64+
print("error",e)
65+
66+
return jsonify({'failed':str(e)})

static/main.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

3-
const applicationServerPublicKey = "BN4BnHEmqGkZN_Oi71tFEjTJILdAspeFWMIMvjP1ZHa-fWL-iRP3_OD0UZ9RJ4uxDDIOJhyoZu_P G9U6JJzo1AM";
4-
var sub_token = '';
3+
const applicationServerPublicKey = "BA0I0zCKG13_vN7T3I310T_Mkg0D6IpVc_t6lsSxfCqbvDrirGI16gef0Uks52pAjGXq02LpyNTG dYnzr7gIwhs";
54
const pushButton = document.querySelector('.js-push-btn');
65

76
let isSubscribed = false;
@@ -43,15 +42,12 @@ function updateSubscriptionOnServer(subscription) {
4342
// TODO: Send subscription to application server
4443

4544
const subscriptionJson = document.querySelector('.js-subscription-json');
46-
var sub_token = document.querySelector('#sub_token');
4745
const subscriptionDetails =
4846
document.querySelector('.js-subscription-details');
4947

5048
if (subscription) {
5149
subscriptionJson.textContent = JSON.stringify(subscription);
52-
sub_token.value = JSON.stringify(subscription);
5350
subscriptionDetails.classList.remove('is-invisible');
54-
console.log("subscribe",JSON.stringify(subscription));
5551
} else {
5652
subscriptionDetails.classList.add('is-invisible');
5753
}
@@ -67,7 +63,7 @@ function subscribeUser() {
6763
console.log('User is subscribed.');
6864

6965
updateSubscriptionOnServer(subscription);
70-
sub_token = subscription;
66+
localStorage.setItem('sub_token',JSON.stringify(subscription));
7167
isSubscribed = true;
7268

7369
updateBtn();
@@ -76,7 +72,7 @@ function subscribeUser() {
7672
console.log('Failed to subscribe the user: ', err);
7773
updateBtn();
7874
});
79-
}push_message
75+
}
8076

8177
function unsubscribeUser() {
8278
swRegistration.pushManager.getSubscription()
@@ -144,10 +140,18 @@ if ('serviceWorker' in navigator && 'PushManager' in window) {
144140
}
145141

146142
function push_message() {
147-
console.log("sub_token", sub_token);
143+
console.log("sub_token", localStorage.getItem('sub_token'));
148144
$.ajax({
149145
type: "POST",
150-
url: "/push/",
151-
data: JSON.stringify({'token':sub_token}),
146+
url: "/push_v1/",
147+
contentType: 'application/json; charset=utf-8',
148+
dataType:'json',
149+
data: JSON.stringify({'sub_token':localStorage.getItem('sub_token')}),
150+
success: function( data ){
151+
console.log("success",data);
152+
},
153+
error: function( jqXhr, textStatus, errorThrown ){
154+
console.log("error",errorThrown);
155+
}
152156
});
153157
}

static/sw.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/* eslint-disable max-len */
44

5-
const applicationServerPublicKey = "BN4BnHEmqGkZN_Oi71tFEjTJILdAspeFWMIMvjP1ZHa-fWL-iRP3_OD0UZ9RJ4uxDDIOJhyoZu_P G9U6JJzo1AM";
5+
const applicationServerPublicKey = "BA0I0zCKG13_vN7T3I310T_Mkg0D6IpVc_t6lsSxfCqbvDrirGI16gef0Uks52pAjGXq02LpyNTG dYnzr7gIwhs";
66

77
/* eslint-enable max-len */
88

templates/index.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,16 @@ <h1>WebPush Notification</h1>
3737

3838
<hr>
3939
<p>You can test push notification below.</p>
40+
<button type="submit" class="mdl-button mdl-js-button mdl-button--raised mdl-button--colored" onclick="push_message()">Test Push Notification</button>
4041

41-
<form action="/push/" method="POST">
42+
<!-- <form action="/push/" method="POST">
4243
<div class="mdl-textfield mdl-js-textfield">
4344
<input required type="hidden" name="sub_token" id="sub_token">
4445
<label class="mdl-textfield__label" for="message">Your message.</label>
4546
<input class="mdl-textfield__input" required type="text" name="message" id="message">
4647
</div>
4748
<button type="submit" class="mdl-button mdl-js-button mdl-button--raised mdl-button--colored">PUSH</button>
48-
</form>
49+
</form> -->
4950
</section>
5051
</main>
5152

0 commit comments

Comments
 (0)