wireframe-apn_on_rails 0.3.0.20091216113551 → 0.3.0.20091216122815
Sign up to get free protection for your applications and to get access to all the features.
@@ -38,10 +38,10 @@ module APN # :nodoc:
|
|
38
38
|
|
39
39
|
# Raised when a notification message to Apple is longer than 256 bytes.
|
40
40
|
class ExceededMessageSizeError < StandardError
|
41
|
-
|
41
|
+
MAX_BYTES = 255
|
42
42
|
def initialize(message) # :nodoc:
|
43
|
-
super("The maximum size allowed for a notification payload is
|
44
|
-
@overage = message.size.to_i -
|
43
|
+
super("The maximum size allowed for a notification payload is #{MAX_BYTES} bytes: '#{message}'")
|
44
|
+
@overage = message.size.to_i - MAX_BYTES
|
45
45
|
end
|
46
46
|
def overage
|
47
47
|
@overage
|
@@ -16,7 +16,6 @@
|
|
16
16
|
# so as to not be sent again.
|
17
17
|
class APN::Notification < APN::Base
|
18
18
|
include ::ActionView::Helpers::TextHelper
|
19
|
-
extend ::ActionView::Helpers::TextHelper
|
20
19
|
|
21
20
|
serialize :payload
|
22
21
|
|
@@ -57,11 +56,12 @@ class APN::Notification < APN::Base
|
|
57
56
|
end
|
58
57
|
|
59
58
|
# Creates the binary message needed to send to Apple.
|
59
|
+
# see http://developer.apple.com/IPhone/library/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingWIthAPS/CommunicatingWIthAPS.html#//apple_ref/doc/uid/TP40008194-CH101-SW4
|
60
60
|
def message_for_sending
|
61
61
|
json = self.to_apple_json
|
62
|
-
|
63
|
-
|
64
|
-
|
62
|
+
raise APN::Errors::ExceededMessageSizeError.new(json) if json.size.to_i > APN::Errors::ExceededMessageSizeError::MAX_BYTES
|
63
|
+
|
64
|
+
"\0\0 #{self.device.to_hexa}\0#{(json.length).chr}#{json}"
|
65
65
|
end
|
66
66
|
|
67
67
|
class << self
|
@@ -98,10 +98,13 @@ class APN::Notification < APN::Base
|
|
98
98
|
# Truncate alert message if message payload will be too long
|
99
99
|
def truncate_alert
|
100
100
|
return unless self.alert
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
101
|
+
while self.alert.length > 1
|
102
|
+
begin
|
103
|
+
self.message_for_sending
|
104
|
+
break
|
105
|
+
rescue APN::Errors::ExceededMessageSizeError => e
|
106
|
+
self.alert = truncate(self.alert, :length => self.alert.mb_chars.length - 1)
|
107
|
+
end
|
105
108
|
end
|
106
109
|
end
|
107
110
|
end # APN::Notification
|