Class: ApnClient::Message

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Message

Creates an APN message to to be sent over SSL to the APN service.

Parameters:

  • message_id (Fixnum)

    a unique (at least within a delivery) integer identifier for this message (required)

  • device_token (String)

    A 64 byte long hex digest supplied from an app installed on a device to the server (required)

  • alert (String)

    A text message to display to the user. Should be tweet sized (payload may not exceed 256 bytes)

  • badge (Fixnum)

    the number to show on the badge on the app icon - number of new/unread items

  • sound (String)

    filename of a sound file in the app bundle to be played to the user

  • content_available (Boolean)

    set to true if the message should trigger download of new content



13
14
15
16
17
18
19
# File 'lib/apn_client/message.rb', line 13

def initialize(attributes = {})
  self.attributes = NamedArgs.symbolize_keys!(attributes)
  NamedArgs.assert_valid!(attributes,
    :optional => self.class.optional_attributes,
    :required => self.class.required_attributes)
  check_payload_size!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object

Delegate attribute reading and writing (#attribute_name and #attribute_name=) to the attributes hash.



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/apn_client/message.rb', line 52

def method_missing(method_name, *args)
  method_match, attribute_name, equal_sign = method_name.to_s.match(/\A([^=]+)(=)?\Z/).to_a
  if attribute_name && self.class.valid_attributes.include?(attribute_name.to_sym)
    if equal_sign          
      attributes[attribute_name.to_sym] = args.first
    else
      attributes[attribute_name.to_sym]
    end
  else
    super
  end
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



3
4
5
# File 'lib/apn_client/message.rb', line 3

def attributes
  @attributes
end

Class Method Details

.error_codesObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/apn_client/message.rb', line 30

def self.error_codes
  {
      :no_errors_encountered => 0,
      :processing_error => 1,
      :missing_device_token => 2,
      :missing_topic => 3,
      :missing_payload => 4,
      :invalid_token_size => 5,
      :invalid_topic_size => 6,
      :invalid_payload_size => 7,
      :invalid_token => 8,
      :unknown => 255
  }
end

.expires_atObject



45
46
47
48
# File 'lib/apn_client/message.rb', line 45

def self.expires_at
  seconds_per_day = 24*3600
  (Time.now + 30*seconds_per_day).to_i
end

.optional_attributesObject



65
66
67
# File 'lib/apn_client/message.rb', line 65

def self.optional_attributes
  [:alert, :badge, :sound, :content_available]
end

.required_attributesObject



69
70
71
# File 'lib/apn_client/message.rb', line 69

def self.required_attributes
  [:message_id, :device_token]
end

.valid_attributesObject



73
74
75
# File 'lib/apn_client/message.rb', line 73

def self.valid_attributes
  optional_attributes + required_attributes
end

Instance Method Details

#==(other_message) ⇒ Object



26
27
28
# File 'lib/apn_client/message.rb', line 26

def ==(other_message)
  other_message && other_message.is_a?(self.class) && other_message.message_id == self.message_id
end

#payloadObject

The payload is a JSON formated hash with alert, sound, badge, content-available, and any custom properties, example: => {“badge” => 5, “sound” => “my_sound.aiff”, “alert” => “Hello!”}



80
81
82
# File 'lib/apn_client/message.rb', line 80

def payload
  payload_hash.to_json
end

#payload_hashObject



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/apn_client/message.rb', line 84

def payload_hash
  result = {}
  result['aps'] = {}
  result['aps']['alert'] = alert if alert
  result['aps']['badge'] = badge if badge and badge > 0
  if sound
    result['aps']['sound'] = sound if sound.is_a? String
    result['aps']['sound'] = "1.aiff" if sound.is_a? TrueClass
  end
  result['aps']['content-available'] = 1 if content_available
  result
end

#payload_sizeObject



97
98
99
# File 'lib/apn_client/message.rb', line 97

def payload_size
  payload.bytesize
end

#to_hashObject



101
102
103
# File 'lib/apn_client/message.rb', line 101

def to_hash
  attributes
end

#to_jsonObject



105
106
107
# File 'lib/apn_client/message.rb', line 105

def to_json
  to_hash.to_json
end

#to_sObject

We use the enhanced format. See the Apple documentation for details.



22
23
24
# File 'lib/apn_client/message.rb', line 22

def to_s
  [1, message_id, self.class.expires_at, 0, 32, device_token, 0, payload_size, payload].pack('ciiccH*cca*')
end