Send Push Notifications from rails server to iOS devices.

What is Push Notification ?
Push notification is a feature that allows an app to notify user of new messages or events, even when the user is not actively using your application. Like on Android devices, how a whatsapp message will make a sound and icon appear in the status bar.

What is Apple Push Notification service(APNs) ?
“Apple Push Notification service” is the service for implementing the push notification feature for IOS devices.

How it works ?
Each device establishes encrypted IP connection with the service and receives notifications over this persistent connection. If a notification for an application arrives when that application is not running, the device alerts the user that the application has data waiting for it.

How server originates push notifications for devices ?
Server connects with APNs through a persistent and secure channel while monitoring incoming data intended for their client applications. When new data for an application arrives, the server prepares and sends a notification through the channel to APNs, which pushes the notification to the target device.

How to implement it in Rails App ?
Multiple gems are available to implement Push Notifications in rails app example: APNSpushmeupapn_on_railshouston etc.

We have used APNS gem for our app.

This need APNS certificate, here are the steps to create APNS certificate 

Then we have to convert cert.p12 file to _cert.pem_ file and few configuration to specify _.pem_ file path, APNS host and post.

APNS.host = ‘gateway.push.apple.com’
APNS.pem = ‘/path/to/pem/file’
APNS.port = 2195

To send notification just write:

 APNS.send_notification(device_token, ‘Hello iPhone!’ )

– device_token will be issued by Apple server and we have to save it in DB.

Similarly we can send multiple notification. Click here for multiple notification

Thanks

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s