Class: Parse::Push

Inherits:
Object
  • Object
show all
Includes:
Client::Connectable
Defined in:
lib/parse/model/push.rb

Overview

This class represents the API to send push notification to devices that are available in the Installation table. Push notifications are implemented through the `Parse::Push` class. To send push notifications through the REST API, you must enable `REST push enabled?` option in the `Push Notification Settings` section of the `Settings` page in your Parse application. Push notifications targeting uses the Installation Parse class to determine which devices receive the notification. You can provide any query constraint, similar to using `Parse::Query`, in order to target the specific set of devices you want given the columns you have configured in your `Installation` class. The `Parse::Push` class supports many other options not listed here.

Examples:


push = Parse::Push.new
push.send( "Hello World!") # to everyone

# simple channel push
push = Parse::Push.new
push.channels = ["addicted2salsa"]
push.send "You are subscribed to Addicted2Salsa!"

# advanced targeting
push = Parse::Push.new( {..where query constraints..} )
# or use `where()`
push.where :device_type.in => ['ios','android'], :location.near => some_geopoint
push.alert = "Hello World!"
push.sound = "soundfile.caf"

# additional payload data
push.data = { uri: "app://deep_link_path" }

# Send the push
push.send

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Client::Connectable

#client

Constructor Details

#initialize(constraints = {}) ⇒ Push

Initialize a new push notification request.

Parameters:

  • constraints (Hash) (defaults to: {})

    a set of query constraints


85
86
87
# File 'lib/parse/model/push.rb', line 85

def initialize(constraints = {})
  self.where constraints
end

Instance Attribute Details

#alertString Also known as: message

Returns:


71
72
# File 'lib/parse/model/push.rb', line 71

attr_accessor :query, :alert, :badge, :sound, :title, :data,
:expiration_time, :expiration_interval, :push_time, :channels

#badgeInteger

Returns:

  • (Integer)

71
72
# File 'lib/parse/model/push.rb', line 71

attr_accessor :query, :alert, :badge, :sound, :title, :data,
:expiration_time, :expiration_interval, :push_time, :channels

#channelsArray

Returns an array of strings for subscribed channels.

Returns:

  • (Array)

    an array of strings for subscribed channels.


71
72
# File 'lib/parse/model/push.rb', line 71

attr_accessor :query, :alert, :badge, :sound, :title, :data,
:expiration_time, :expiration_interval, :push_time, :channels

#dataHash

Returns specific payload data.

Returns:

  • (Hash)

    specific payload data.


71
72
# File 'lib/parse/model/push.rb', line 71

attr_accessor :query, :alert, :badge, :sound, :title, :data,
:expiration_time, :expiration_interval, :push_time, :channels

#expiration_intervalInteger

Returns:

  • (Integer)

71
72
# File 'lib/parse/model/push.rb', line 71

attr_accessor :query, :alert, :badge, :sound, :title, :data,
:expiration_time, :expiration_interval, :push_time, :channels

#expiration_timeParse::Date

Returns:


71
72
# File 'lib/parse/model/push.rb', line 71

attr_accessor :query, :alert, :badge, :sound, :title, :data,
:expiration_time, :expiration_interval, :push_time, :channels

#push_timeParse::Date

Returns:


71
72
# File 'lib/parse/model/push.rb', line 71

attr_accessor :query, :alert, :badge, :sound, :title, :data,
:expiration_time, :expiration_interval, :push_time, :channels

#queryObject

Returns the value of attribute query.

# File 'lib/parse/model/push.rb', line 47

#soundString

Returns the name of the sound file.

Returns:

  • (String)

    the name of the sound file


71
72
# File 'lib/parse/model/push.rb', line 71

attr_accessor :query, :alert, :badge, :sound, :title, :data,
:expiration_time, :expiration_interval, :push_time, :channels

#titleString

Returns:


71
72
# File 'lib/parse/model/push.rb', line 71

attr_accessor :query, :alert, :badge, :sound, :title, :data,
:expiration_time, :expiration_interval, :push_time, :channels

Class Method Details

.send(payload) ⇒ Object

Send a push notification using a push notification hash

Parameters:

  • payload (Hash)

    a push notification hash payload


79
80
81
# File 'lib/parse/model/push.rb', line 79

def self.send(payload)
  client.push payload.as_json
end

Instance Method Details

#as_json(*args) ⇒ Hash

Returns a JSON encoded hash.

Returns:

  • (Hash)

    a JSON encoded hash.


122
123
124
# File 'lib/parse/model/push.rb', line 122

def as_json(*args)
  payload.as_json
end

#payloadHash

This method takes all the parameters of the instance and creates a proper hash structure, required by Parse, in order to process the push notification.

Returns:

  • (Hash)

    the prepared push payload to be used in the request.


134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/parse/model/push.rb', line 134

def payload
  msg = {
    data: {
      alert: alert,
      badge: badge || "Increment",
    },
  }
  msg[:data][:sound] = sound if sound.present?
  msg[:data][:title] = title if title.present?
  msg[:data].merge! @data if @data.is_a?(Hash)

  if @expiration_time.present?
    msg[:expiration_time] = @expiration_time.respond_to?(:iso8601) ? @expiration_time.iso8601(3) : @expiration_time
  end
  if @push_time.present?
    msg[:push_time] = @push_time.respond_to?(:iso8601) ? @push_time.iso8601(3) : @push_time
  end

  if @expiration_interval.is_a?(Numeric)
    msg[:expiration_interval] = @expiration_interval.to_i
  end

  if query.where.present?
    q = @query.dup
    if @channels.is_a?(Array) && @channels.empty? == false
      q.where :channels.in => @channels
    end
    msg[:where] = q.compile_where unless q.where.empty?
  elsif @channels.is_a?(Array) && @channels.empty? == false
    msg[:channels] = @channels
  end
  msg
end

#send(message = nil) ⇒ Object

helper method to send a message

Parameters:

  • message (String) (defaults to: nil)

    the message to send


170
171
172
173
174
# File 'lib/parse/model/push.rb', line 170

def send(message = nil)
  @alert = message if message.is_a?(String)
  @data = message if message.is_a?(Hash)
  client.push(payload.as_json)
end

#to_json(*args) ⇒ String

Returns a JSON encoded string.

Returns:

  • (String)

    a JSON encoded string.


127
128
129
# File 'lib/parse/model/push.rb', line 127

def to_json(*args)
  as_json.to_json
end

#where(constraints = nil) ⇒ Hash, Parse::Query

Apply a set of constraints.

Parameters:

  • constraints (Hash) (defaults to: nil)

    the set of Query cosntraints

Returns:

  • (Hash)

    if no constraints were passed, returns a compiled query.

  • (Parse::Query)

    if constraints were passed, returns the chainable query.


103
104
105
106
107
# File 'lib/parse/model/push.rb', line 103

def where(constraints = nil)
  return query.compile_where unless constraints.is_a?(Hash)
  query.where constraints
  query
end

#where=(where_clausees) ⇒ Parse::Query

Set a hash of conditions for this push query.

Returns:


95
96
97
# File 'lib/parse/model/push.rb', line 95

def where=(where_clausees)
  query.where where_clauses
end