amoslanka / notify

A Rails engine to manage user notifications
MIT License
0 stars 0 forks source link

Notify

You have an application that needs to talk to your users. Rendering emails and sending push notifications isn't the problem, but managing that data gets really messy.

Notify cleans all that up, and lets you focus on what notifications to send and what they say.

Travis CI Circle CI

Notify is a Rails Engine that seeks to manage the data structure of your notification system and gives you a way to define what notifications you send and some rules about how or when they're sent. It doesn't deal with the views, delivery, or logic that says when to send them. It provides notification strategy declarations and manages the data that links your users with things they're notified about.

In most notification systems built for rails applications, the meta information that that system must include can get ugly and complex real fast. The goal of this engine is to keep all that out of sight, but still accessible. Meanwhile your app should only have to declare notification strategies and say when to send them. All those extra rules and configurations are managed from a few single points of contact. The following features are configurable globally, per notification strategy, or per message:

The above features are all configurable options when declaring or sending your notificaitons. Additionally, you have access to the following features:


Some keys to understanding what Notify does:


Setup

First, install the engine.

# Add to Gemfile
gem 'notify-engine'

# Then run
bundle install
rake db:migrate

Second, declare some notification strategies. Use the generator to create one, or copy and paste some simple code into your app's app/notifications directory.

rails generate notify:notification foo

This creates a notification definition in app/notifications using the same name, and you should think of that name as the canonical name of this strategy.

# app/notifications/foo_notification.rb

class WelcomeNotification
  extend Notify::Notification

  self.deliver_via = :action_mailer
  self.visible = true
end

And create a foo mailer to match it. Out of the box, Notify will delivery messages by way of ActionMailer. If you don't override any configurations for your notification, Notify will try to use the mailer at NotificationsMailer#foo.

# app/views/notifications_mailer/foo.html.erb

<h1> Hello foo! </h1>

All you have to do now is send a message to your user.

user = User.first
Notify.send :foo, to: user

Done and done.


More Detail

Global configuration
Generators
Notification
rails g notify:notification foo

Generates a notification strategy file in app/notifications. The name defined here is then used when you create a notification using this configuration.

For example, if you generate a notification named "announcement", you would then create an announcement notification by calling Notify.create :announcement, to: User.all

Adapter
rails g notify:adapter foo

Generates a adapter that will allow you to convert notification delivery data into an action that sends the notification by way of the service it is built for.

For example, if your app sends out push notifications and you have a service object that handles sending a message to a particular device token, generate a adapter named "push" and edit the generated class's deliver method to retrieve the device token from the delivery receiver, render the message of the notification, and send both to your service object for delivery.

To automatically deliver a particular notification through your created "push" adapter, add it to the declaration's deliver_via rule: deliver_via << :push.

Creating a notification strategy
Creating a notification
Helpers for your receiver classes
Deliver to multiple services by defining adapters
Retrieve a receiver's notifications
Mark messages as received by a receiver
Delivery failure and safety mechanisms
Use background queueing to carry out deliveries