Written by Chris Oliver
When you hear the word “notifications” a lot of different things can come to mind. You might be thinking about:
But really, these are delivery methods for an event that happened in your app.
Let’s say a user comments on a Post you made in an app. We definitely want to store the event in the database to display in the navbar. We can keep track if the event was read or not by storing that in the database. The user will be able to see this immediately if they’re using our website right now.
What if they aren’t on our site? We can send them a notification through another channel like Email, SMS, or Slack. Each of these require different formats, options, and data. Slack needs a channel and some JSON while email needs an address and the HTML content of the email. Plus, users have preferences so they may not want to receive SMS or Email notifications.
Noticed is designed to organize all this. We can define a class for the notification and all the different ways it could be delivered.
class CommentNotification < Noticed::Base deliver_by :database deliver_by :action_cable deliver_by :email, mailer: 'CommentMailer', delay: 5.minutes, if: :email_notifications? # I18n helpers def message t(".message") end # URL helpers are accessible in notifications # Don't forget to set your default_url_options so Rails knows how to generate urls def url post_path(params[:post]) end def email_notifications? !!recipient.preferences[:email] end end
This example shows how we could define a notification when a new comment is posted. We have it write to the database and broadcast to the browser immediately with ActionCable.
After five minutes, we’ll deliver by email if the user has enabled email notifications.
We can also use this class to handle URLs and internationalization. For example, the notification is about a comment, but we want to link to the Post that the comment replied to in our navbar or emails.
To send the notifcation, we simply create it and tell it who to deliver to:
Noticed works just like ActionMailer so you can pass params and decide to deliver the notification immediately or later (in a background job).