Skip to content

Commit

Permalink
botw-schedule - fix sending notifications when bill updated
Browse files Browse the repository at this point in the history
  • Loading branch information
dcordz committed Nov 15, 2024
1 parent aeb2a64 commit 17cbc5d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
21 changes: 14 additions & 7 deletions app/frontend/pages/Notifications.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -29,7 +29,14 @@ const Notifications: React.FC<IProps> = ({ 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);
}
});
});
Expand Down Expand Up @@ -163,22 +170,22 @@ const Notifications: React.FC<IProps> = ({ user: _user, subscriptions }) => {
if (subscription?.subscribed) {
return (
<div className="col text-center mt-5 vh-50">
<div className="mx-3 my-5">
<div className="mx-3 mt-5 mb-1">
We'll stop sending you a push notification whenever a new Bill of the Week is released.
</div>
<div className="my-5">
<div className="mb-5">
<Button onClick={disableNotifications} variant="outline-danger">
Disable Notifications
</Button>
</div>
<div>
<Button variant="outline-primary" onClick={test}>
Test Notifications
</Button>
<p>
If you don't receive a notification make sure that notifications are permitted for this browser
in your device settings.
</p>
<Button variant="outline-primary" onClick={test}>
Test Notifications
</Button>
</div>
</div>
);
Expand Down
5 changes: 2 additions & 3 deletions app/models/bill.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -208,9 +209,7 @@ def send_notifications_on_update
end
end

# <attribute>_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
1 change: 1 addition & 0 deletions app/services/sway_push_notification_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
6 changes: 3 additions & 3 deletions public/service_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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");
});

0 comments on commit 17cbc5d

Please sign in to comment.