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.



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

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

Instance Attribute Details

#alert ⇒ String Also known as: message



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

#badge ⇒ 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

#channels ⇒ Array



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

#data ⇒ Hash



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_interval ⇒ 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_time ⇒ Parse::Date



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_time ⇒ Parse::Date



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

#query ⇒ Object

Returns the value of attribute query.



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


#sound ⇒ String



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

#title ⇒ String



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



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



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

def as_json(*args)
  payload.as_json
end

#payload ⇒ Hash

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.



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



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



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.



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.



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

def where=(where_clausees)
  query.where where_clauses
end