From 17cbc5dc50ae23f8dec1ad5a5b2f9945da11ef01 Mon Sep 17 00:00:00 2001 From: dcordz <17937472+dcordz@users.noreply.github.com> Date: Fri, 15 Nov 2024 08:19:46 -0500 Subject: [PATCH] botw-schedule - fix sending notifications when bill updated --- app/frontend/pages/Notifications.tsx | 21 ++++++++++++------- app/models/bill.rb | 5 ++--- .../sway_push_notification_service.rb | 1 + public/service_worker.js | 6 +++--- 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/app/frontend/pages/Notifications.tsx b/app/frontend/pages/Notifications.tsx index 34d2432f..7849f43f 100644 --- a/app/frontend/pages/Notifications.tsx +++ b/app/frontend/pages/Notifications.tsx @@ -1,6 +1,6 @@ import { router } from "@inertiajs/react"; import { useAxiosPost } from "app/frontend/hooks/useAxios"; -import { handleError, logDev } from "app/frontend/sway_utils"; +import { handleError, logDev, notify } from "app/frontend/sway_utils"; import { useCallback, useEffect, useState } from "react"; import { Button } from "react-bootstrap"; import { sway } from "sway"; @@ -29,7 +29,14 @@ const Notifications: React.FC = ({ user: _user, subscriptions }) => { r.pushManager.getSubscription().then((s) => { if (s?.endpoint) { - testNotify({ endpoint: s.endpoint }).catch(console.error); + testNotify({ endpoint: s.endpoint }) + .then(() => { + notify({ + level: "success", + title: "Test notification sent. You should receive one soon...", + }); + }) + .catch(console.error); } }); }); @@ -163,22 +170,22 @@ const Notifications: React.FC = ({ user: _user, subscriptions }) => { if (subscription?.subscribed) { return (
-
+
We'll stop sending you a push notification whenever a new Bill of the Week is released.
-
+
-

If you don't receive a notification make sure that notifications are permitted for this browser in your device settings.

+
); diff --git a/app/models/bill.rb b/app/models/bill.rb index 098efa39..19f33b6d 100644 --- a/app/models/bill.rb +++ b/app/models/bill.rb @@ -200,6 +200,7 @@ def downcase_status sig { void } def send_notifications_on_update + Rails.logger.info("Bill.send_notifications_on_update - New Release Date - #{scheduled_release_date_utc} - WAS - #{attribute_before_last_save("scheduled_release_date_utc")}") if updated_scheduled_release_date_utc? SwayPushNotificationService.new( title: "New Bill of the Week", @@ -208,9 +209,7 @@ def send_notifications_on_update end end - # _was - # https://stackoverflow.com/questions/607069/using-activerecord-is-there-a-way-to-get-the-old-values-of-a-record-during-afte def updated_scheduled_release_date_utc? - scheduled_release_date_utc == Time.zone.today && scheduled_release_date_utc_was != Time.zone.today + scheduled_release_date_utc == Time.zone.today && attribute_before_last_save("scheduled_release_date_utc") != Time.zone.today end end diff --git a/app/services/sway_push_notification_service.rb b/app/services/sway_push_notification_service.rb index be94e679..5807cf26 100644 --- a/app/services/sway_push_notification_service.rb +++ b/app/services/sway_push_notification_service.rb @@ -13,6 +13,7 @@ def initialize(subscription = nil, title:, body:) end def send_push_notification + Rails.logger.info("Sending push notifications.") subscriptions.send(iterator) do |sub| sub.send_web_push_notification(message) Rails.logger.info "Sent webpush to - #{sub.endpoint}" unless Rails.env.production? diff --git a/public/service_worker.js b/public/service_worker.js index b99bf8a9..ca63c5e4 100644 --- a/public/service_worker.js +++ b/public/service_worker.js @@ -2,11 +2,11 @@ // https://medium.com/@dejanvu.developer/implementing-web-push-notifications-in-a-ruby-on-rails-application-dcd829e02df0 self.addEventListener("install", (event) => { - console.log("Service worker installed"); + console.log("service_worker installed"); }); self.addEventListener("activate", (event) => { - console.log("Service worker activated"); + console.log("service_worker activated"); }); self.addEventListener("push", function (event) { @@ -23,6 +23,6 @@ self.addEventListener("push", function (event) { }; console.log("service_worker - show notification - notification data:", options); - event.waitUntil(self.registration.showNotification(notificationData.title, options)); + console.log("service_worker - event shown"); });