/ RAILS

Send Push notifications to Android/iOS devices using Parse

This tutorial will help you to send Push notifications to Android and iOS devices from Ruby on Rails application using Parse

Parse

parse

What is Parse?

Parse can be used to increase user engagement with targeted push notifications to mobile devices having Android / iOS running. Push notifications can be sent across devices and platforms with ease. Head over to Parse to get started with Parse

Setting up Android Application

Follow the Android Push Notifications doc to set up your Android Application to use Parse for receiving Push Notifications

Setting up iOS Application

Follow the iOS Push Notifications doc to set up your iOS Application to use Parse for receiving Push Notifications

Rubygem - parse-ruby-client

Parse Ruby Client is Ruby Gem that can be used to Send Push notifications to Android/iOS devices from Ruby

Github: parse-ruby-client

Setup Steps:

1. Obtaining Application ID and API Key

To use Parse with Ruby, you will need application_id and api_key.

  • appliaction_id

    This uniquely identifies your subscription and corresponding devices/configuration under your Application

  • api_key

    This is the REST API key as shown in the image given below If you are Registered with Parse, you can find this under Parse App Keys section as given in the Screenshot given aboveparse-app-keys

2. Install

parse-ruby-client can be installed by

gem install parse-ruby-client

If you are building Ruby on Rails application, then you need to add gem to the Gemfile as,

gem 'parse-ruby-client'

This will install and configure latest Stable version with your Rails Application

3. Initialize Parse

Initialize Parse by passing your applicationid and apikey as given below -

Parse.init application_id: APPLICATION_ID,
api_key: API_KEY,
quiet: false

application_id: is the application_id as given above

api_key: is the REST api key as given above

quiet: Logging Parameter if set equal to true - you will get Parse process logs

4. Send Push Notification

Channels

We can send notifications to Channels subscribed from Android/iOS devices using the code -

data = { alert: "This is a notification from Parse" }
push = Parse::Push.new(data, 'Organization')
push.type = 'android'
# Triggers push notification send
push.save

data - It is a hash that gets delivered to android/iOS device

Organization - This is the channel to which your application should be subscribed of push.type - This sets device type as android / ios as per the need

Individual Target (Advanced Targeting)

We can send notifications to Individuals subscribed from Android/iOS devices using the code -

data = { alert: "This is a notification from Parse" }
push = Parse::Push.new(data)

# push.type = "ios" # NOT NEEDED for Advanced Targeting
# you will find this one in Github parse-ruby-client doc

query = Parse::Query.new(Parse::Protocol::CLASS_INSTALLATION)
query.eq('injuryReports', true)
# here you need to set device type instead
query.eq('deviceType', 'android')
push.where = query.where
# Triggers push notification send
push.save

5. Ruby Implementation

which can be used as a Service or Library in your Ruby on Rails Applications

class ParseNotification

APPLICATION_ID = APP_CONFIG['parse']['application_id'].freeze
API_KEY = APP_CONFIG['parse']['api_key'].freeze
ORGANIZATION_CHANNEL = 'OrganizationName'.freeze
PUSH_TYPE_DEFAULT = 'android'.freeze

attr_accessor :username, :type, :data, :push

# Intialize Parse at the time of Class Load itself
Parse.init application_id: APPLICATION_ID,
api_key: API_KEY,
quiet: false

def initialize(params)
@username = params[:username]
@type = @username.present? ? 'individual' : 'channel'
@data = params[:data]
end

def send
# initialize push object
@push = if individual?
Parse::Push.new(@data)
else
Parse::Push.new(@data, ORGANIZATION_CHANNEL)
end

# setting device type if channel type notification
@push.type = PUSH_TYPE_DEFAULT if channel?
# adding where clause if notification type is individual
@push.where = query.where if individual?
response = @push.save
Rails.logger.info "ParseNotification:Response:#{response.inspect}"
response
end

private

def individual?
type == 'individual'
end

def channel?
type == 'channel'
end

def query
# initialize query object
@query = Parse::Query.new(Parse::Protocol::CLASS_INSTALLATION)
# set query where clause by some attribute
@query.eq('username', @username.to_s)
# setting deviceType in where clause
@query.eq('deviceType', 'android') if @type.individual?
@query
end

end

Above class can be used to Send Notifications to Individual users as well as Channel as given below

Send to Individual -

ParseNotification.new(username: '8793827397', data: {alert: 'This is individual alert from Parse'}).send

Send to Channel/Organization -

ParseNotification.new(data: {alert: 'This is individual alert from Parse'}).send

By using above two types of Push Notification sends you can send push notifications at any time to any device

Handling Response

After push.save, Parse will return response something like -

{"success" => true}

This indicates that your Push Notification is successfully enqueued for sending with Parse service.

Sometimes Push can also send protocol error if it finds some error in your request and returns Exception

Tracking Push Notifications

You can check the status of your Push notifications from Parse dashboard itself. Just visit the Push tab from your Parse account - https://www.parse.com/apps/organization-name/push_notifications

akshay

Akshay Mohite

Hi there! I am a Ruby on Rails & ReactJS Enthusiast, building some cool products at DTree Labs.

Read More
Buy me a coffee