Notifications for Jumpstart Pro are built-in using the Noticed gem and allows delivering notifications across any medium.
To generate a new notification, run the following:
rails g noticed:notification NewComment
This will generate the notification as app/notifications/new_comment.rb
. The notification file includes comments to help customzie the notification message, url, and delivery methods.
To deliver a notification to a user, run the following:
Deliver in background (recommended)
NewComment.with(comment: @comment).deliver_later(@comment.post.owner)
Deliver immediately (can be slow)
NewComment.with(comment: @comment).deliver(@comment.post.owner)
This will write the notification to the database and perform any other deliveries that are configured for the notification.
Jumpstart Pro ships with an ApplicationNotification
that notifications inherit from. This parent class is responsible for handling anything that all notifications should have just like ApplicationRecord
does for your models.
Check the Noticed gem's readme to see the full list of delivery methods and how to create your own.
Here are a few of the supported delivery methods:
Notifications using the :database
delivery method will be rendered in the navbar.
Use the :action_cable
delivery method to add notifications to the navbar immediately. Generated notifications include ActionCable as an example by default.
ActionCable notification format
class NewComment < ApplicationNotification
deliver_by :action_cable, format: :to_websocket
def to_websocket
{
# Filters notification client side to the user's current account
# Removing this always renders the notification
account_id: record.account_id,
# Used for inserting the notification into the navbar
html: ApplicationController.render(partial: "notifications/notification", locals: {notification: record}),
# Used for triggering a native brower Notification API
# https://developer.mozilla.org/en-US/docs/Web/API/notification
browser: {
title: "",
options: {}
}
}
end
end
Native browser notifications will only be shown if the user has granted permission.
Notifications have been modified to have an account
association on them. This allows you to send notifications to a specific account for a user if you would like.
By default, ApplicationNotification
writes the recipient's personal account ID but you can override this by passing in account: recipients_account_to_notify
in the notification params. Just make sure the account is one of the recipient's accounts.
Deliver notification to a user's account
NewComment.with(account: @comment.post.account, comment: @comment).deliver_later(@comment.post.owner)
Warning: Do not include acts_as_tenant
on the Notification model or you won't be able to create notifications for other users correctly.