📜 ⬆️ ⬇️

How to send push notifications from your Rails application

One of the most popular ways to connect a mobile application with a server is to send push notifications to the user. If you have already encountered the implementation of push notifications, then the discovery of America will not happen for you, however, newcomers in this topic have a hard time - this is due to the huge confusion in the information (from the translator: really quite a lot of contradictory, and often completely useless information) . This confusion was the reason for writing this article for WellWithMe, where I will describe the development of the server side of push notifications.



Before I continue, I have to warn about plug-n-play services that provide push notifications if you want to pay them ( Parse , mobDB , Pushwoosh , Urban Airship , etc.), but this is not the way of the warrior. Let's do it yourself, from scratch (and for free) (in fact, not so from scratch, but nobody will have to pay) .
')
If you want to make an apple pie from scratch, then first create the universe
Karl Sagan

Here are three components that play an important role in our push notification delivery system:


First of all, you need to ask the user if he wants to receive push notifications (golden words) and if the user agrees to send a device token (I don’t know how to translate, I think the device token will be clear to everyone) . We will save them (device token) in a simple ActiveRecord model, which we will call Device
# Schema Information # Table name: devices # id :integer not null, primary key # user_id :integer # token :string(255) # enabled :boolean default(TRUE) # created_at :datetime not null # updated_at :datetime not null # platform :string(255) class Device < ActiveRecord::Base attr_accessible :enabled, :token, :user, :platform belongs_to :user validates_uniqueness_of :token, :scope => :user_id end 


Device instances (entries to the database) must be created when the application calls the appropriate API method.

 resource :devices do post do @device = Device.create(user: current_user, token: params[:token], platform: params[:platform]) present @device, with: WellWithMe::Entities::Device end end 


Use the Redis Notification model as a queue

 class NotificationSender @queue = :notifications def self.perform @list = Redis::List.new(Notification.key_name) while notification = @list.pop do notification_json = JSON.parse(notification) if notification_json['platform'] == 'iOS' note = Grocer::Notification.new( device_token: notification_json['token'], alert: notification_json['message'], sound: 'default', badge: 0 ) PUSHER.push(note) elsif notification_json['platform'] == 'Android' gcm = GCM.new(ENV['gcm_key']) registration_id = [notification_json['token']] options = { 'data' => { 'message' => notification_json['message'] }, 'collapse_key' => 'updated_state' } response = gcm.send_notification(registration_id, options) end end end end 


NotificationSender reads tasks from the queue while constantly maintaining a connection to the servers, without trying to establish new connections to send each push notification, which Apple actively impedes: Apple's note about connecting to push notification servers

This code runs every minute, checking the Redis notification queue and sending push notifications to devices.

Great jams grocer for iOS and GCM for Android were used. Both jams work great and are very well documented. The only restriction is that you have to create certificates for iOS and export them correctly - just follow the instructions from Apple.

Now you have an easily expandable push notification system. Push notifications will help to increase the number of your users, but do not abuse them, otherwise the opposite effect will occur.
And the translator will write his crutch almost in the same spirit, only throwing out Redis and adding Windows Phone

Source: https://habr.com/ru/post/214607/


All Articles