Class: Rapns::Apns::Notification

Inherits:
Notification
  • Object
show all
Defined in:
lib/rapns/apns/notification.rb

Defined Under Namespace

Classes: MultipleAppAssignmentError

Constant Summary collapse

MDM_KEY =
'__rapns_mdm__'
CONTENT_AVAILABLE_KEY =
'__rapns_content_available__'

Instance Method Summary collapse

Methods inherited from Notification

#data, #initialize, #payload, #payload_size

Methods included from MultiJsonHelper

#multi_json_dump, #multi_json_load

Constructor Details

This class inherits a constructor from Rapns::Notification

Instance Method Details

#alertObject



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rapns/apns/notification.rb', line 29

def alert
  string_or_json = read_attribute(:alert)

  if has_attribute?(:alert_is_json)
    if alert_is_json?
      multi_json_load(string_or_json)
    else
      string_or_json
    end
  else
    multi_json_load(string_or_json) rescue string_or_json
  end
end

#alert=(alert) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/rapns/apns/notification.rb', line 19

def alert=(alert)
  if alert.is_a?(Hash)
    write_attribute(:alert, multi_json_dump(alert))
    self.alert_is_json = true if has_attribute?(:alert_is_json)
  else
    write_attribute(:alert, alert)
    self.alert_is_json = false if has_attribute?(:alert_is_json)
  end
end

#as_jsonObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rapns/apns/notification.rb', line 54

def as_json
  json = ActiveSupport::OrderedHash.new

  if attributes_for_device && attributes_for_device.key?(MDM_KEY)
    json['mdm'] = attributes_for_device[MDM_KEY]
  else
    json['aps'] = ActiveSupport::OrderedHash.new
    json['aps']['alert'] = alert if alert
    json['aps']['badge'] = badge if badge
    json['aps']['sound'] = sound if sound

    if attributes_for_device && attributes_for_device[CONTENT_AVAILABLE_KEY]
      json['aps']['content-available'] = 1
    end

    if attributes_for_device
      non_aps_attributes = attributes_for_device.reject { |k, v| k == CONTENT_AVAILABLE_KEY }
      non_aps_attributes.each { |k, v| json[k.to_s] = v }
    end
  end

  json
end

#attributes_for_device=Object



12
# File 'lib/rapns/apns/notification.rb', line 12

alias_method :attributes_for_device=, :data=

#content_available=(bool) ⇒ Object



49
50
51
52
# File 'lib/rapns/apns/notification.rb', line 49

def content_available=(bool)
  return unless bool
  self.attributes_for_device = (attributes_for_device || {}).merge({ CONTENT_AVAILABLE_KEY => true })
end

#data=(attrs) ⇒ Object

Raises:

  • (ArgumentError)


83
84
85
86
87
# File 'lib/rapns/apns/notification.rb', line 83

def data=(attrs)
  return unless attrs
  raise ArgumentError, "must be a Hash" if !attrs.is_a?(Hash)
  super attrs.merge(data || {})
end

#device_token=(token) ⇒ Object



15
16
17
# File 'lib/rapns/apns/notification.rb', line 15

def device_token=(token)
  write_attribute(:device_token, token.delete(" <>")) if !token.nil?
end

#mdm=(magic) ⇒ Object



44
45
46
# File 'lib/rapns/apns/notification.rb', line 44

def mdm=(magic)
  self.attributes_for_device = (attributes_for_device || {}).merge({ MDM_KEY => magic })
end

#to_binary(options = {}) ⇒ Object



78
79
80
81
# File 'lib/rapns/apns/notification.rb', line 78

def to_binary(options = {})
  id_for_pack = options[:for_validation] ? 0 : id
  [1, id_for_pack, expiry, 0, 32, device_token, payload_size, payload].pack("cNNccH*na*")
end