Communify

The Communify gem lets your ruby backend send SMS through twilio and push notifications through Firebase cloud messaging while maintaining the log of the same. Also it logs all the emails that are sent through ActionMailer.

Why communify?

Companies can communicate or notify their customers with information about the app, its features, its updates, and other details with their customers using email, push notifications and through SMS.

This stays to be a common mode of communication irrespective of the purpose of the app. So in order to ease the coding part and bring in all the 3 modes of communication together, we have come up with a Rails gem that helps developers include SMS, push notification and email logging with minimal lines of code.

Installation

Add this line to your application's Gemfile:

gem 'communify'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install communify

Installing communify in your code also includes twilio-ruby with version equal to or higher to 5.69.0 and fcm with version equal to or higher than 1.0.8 i.e Communify abstracts the working of these gems

Requirements

For successful functioning of this gem include sidekiq, redis and dotenv-rails gems in your application's gemfile

And keep your sidekiq running, to run locally use

$ bundle exec sidekiq

Usage

For maintainig logs, run the following command in your command line

$ rails g communify:install

This command generates a migration file and a model named CommunifyLog

And then execute:

$ rails db:migrate

This creates a table with fields

  • title -> title of a push notification goes here

  • content -> message/notification text to be sent goes here

  • recipient -> recipient mobile number incase of sms or registration id (generated when an app is installed) incase of push notification goes here

  • priority -> is an enum with options urgent(0 minutes), high(5 minutes), medium(30 minutes), low(60 minutes). When a message is with priority - urgent, the sidekiq job is triggered immediately, in case of high priority - the sidekiq job is triggered in 5 minutes etc., default value: urgent

  • content_type -> this field stores if the log is for sms(0) / push notification(1)

  • status -> stores the status of the message/notification sent if it is successful sent or failured if so then with what error

  • third_party_id -> id generated when a message or notification is successfully sent

  • attempt_count -> is generated automatically and tells the number of attempts taken to send the text. The text is attempted to send utmost 3 times

For SMS

To store Twilio keys in your app, run the following command in your command line

$ rails g communify:sms

This command generates communify_sms.rb file in config/initializers folder of your app

Uncomment the lines and set the account_sid, auth_token and sender_no with the keys provided in your twilio account.

Then add the following lines in your controller method to send sms

Communify::Controllers::Sms.send_message(content, recipient, priority) # returns the resource_id if successful

Note: Content -> text to be sent through SMS

Recipient -> recipient number with country code Example: “+91-1234567890”

Priority -> “urgent” / “high” / “medium” / “low”

Sample Log:

For Push notification

To store device information, run the following command in your command line

$ rails g communify:push_notification

This command generates a migration file and a model in your project folder

And then execute:

$ rails db:migrate

This creates a table named Devices with fields

  • device_id -> registration id goes here

  • device_name -> name of the devices goes here (android/ios/web)

  • person_id, person_type -> user id and class name goes here

Get registration_id/device_id (An ID generated by the FCM SDK for each client app instance) from the frontend and link it with a user and store it in Device table using

Device.create(device_id: "registration_id goes here", device_name: "Name of the device", person: user)

To send and maintain logs of the notification

First store the FCM SERVER KEY in your .env file with variable name FCM_SERVER_KEY

Then to send notification add this line of code in your controller

Communify::Controllers::PushNotification.send_push_notification(title, content, recipient, notification_type, other_details, priority)

Note:

Title -> Title of the notification goes here

Content -> Text to be sent through SMS

Recipient -> Array of registration ids Example: [“reg_id1”,”reg_id2”]

Notification Type -> Type of notification i.e keyword that helps Frontend to know where to redirect the user when the notification is clicked

Other details -> Can be a string or a json converted to a string such that these details are sent to frontend/mobile through the notification but are not visible to the user

Priority -> “urgent” / “high” / “medium” / “low”

  • user refers to the user record

  • registration_id and device_id refers to the same and is used interchangebly

Sample log:

For Maintaining Email logs

To allow storing logs of all emails that are sent, execute the following command,

$ rails g communify:email

This creates a mail_observer.rb file in config/initializers directory. Observers give you access to the email message after it has been sent.

So this helps maintaing the logs in CommunifyLog table where the content, receiver email address and sent timings are stored.

Sample log: