Class: ApnClient::Message
- Inherits:
-
Object
- Object
- ApnClient::Message
- Defined in:
- lib/apn_client/message.rb
Instance Attribute Summary collapse
-
#alert ⇒ Object
Returns the value of attribute alert.
-
#badge ⇒ Object
Returns the value of attribute badge.
-
#content_available ⇒ Object
Returns the value of attribute content_available.
-
#custom_properties ⇒ Object
Returns the value of attribute custom_properties.
-
#device_token ⇒ Object
Returns the value of attribute device_token.
-
#message_id ⇒ Object
Returns the value of attribute message_id.
-
#sound ⇒ Object
Returns the value of attribute sound.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other_message) ⇒ Object
-
#initialize(message_id, config = {}) ⇒ Message
constructor
Creates an APN message to to be sent over SSL to the APN service.
-
#payload ⇒ Object
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!”}.
- #payload_hash ⇒ Object
- #payload_size ⇒ Object
-
#to_s ⇒ Object
We use the enhanced format.
Constructor Details
#initialize(message_id, config = {}) ⇒ Message
Creates an APN message to to be sent over SSL to the APN service.
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/apn_client/message.rb', line 13 def initialize(, config = {}) self. = self.device_token = device_token NamedArgs.assert_valid!(config, :optional => [:alert, :badge, :sound, :content_available], :required => [:device_token]) config.keys.each do |key| send("#{key}=", config[key]) end check_payload_size! end |
Instance Attribute Details
#alert ⇒ Object
Returns the value of attribute alert.
3 4 5 |
# File 'lib/apn_client/message.rb', line 3 def alert @alert end |
#badge ⇒ Object
Returns the value of attribute badge.
3 4 5 |
# File 'lib/apn_client/message.rb', line 3 def badge @badge end |
#content_available ⇒ Object
Returns the value of attribute content_available.
3 4 5 |
# File 'lib/apn_client/message.rb', line 3 def content_available @content_available end |
#custom_properties ⇒ Object
Returns the value of attribute custom_properties.
3 4 5 |
# File 'lib/apn_client/message.rb', line 3 def custom_properties @custom_properties end |
#device_token ⇒ Object
Returns the value of attribute device_token.
3 4 5 |
# File 'lib/apn_client/message.rb', line 3 def device_token @device_token end |
#message_id ⇒ Object
Returns the value of attribute message_id.
3 4 5 |
# File 'lib/apn_client/message.rb', line 3 def @message_id end |
#sound ⇒ Object
Returns the value of attribute sound.
3 4 5 |
# File 'lib/apn_client/message.rb', line 3 def sound @sound end |
Class Method Details
.error_codes ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/apn_client/message.rb', line 34 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_at ⇒ Object
49 50 51 52 |
# File 'lib/apn_client/message.rb', line 49 def self.expires_at seconds_per_day = 24*3600 (Time.now + 30*seconds_per_day).to_i end |
Instance Method Details
#==(other_message) ⇒ Object
30 31 32 |
# File 'lib/apn_client/message.rb', line 30 def ==() && .is_a?(self.class) && . == self. end |
#payload ⇒ Object
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!”}
57 58 59 |
# File 'lib/apn_client/message.rb', line 57 def payload payload_hash.to_json end |
#payload_hash ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/apn_client/message.rb', line 61 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_size ⇒ Object
74 75 76 |
# File 'lib/apn_client/message.rb', line 74 def payload_size payload.bytesize end |
#to_s ⇒ Object
We use the enhanced format. See the Apple documentation for details.
26 27 28 |
# File 'lib/apn_client/message.rb', line 26 def to_s [1, , self.class.expires_at, 0, 32, device_token, 0, payload_size, payload].pack('ciiccH*cca*') end |