-
Notifications
You must be signed in to change notification settings - Fork 214
feat: Add web push notifications #4112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
google-labs-jules
wants to merge
10
commits into
dev
Choose a base branch
from
feature/web-push-notifications
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d828fd5
feat: Add web push notifications
google-labs-jules[bot] 4883d6b
Merge branch 'dev' into feature/web-push-notifications
CloCkWeRX e599b98
Merge branch 'dev' into feature/web-push-notifications
CloCkWeRX 51b8c2b
Merge branch 'dev' of https://github.com/Growstuff/growstuff into fea…
CloCkWeRX c9a0e22
Update
CloCkWeRX 103e117
Update
CloCkWeRX 6b8d768
Update app/assets/javascripts/push_notifications.js
CloCkWeRX 5d133b0
Update app/assets/javascripts/push_notifications.js
CloCkWeRX 2b6de6d
Update app/assets/javascripts/push_notifications.js
CloCkWeRX 7b677d6
Merge branch 'dev' into feature/web-push-notifications
CloCkWeRX File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,3 +1,4 @@ | ||
| // = link_tree ../images | ||
| // = link serviceworker.js | ||
| // = link_directory ../javascripts .js | ||
| // = link_directory ../stylesheets .css |
This file contains hidden or 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,59 @@ | ||
| // | ||
| // This is a manifest file that'll be compiled into application.js, which will include all the files | ||
| // listed below. | ||
| // | ||
| // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, or any plugin's | ||
| // vendor/assets/javascripts directory can be referenced here using a relative path. | ||
| // | ||
| // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the | ||
| // compiled file. JavaScript code in this file should be added after the last require_* statement. | ||
| // | ||
| // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details | ||
| // about supported directives. | ||
| // | ||
| //= require rails-ujs | ||
| //= require activestorage | ||
| //= require_tree . | ||
|
|
||
| function urlBase64ToUint8Array(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); | ||
|
|
||
| for (let i = 0; i < rawData.length; ++i) { | ||
| outputArray[i] = rawData.charCodeAt(i); | ||
| } | ||
| return outputArray; | ||
| } | ||
|
|
||
| document.addEventListener('turbolinks:load', () => { | ||
| const pushButton = document.getElementById('enable-push-notifications'); | ||
| if (pushButton) { | ||
| pushButton.addEventListener('click', () => { | ||
| if ('serviceWorker' in navigator) { | ||
| navigator.serviceWorker.ready.then(registration => { | ||
| const vapidPublicKey = document.querySelector('meta[name="vapid-public-key"]').content; | ||
| const convertedVapidKey = urlBase64ToUint8Array(vapidPublicKey); | ||
|
|
||
| registration.pushManager.subscribe({ | ||
| userVisibleOnly: true, | ||
| applicationServerKey: convertedVapidKey | ||
| }).then(subscription => { | ||
| fetch('/push_subscriptions', { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| 'X-CSRF-Token': document.querySelector('meta[name="csrf-token"]').content | ||
| }, | ||
| body: JSON.stringify({ subscription: subscription.toJSON() }) | ||
| }); | ||
| }); | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
| }); | ||
This file contains hidden or 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,13 @@ | ||
| self.addEventListener('push', function(event) { | ||
| const data = event.data.json(); | ||
| const title = data.title || 'Growstuff'; | ||
| const options = { | ||
| body: data.body, | ||
| icon: '/assets/growstuff-apple-touch-icon-precomposed.png', | ||
| badge: '/assets/growstuff-apple-touch-icon-precomposed.png' | ||
| }; | ||
|
|
||
| event.waitUntil( | ||
| self.registration.showNotification(title, options) | ||
| ); | ||
| }); |
This file contains hidden or 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,20 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class PushSubscriptionsController < ApplicationController | ||
| before_action :authenticate_member! | ||
|
|
||
| def create | ||
| subscription = current_member.push_subscriptions.find_or_initialize_by(endpoint: params[:subscription][:endpoint]) | ||
| subscription.update( | ||
| p256dh: params[:subscription][:keys][:p256dh], | ||
| auth: params[:subscription][:keys][:auth] | ||
| ) | ||
| head :ok | ||
| end | ||
|
|
||
| def destroy | ||
| subscription = current_member.push_subscriptions.find_by(endpoint: params[:endpoint]) | ||
| subscription&.destroy | ||
| head :ok | ||
| end | ||
| end |
This file contains hidden or 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,35 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class PushNotificationJob < ApplicationJob | ||
| queue_as :default | ||
|
|
||
| def perform(*args) | ||
| Member.where.not(timezone: nil).pluck(:timezone).uniq.each do |timezone| | ||
| Time.use_zone(timezone) do | ||
| if Time.zone.now.hour == 8 | ||
| Member.where(timezone: timezone).each do |member| | ||
| send_planting_notifications(member) | ||
| send_activity_notifications(member) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def send_planting_notifications(member) | ||
| member.plantings.active.annual.each do |planting| | ||
| if planting.finish_is_predicatable? && (planting.late? || planting.super_late?) | ||
| PushNotificationService.new(member, "Your #{planting.crop_name} planting is ready to be marked as finished.").send | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def send_activity_notifications(member) | ||
| due_activities = member.activities.where(due_date: Date.today, finished: false) | ||
| due_activities.each do |activity| | ||
| PushNotificationService.new(member, "Activity due: #{activity.name}").send | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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,5 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class PushSubscription < ApplicationRecord | ||
| belongs_to :member | ||
| end |
This file contains hidden or 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,31 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class PushNotificationService | ||
| def initialize(member, message) | ||
| @member = member | ||
| @message = message | ||
| end | ||
|
|
||
| def send | ||
| @member.push_subscriptions.each do |subscription| | ||
| begin | ||
| WebPush.payload_send( | ||
| message: JSON.generate(title: 'Growstuff', body: @message), | ||
| endpoint: subscription.endpoint, | ||
| p256dh: subscription.p256dh, | ||
| auth: subscription.auth, | ||
| vapid: { | ||
| subject: "mailto:#{ENV.fetch('GROWSTUFF_EMAIL', '[email protected]')}", | ||
| public_key: ENV['GROWSTUFF_VAPID_PUBLIC_KEY'], | ||
| private_key: ENV['GROWSTUFF_VAPID_PRIVATE_KEY'] | ||
| } | ||
| ) | ||
| rescue WebPush::InvalidSubscription => e | ||
| # A subscription can become invalid if the user revokes the permission. | ||
| # In this case, we should delete the subscription. | ||
| subscription.destroy | ||
| Rails.logger.info "Subscription deleted because it was invalid: #{e.message}" | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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 @@ | ||
| .card.mt-3 | ||
| .card-body | ||
| %h5.card-title Notifications | ||
| %p | ||
| Install Growstuff as a Progressive Web App (PWA) to get notifications on your device. | ||
| Look for the "Add to Home Screen" option in your browser's menu. | ||
| %button.btn.btn-primary#enable-push-notifications | ||
| Enable Push Notifications |
This file contains hidden or 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 hidden or 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 hidden or 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,7 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class AddTimezoneToMembers < ActiveRecord::Migration[7.2] | ||
| def change | ||
| add_column :members, :timezone, :string | ||
| end | ||
| end |
This file contains hidden or 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,15 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class CreatePushSubscriptions < ActiveRecord::Migration[7.2] | ||
| def change | ||
| create_table :push_subscriptions do |t| | ||
| t.references :member, null: false, foreign_key: true | ||
| t.string :endpoint, null: false | ||
| t.string :p256dh, null: false | ||
| t.string :auth, null: false | ||
|
|
||
| t.timestamps | ||
| end | ||
| add_index :push_subscriptions, :endpoint, unique: true | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.