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 SaganHere are three components that play an important role in our push notification delivery system:
- API method for getting mobile device tokens
- Worker - constantly connected to Apple / Google servers and monitors the queue in Redis
- Code that, in fact, sends messages to the queue
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
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 serversThis 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