Get $50+ off for Black Friday!

Notifications

Notifications for Jumpstart Pro are built-in using the Noticed gem and allows delivering notifications across any medium.

Creating Notifications

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.

Notifiers inherit from ApplicationNotifier. Pass --parent AdminNotifier to inherit from another class, or set a default with config.generators { |g| g.notifier parent: "AdminNotifier" } in config/application.rb.

Sending Notifications

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.

ApplicationNotifier

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.

Delivery Methods

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:

  • Database
  • Email
  • ActionCable
  • Slack
  • Twilio
  • Vonage

Use the :action_cable delivery method to broadcast notifications to the navbar immediately. Generated notifications include ActionCable by default, sending the message built by ApplicationNotifier#to_websocket.

class NewCommentNotifier < ApplicationNotifier
  deliver_by :action_cable do |config|
    config.channel = "Noticed::NotificationChannel"
    config.stream = -> { recipient }
    config.message = :to_websocket
  end
end

to_websocket returns a hash with three keys:

  • account_id filters the notification client side to the user's current account. Removing it always shows the notification.
  • html is the notification's row, inserted at the top of the navbar menu.
  • browser is a desktop notification using the browser Notification API. Its title is the notifier's title, or its message when there's no title, and clicking it opens the notification like its navbar row does.

Browser Notifications

Browsers only show desktop notifications once the user has allowed them, and only let a page ask for permission when the user clicks something. The navbar menu has an "Enable browser notifications" button for this, which shows until the user has allowed or blocked notifications. Override browser_notification in a notifier to change what the desktop notification shows, like adding an icon to its options, or return nil to send none.

Icons, Status and Actions

Each notification in the navbar menu and on the notifications page shows an icon, an optional bold title, its message, and the time it arrived. Notifiers fill these in by overriding methods from ApplicationNotifier, which all default to nothing:

  • subject is what the notification is about. A User is drawn as their avatar. To draw your own records, copy notifications/_icon.html.erb into app/views/notifications/ and add them to it.
  • status is "success", "failed" or "warning", drawn as a badge on the icon, or as the icon itself when there's no subject.
  • title is a bold line above the message, like the name of the record.
  • body is the text of the notification. It's the message unless you change it.
  • actions are buttons for a notification that needs something done. They show in the navbar menu until the notification is read, alongside a Dismiss button.
class ExportFailedNotifier < ApplicationNotifier
  deliver_by :action_cable, message: :to_websocket

  def subject = params[:user]
  def status = "failed"
  def title = "Export"

  def message
    "Your export of #{record.name} failed"
  end

  # Without a path, a button opens the notification's url
  def actions
    [
      {label: "Try again", path: export_retries_path(record), method: :post},
      {label: "View details"}
    ]
  end

  def url
    export_path(record)
  end
end

Clicking a notification, one of its actions, or Dismiss marks it read, which hides the actions and the unread dot. The count on the bell is the number of notifications that arrived since the menu was last opened.

Multitenancy

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.