Skip to content

Instantly share code, notes, and snippets.

@SuryaSankar
SuryaSankar / service_worker.js
Created February 20, 2020 12:10
Basic Service worker
'use strict';
/* eslint-enable max-len */
self.addEventListener('install', function(event) {
console.log('Service Worker installing.');
});
self.addEventListener('activate', function(event) {
console.log('Service Worker activating.');
@SuryaSankar
SuryaSankar / register_service_worker.js
Created February 20, 2020 12:28
Basic registration script
'use strict';
function urlB64ToUint8Array(base64String) {
const padding = '='.repeat((4 - base64String.length % 4) % 4);
const base64 = (base64String + padding)
.replace(/\-/g, '+')
.replace(/_/g, '/');
const rawData = window.atob(base64);
const outputArray = new Uint8Array(rawData.length);
@SuryaSankar
SuryaSankar / push_subscription_sample.json
Created February 20, 2020 12:30
Sample push subscription json
{
"endpoint":"https://fcm.googleapis.com/fcm/send/c434328ouoiuoiu32432cMR1nib-T2eDKDu3cZg3Y9M8y_gQa2VLtKeBerwerwerewr32423432432ewrrewrewfcaezv6ctv84-wKqTOeDZFN423409832049832049iuaroieuroewBgg3-26BtZ", "expirationTime":null,
"keys":{
"p256dh":"BEuDDnZ4324234ewrewrewr2432432ewrew7gzfEsBKbe3z26ycB8l2h-h_v9yLLAw82CH83IQiCbT9238409348305Hgs",
"auth":"i7jSDPe_1Nm321rrerew12321-XSA"
}
}
@SuryaSankar
SuryaSankar / webpush_handler.py
Created February 20, 2020 12:33
Basic webpush handler
from pywebpush import webpush, WebPushException
import json
from flask import current_app
def trigger_push_notification(push_subscription, title, body):
try:
response = webpush(
subscription_info=json.loads(push_subscription.subscription_json),
data=json.dumps({"title": title, "body": body}),
@SuryaSankar
SuryaSankar / webpush_handler.py
Last active February 20, 2020 12:42
Basic webpush handler
from pywebpush import webpush, WebPushException
import json
from flask import current_app
def trigger_push_notification(push_subscription, title, body):
try:
response = webpush(
subscription_info=json.loads(push_subscription.subscription_json),
data=json.dumps({"title": title, "body": body}),
@SuryaSankar
SuryaSankar / index.html
Created February 20, 2020 12:43
Webpush basic home page
<html>
<head>
<link rel="icon" href="data:,">
</head>
<body>
<h1>Webpush Demo</h1>
<script
type="text/javascript"
src="/static/register_service_worker.js">
</script>
@SuryaSankar
SuryaSankar / push_subscriptions_api.py
Created February 20, 2020 12:46
Simple api for push subscriptions
@app.route("/api/push-subscriptions", methods=["POST"])
def create_push_subscription():
json_data = request.get_json()
subscription = PushSubscription.query.filter_by(
subscription_json=json_data['subscription_json']
).first()
if subscription is None:
subscription = PushSubscription(
subscription_json=json_data['subscription_json']
)
@SuryaSankar
SuryaSankar / trigger_push_notifications_basic.py
Created February 20, 2020 12:48
Trigger push notifications - basic
@app.route("/admin-api/trigger-push-notifications", methods=["POST"])
def trigger_push_notifications():
json_data = request.get_json()
subscriptions = PushSubscription.query.all()
results = trigger_push_notifications_for_subscriptions(
subscriptions,
json_data.get('title'),
json_data.get('body')
)
return jsonify({
@SuryaSankar
SuryaSankar / db_connection_simple.py
Last active March 10, 2020 09:17
Basic webpush app - db connection
app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite://"
db = SQLAlchemy(app)
class PushSubscription(db.Model):
id = db.Column(db.Integer, primary_key=True, unique=True)
subscription_json = db.Column(db.Text, nullable=False)
db.create_all()
@SuryaSankar
SuryaSankar / webpush_basic_admin.html
Created February 20, 2020 13:08
Webpush basic functionality - Admin
<html>
<head>
<link rel="icon" href="data:,">
<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
</head>
<body>
<h1>Trigger a Push Notification</h1>