In this tutorial we explain how to use the pushpad
gem with the noticed
gem to create multi-channel notifications in a Rails application. In particular we'll use the noticed
gem for the notifications in general, while Pushpad will be used to deliver the web push notifications in real time, even when the website is closed.
Pushpad has an official library for Ruby and most Rails applications can simply use that library for sending web push notifications. However it is also possible to use Pushpad together with other delivery methods: website, email, sms, etc. In this complex scenario, where multiple channels are used, a gem like noticed can be useful.
For example, in this tutorial, we want to notify the author of a post of any new comments on his post. We'll first try to notify the author using the website and push notifications, then, after some time, we'll fallback to email if the notifications are not read.
Install and configure the noticed gem
Install the noticed
gem as described in the official documentation.
We'll use this gem for creating the notifications in the database, displaying them in the website and tracking when a notification is read.
Basically you need to add the gem to your application:
gem 'noticed'
Then generate the required files and complete the installation:
rails generate noticed:model
And finally we can generate a model for our notifications:
rails generate noticed:notification CommentNotification
Install and configure the pushpad gem
Install the pushpad
gem as described in the official documentation.
We'll use this gem for sending the push notifications and reaching the user even when the website is closed.
Basically you need to add the gem to your application:
gem 'pushpad'
Then set the authentication credentials (in an initializer):
Pushpad.auth_token = Rails.application.credentials.pushpad_auth_token
Pushpad.project_id = 12345
Integration between the noticed gem and pushpad gem
The noticed
gem can deliver the same notification using different means: database, action cable, email, sms, etc.
Now we are going to add a new delivery method: Pushpad and web push notifications.
Run this command:
rails generate noticed:delivery_method Pushpad
Then open the new file located in the app/notifications/delivery_methods
folder and add this content:
class DeliveryMethods::Pushpad < Noticed::DeliveryMethods::Base
def deliver
Pushpad::Notification.new(format).deliver_to recipient
end
private
def format
notification.send(options[:format])
end
end
Now the noticed
gem can deliver the notifications using this new strategy.
Sending the notifications
Let's finish the CommentNotification
class:
class CommentNotification < Noticed::Base
deliver_by :database
deliver_by :pushpad, class: 'DeliveryMethods::Pushpad', format: :pushpad_format
deliver_by :email, mailer: 'UserMailer', delay: 1.hour, if: :unread?
# Customize notification
# See https://github.com/pushpad/pushpad-ruby#sending-push-notifications
def pushpad_format
comment = params[:comment]
{
title: "#{comment.user.name} replied to your post",
body: comment.body,
target_url: post_url(comment.post)
}
end
end
This class
saves the notification in the database (e.g. to display it in the website), it sends a web push notification using Pushpad, and finally uses the email after some time if the notification is not read.
In this way the author of the post we'll never miss the comments on his post.
Finally in your models or controllers, when a new comment is posted, you can create a notification with this code:
notification = CommentNotification.with(comment: @comment)
notification.deliver_later(@comment.post.author)
Subscribe the users to web push notifications
The above code is complete, however your users are not subscribed to the web push notifications yet.
In order to add the notifications to your website, follow the Getting Started Tutorial.
Then you can use the Pushpad JavaScript SDK to subscribe the users to your notifications.
This command subscribes the browser to the notifications and attaches a user ID (uid) that is used for targeting this specific user with notifications:
pushpad('subscribe', function () {}, { uid: 'myUser123' });
We also recommend to check out this resources for more information:
Conclusion
That's it.
In the above example we have shown a way to notify comments on a post, but the process is similar for any other kind of notification. You just need to create another class (e.g. LikeNotification
, DirectMessageNotification
, FollowNotification
) and add deliver_by :pushpad
.
Now you can easily add multi-channel notifications to any Rails application.