diff options
| author | altaf-creator <dev@altafcreator.com> | 2025-12-03 15:08:58 +0700 |
|---|---|---|
| committer | altaf-creator <dev@altafcreator.com> | 2025-12-03 15:08:58 +0700 |
| commit | 2f946c233ee67e149b79c2878efeddbbb0dc1846 (patch) | |
| tree | 719ffc079275f9cff322c89bb08aa83826ceeb91 | |
| parent | 3979b8bcb35bc981355a83134ce7828dbfc7c0e3 (diff) | |
notif
| -rw-r--r-- | backend/main.py | 15 | ||||
| -rw-r--r-- | backend/notif_test.py | 21 | ||||
| -rw-r--r-- | frontend/main.js | 5 | ||||
| -rw-r--r-- | frontend/start.js | 1 |
4 files changed, 37 insertions, 5 deletions
diff --git a/backend/main.py b/backend/main.py index 22d2936..c69247f 100644 --- a/backend/main.py +++ b/backend/main.py @@ -106,6 +106,7 @@ ONESIGNAL_APP_ID = "83901cc7-d964-475a-90ec-9f854df4ba52" class RequestData(BaseModel): duration: int machine_id: str + onesignal_subscription_id: str class InformationRequestData(BaseModel): @@ -191,7 +192,11 @@ def reminder_timer_finished(timer_id): scheduler.add_job(final_timer_finished, 'date', run_date=out[0][RowIndices.END_TIME], id=str(timer_id), args=[timer_id]) - notification = Notification(app_id=ONESIGNAL_APP_ID, included_segments=['All'], contents={'en': 'get ready to get your bloody laundry'}, headings={'en': 'laundry almost finished'}, priority=10) + notification = Notification(app_id=ONESIGNAL_APP_ID, + include_subscription_ids=[out[0][RowIndices.SUBSCRIPTION_ID]], + contents={'en': 'get ready to get your bloody laundry'}, + headings={'en': 'Laundry Reminder'}, + priority=10) try: api_response = api_instance.create_notification(notification) @@ -204,7 +209,11 @@ def final_timer_finished(timer_id): cursor.execute(f"SELECT * FROM timers WHERE timer_id = '{timer_id}'") out = cursor.fetchall() - notification = Notification(app_id=ONESIGNAL_APP_ID, included_segments=['All'], contents={'en': 'get your bloody laundry'}, headings={'en': 'laundry finished'}, priority=10) + notification = Notification(app_id=ONESIGNAL_APP_ID, + include_subscription_ids=[out[0][RowIndices.SUBSCRIPTION_ID]], + contents={'en': 'get your bloody laundry'}, + headings={'en': 'Laundry Finished'}, + priority=10) try: api_response = api_instance.create_notification(notification) @@ -276,7 +285,7 @@ def start_new_timer(data: RequestData, response: fastapi.Response, session_key: end_date = now + datetime.timedelta(minutes=(data.duration * 30)) cursor.execute(f""" INSERT INTO timers (user_id, start_time, end_time, block, machine, status, subscription_id) - VALUES ('{session_key}', '{now.isoformat()}', '{end_date.isoformat()}', {block}, {machine}, 'RUNNING', 'TODO') + VALUES ('{session_key}', '{now.isoformat()}', '{end_date.isoformat()}', {block}, {machine}, 'RUNNING', '{data.onesignal_subscription_id}') """) conn.commit() cursor.execute("SELECT * FROM timers;") diff --git a/backend/notif_test.py b/backend/notif_test.py new file mode 100644 index 0000000..e0d82c0 --- /dev/null +++ b/backend/notif_test.py @@ -0,0 +1,21 @@ +from dotenv import load_dotenv +from os import getenv +import onesignal +from onesignal.api import default_api +from onesignal.model.notification import Notification + +load_dotenv() + +onesignal_configuration = onesignal.Configuration( + rest_api_key=getenv("REST_API_KEY"), + organization_api_key=getenv("ORGANIZATION_API_KEY"), +) + +api_client = onesignal.ApiClient(onesignal_configuration) +api_instance = default_api.DefaultApi(api_client) + +ONESIGNAL_APP_ID = "83901cc7-d964-475a-90ec-9f854df4ba52" + +# yes that's my subscription id. probably outdated by now +notification = Notification(app_id=ONESIGNAL_APP_ID, include_subscription_ids=['86f24e73-8887-4e29-b607-31e1a8f25b93', '6a2b4b58-a1d2-403b-afa0-b4a987ca6139'], contents={'en': 'hello world'}, headings={'en': 'Laundry Finished!'}, priority=10) +print(api_instance.create_notification(notification)) diff --git a/frontend/main.js b/frontend/main.js index 969614e..7ac8c08 100644 --- a/frontend/main.js +++ b/frontend/main.js @@ -1,7 +1,8 @@ // --- begin, important constants const data = { duration: 1, // will be multiplied by 30 - machine_id: "" + machine_id: "", + onesignal_subscription_id: "", } const API_URL = "https://backend.laundryweb.altafcreator.com" @@ -49,7 +50,7 @@ function decreaseTime() { } // --- starting a timer -function start() { +async function start() { fetch(`${API_URL}/start`, { credentials: "include", method: "POST", diff --git a/frontend/start.js b/frontend/start.js index 374c68d..1bf7571 100644 --- a/frontend/start.js +++ b/frontend/start.js @@ -17,6 +17,7 @@ OneSignalDeferred.push(async function(OneSignal) { notif.remove(); startbtn.disabled = false; } + data.onesignal_subscription_id = OneSignal.User.PushSubscription["id"] ; }); notbtn.addEventListener("click", () => requestPermission()) |
