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:notifier NewComment
This will generate app/notifiers/new_comment_notifier.rb
. The Notifier file includes comments to help customize the message, url, and delivery methods.
To deliver a notification to users, run the following:
NewCommentNotifier.with(account: @comment.account, comment: @comment).deliver_later(@comment.post.owner)
This will write the notification to the database and perform any other deliveries that are configured for the Notifier.
Jumpstart Pro ships with an ApplicationNotifier
that notifiers 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:
Use the :action_cable
delivery method to broadcast notifications to the navbar immediately. Generated notifications include ActionCable as an example by default.
class NewComment < ApplicationNotification
deliver_by :action_cable do |config|
config.message = ->(notification) {
{
# Filters notification client side to the user's current account. Removing this always renders the notification.
account_id: notification.account_id,
# Used for inserting the notification into the navbar
html: ApplicationController.render(partial: "notifications/notification", locals: {notification: notification}),
# Used for triggering a native brower Notification API
# https://developer.mozilla.org/en-US/docs/Web/API/notification
browser: {
title: "",
options: {}
}
}
}
end
Native browser notifications will only be shown if the user has granted permission.
Notifiers have been modified to have a belongs_to :account
association. 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
NewCommentNotifier.with(account: @comment.post.account, comment: @comment).deliver(@comment.post.owner)
Warning: Do not include acts_as_tenant
on Noticed models or you won't be able to create notifications for other users correctly.