Permalink
Browse files

change the method to pack apn messages

  • Loading branch information...
1 parent af30c47 commit 07c548f3f6693f751cbcd6a964bcd93fe6f1fc4a @ramigg committed Sep 4, 2012
Showing with 45 additions and 13 deletions.
  1. +45 −13 lib/apn_on_rails/app/models/apn/group_notification.rb
@@ -3,17 +3,17 @@ class APN::GroupNotification < APN::Base
include ::ActionView::Helpers::TextHelper
extend ::ActionView::Helpers::TextHelper
serialize :custom_properties
-
+
belongs_to :group, :class_name => 'APN::Group'
- has_one :app, :class_name => 'APN::App', :through => :group
- has_many :device_groupings, :through => :group
-
+ has_one :app, :class_name => 'APN::App', :through => :group
+ has_many :device_groupings, :through => :group
+
validates_presence_of :group_id
-
+
def devices
self.group.devices
end
-
+
# Stores the text alert message you want to send to the device.
#
# If the message is over 150 characters long it will get truncated
@@ -24,7 +24,7 @@ def alert=(message)
end
write_attribute('alert', message)
end
-
+
# Creates a Hash that will be the payload of an APN.
#
# Example:
@@ -50,13 +50,21 @@ def apple_hash
result['aps']['sound'] = "1.aiff" if self.sound.is_a?(TrueClass)
end
if self.custom_properties
- self.custom_properties.each do |key,value|
+ self.custom_properties.each do |key, value|
result["#{key}"] = "#{value}"
end
end
result
end
-
+
+ def payload
+ multi_json_dump(apple_hash)
+ end
+
+ def payload_size
+ payload.bytesize
+ end
+
# Creates the JSON string required for an APN message.
#
# Example:
@@ -68,13 +76,37 @@ def apple_hash
def to_apple_json
self.apple_hash.to_json
end
-
+
# Creates the binary message needed to send to Apple.
+ #def message_for_sending(device)
+ # json = self.to_apple_json.gsub(/\\u([0-9a-z]{4})/) { |s| [$1.to_i(16)].pack("U") } # This will create non encoded string. Otherwise the string is encoded from utf8 to ascii with unicode representation (i.e. \\u05d2)
+ # message = "\0\0 #{device.to_hexa}\0".force_encoding("UTF-8") + "#{json.length.chr}#{json}".force_encoding("UTF-8")
+ # raise APN::Errors::ExceededMessageSizeError.new(message) if message.size.to_i > 256
+ # message
+ #end
+
+ # This method conforms to the enhanced binary format.
+ # http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingWIthAPS/CommunicatingWIthAPS.html#//apple_ref/doc/uid/TP40008194-CH101-SW4
def message_for_sending(device)
- json = self.to_apple_json.gsub(/\\u([0-9a-z]{4})/) {|s| [$1.to_i(16)].pack("U")} # This will create non encoded string. Otherwise the string is encoded from utf8 to ascii with unicode representation (i.e. \\u05d2)
- message = "\0\0 #{device.to_hexa}\0".force_encoding("UTF-8") + "#{json.length.chr}#{json}".force_encoding("UTF-8")
+ message = [1, device.id, 1.day.to_i, 0, 32, device.token, payload_size, payload].pack("cNNccH*na*")
raise APN::Errors::ExceededMessageSizeError.new(message) if message.size.to_i > 256
message
end
-
+
+
+ private
+
+ def multi_json_load(string, options = {})
+ # Calling load on multi_json less than v1.3.0 attempts to load a file from disk. Check the version explicitly.
+ if Gem.loaded_specs['multi_json'].version >= Gem::Version.create('1.3.0')
+ MultiJson.load(string, options)
+ else
+ MultiJson.decode(string, options)
+ end
+ end
+
+ def multi_json_dump(string, options = {})
+ MultiJson.respond_to?(:dump) ? MultiJson.dump(string, options) : MultiJson.encode(string, options)
+ end
+
end # APN::Notification

0 comments on commit 07c548f

Please sign in to comment.