-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4d176be
commit 0763c5e
Showing
7 changed files
with
220 additions
and
43 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
self.addEventListener('push', event => { | ||
try { | ||
const data = event.data.json(); // Tries to parse JSON data | ||
self.registration.showNotification(data.title, { | ||
body: data.body, | ||
icon: './Images/Aria.jpg' | ||
}); | ||
} catch (error) { | ||
console.error('Error parsing push notification data:', error); | ||
self.registration.showNotification('Notification', { | ||
body: event.data.text(), | ||
icon: './Images/Aria.jpg' | ||
}); | ||
} | ||
}); | ||
|
||
self.addEventListener('notificationclick', event => { | ||
event.notification.close(); | ||
event.waitUntil( | ||
self.clients.matchAll({type: 'window'}).then(clientList => { | ||
for (let client of clientList) { | ||
if (client.url === '/' && 'focus' in client) { | ||
return client.focus(); | ||
} | ||
} | ||
if (self.clients.openWindow) { | ||
return self.clients.openWindow('/'); | ||
} | ||
}) | ||
); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from flask_sqlalchemy import SQLAlchemy | ||
|
||
db = SQLAlchemy() | ||
|
||
class Subscription(db.Model): | ||
id = db.Column(db.Integer, primary_key=True) | ||
user_id = db.Column(db.String(120), unique=True, nullable=False) | ||
subscription_info = db.Column(db.String, nullable=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,30 @@ | ||
from apscheduler.schedulers.background import BackgroundScheduler | ||
from flask import current_app as app | ||
from datetime import datetime, timedelta | ||
from services.azure_mongodb import MongoDBClient | ||
import pymongo | ||
from models.subscription import db, Subscription | ||
from pywebpush import webpush, WebPushException | ||
import json | ||
import os | ||
from dotenv import load_dotenv | ||
load_dotenv() | ||
|
||
def notify_check_ins(): | ||
db_client = MongoDBClient.get_client() | ||
db = db_client[MongoDBClient.get_db_name()] | ||
now = datetime.now() | ||
upcoming_check_ins = db.check_ins.find({ | ||
'check_in_time': {'$gte': now, '$lt': now + timedelta(days=7)}, | ||
'notify': True, | ||
'status': 'upcoming' | ||
}) | ||
|
||
for check_in in upcoming_check_ins: | ||
delta = check_in['check_in_time'] - now | ||
if delta.days == 7 or delta.days == 1 or delta.total_seconds() / 3600 <= 1: | ||
send_notification(check_in['user_id'], check_in['check_in_time'], delta) | ||
|
||
def send_notification(user_id, check_in_time, delta): | ||
message = "" | ||
if delta.days == 7: | ||
message = "Your check-in is scheduled in 1 week." | ||
elif delta.days == 1: | ||
message = "Your check-in is scheduled tomorrow." | ||
elif delta.total_seconds() / 3600 <= 1: | ||
message = "Your check-in is in less than 1 hour." | ||
|
||
# This is where you'd integrate your actual notification logic | ||
print(f"Notify {user_id}: {message}") | ||
|
||
scheduler = BackgroundScheduler() | ||
scheduler.add_job(func=notify_check_ins, trigger='interval', hours=1) | ||
scheduler.start() | ||
|
||
def init_scheduler(app): | ||
app.config['scheduler'] = scheduler | ||
def send_push_notification(user_id, message): | ||
subscription = Subscription.query.filter_by(user_id=user_id).first() | ||
if not subscription: | ||
print(f"No subscription found for user {user_id}") | ||
return False | ||
|
||
try: | ||
webpush( | ||
subscription_info=json.loads(subscription.subscription_info), | ||
data=json.dumps({ | ||
"title": "Notification Title", | ||
"body": message | ||
}), | ||
vapid_private_key=os.environ.get("VAPID_PRIVATE_KEY"), | ||
vapid_claims={"sub": "mailto:[email protected]"} | ||
) | ||
print("Notification sent successfully") | ||
return True | ||
except WebPushException as e: | ||
print(f"Failed to send notification: {e}") | ||
if e.response and e.response.json(): | ||
print(e.response.json()) |