Push Notifications for IOS and Android In rails 4
APN's:- Apple Push Notification service (APNs) is a remote notifications feature provided by apple inc. It is a robust and highly efficient service for propagating information to iOS (and, indirectly, watchOS), tvOS, and OS X devices. Each device establishes an accredited and encrypted IP connection with APNs and receives notifications over this persistent connection. If a notification for an app arrives when that app is not running, the device alerts the user that the app has data waiting for it.
How it works:- The device token you provide to the server is analogous to a phone number; it contains information that enables APNs to locate the device on which your client app is installed. APNs also uses it to authenticate the routing of a notification. The device token is provided to you by your client app, which receives the token after registering itself with the remote notification service.
GCM:- Google Cloud Messaging is used to send push notification on android devices. GCM is very usefull tool for sending notification on devices in which your client app is installed.
Push Notifications in RAILS 4:- There are number of gems (like Houston,Rails-push-notoifications,rpush,pushmeup,speedy_gcm etc.) which can help you sending push notifications to ios or android but i will talk about the most efficient and effective gems that i have used while working one of my project.
Step-1 First create a rails project in which you have to use push notification.
rails new [Project_Name]
Step:-2 Install Houston gem to your project.
gem install houston
Step:- 3 Put this code into your model or controller.(For IOS)
Step- 4 Install speedy_gcm gem to your project folder.
def ios_push_notification require 'houston' certificate = File.read("/path/to/apple_push_notification.pem") passphrase = "..." connection = Houston::Connection.new(Houston::APPLE_DEVELOPMENT_GATEWAY_URI, certificate, passphrase) connection.open notification = Houston::Notification.new(device: token) notification.alert = "Hello, World!" notification.badge = 57 notification.sound = "sosumi.aiff" notification.category = "INVITE_CATEGORY" notification.content_available = true notification.custom_data = {foo: "bar"} connection.write(notification.message) connection.close end
Step- 4 Install speedy_gcm gem to your project folder.
gem install speedy_gcm
Step:-5 Put this code into your model or controller.(For Android)
Step:- That's all!! provide all the credentials required and this will work fine.
def android_push_notification require "net/http" require "net/https" message_options = { :registration_ids => [<array of registration ids>], # optional parameters below. Read the docs here: http://developer.android.com/guide/google/gcm/gcm.html#send-msg :collapse_key => "foobar", :data => { :score => "3x1" }, :delay_while_idle => false, :time_to_live => 1 } response = SpeedyGCM::API.send_notification(message_options) end
Step:- That's all!! provide all the credentials required and this will work fine.
Comments
Post a Comment