Class: PYAPNS::Notification

Inherits:
Object
  • Object
show all
Defined in:
lib/pyapns.rb

Overview

PYAPNS::Notification An APNS Notification

You can construct notification objects ahead of time by using this class. However unnecessary, it allows you to programatically generate a Notification like so:

note = PYAPNS::Notification.new 'alert text', 9, 'flynn.caf', {:extra => 'guid'}

-- or --
note = PYAPNS::Notification.new 'alert text'

These can be passed to PYAPNS::Client#notify the same as hashes

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Notification

Returns a new instance of Notification.



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/pyapns.rb', line 291

def initialize(*args)
  kwargs = [:alert, :badge, :sound]
  extra = nil
  if args.length == 1 && args[0].class == Hash
    args = kwargs.map { |k| args[0][k] }
  end
  @note = {
    :aps => {
      :alert => args[0].nil? ? nil : args[0].to_s,
      :badge => args[1].nil? ? nil : args[1].to_i,
      :sound => args[2].nil? ? nil : args[2].to_s
    }
  }
  if args.length == 4
    @note = @note.merge(args[3] || {})
  end
end

Class Method Details

.aps_attr(*symbols) ⇒ Object



309
310
311
312
313
314
315
316
317
318
# File 'lib/pyapns.rb', line 309

def self.aps_attr(*symbols)
  symbols.each do |sy|
    define_method sy do
      instance_variable_get(:@note)[:aps][sy]
    end
    define_method "#{sy}=".to_sym do |val|
      instance_variable_get(:@note)[:aps][sy] = val
    end
  end
end

.encode(note) ⇒ Object



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/pyapns.rb', line 334

def self.encode note
  ret = {}
  if !note[:aps].nil?
    ret['aps'] = {}
    note[:aps].each do |k, v|
      if !v.nil?
        ret['aps'][k.to_s] = v
      end
    end
  end
  note.keys.find_all { |k| !note[k].nil? && k != :aps }.each do |k|
    ret[k.to_s] = note[k]
  end
  ret
end

Instance Method Details

#encodeObject



330
331
332
# File 'lib/pyapns.rb', line 330

def encode
  PYAPNS::Notification.encode(@note)
end

#extra(key) ⇒ Object



322
323
324
# File 'lib/pyapns.rb', line 322

def extra key
  @note[key]
end

#set_extra(key, val) ⇒ Object



326
327
328
# File 'lib/pyapns.rb', line 326

def set_extra key, val
  @note[key] = val
end