apns_simple 1.1.1 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/apns_simple/client.rb +22 -19
- data/lib/apns_simple/notification.rb +25 -19
- data/lib/apns_simple/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c1df2a3e93672f23f6ba436092935d6c98a92216
|
4
|
+
data.tar.gz: b930413426fa2205811741e42c08d644e7b2c24d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 910cc4063645c9bde49f41ea840e775299ded66d502c4f9deadc807307b68f2f21f362a703bd6119968de3c3aed20fe6d901c2c3de2bf915c772530ad52b58ec
|
7
|
+
data.tar.gz: b269f36995255422b1c1df4852b3da6044af36500b02c82a6996b45ac6e19b6c53d66273d2fdbc88421eab032b77200537835bae654c7e8b9ff350f1c18edf1d
|
data/lib/apns_simple/client.rb
CHANGED
@@ -46,29 +46,32 @@ module ApnsSimple
|
|
46
46
|
end
|
47
47
|
|
48
48
|
def push(notification)
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
49
|
+
unless notification.error
|
50
|
+
begin
|
51
|
+
sock = TCPSocket.new(host, port)
|
52
|
+
ssl = OpenSSL::SSL::SSLSocket.new(sock, ssl_context)
|
53
|
+
ssl.sync = true
|
54
|
+
ssl.connect
|
55
|
+
ssl.write(notification.payload)
|
54
56
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
57
|
+
if (ready = IO.select([ssl], [], [], TIMEOUT))
|
58
|
+
readable_ssl_socket = ready.first.first
|
59
|
+
if (error = readable_ssl_socket.read(ERROR_BYTES_COUNT))
|
60
|
+
command, code, _index = error.unpack('ccN')
|
61
|
+
notification.error = true
|
62
|
+
if command == COMMAND
|
63
|
+
notification.error_code = code
|
64
|
+
notification.error_message = "CODE: #{code}, DESCRIPTION: #{CODES[code]}"
|
65
|
+
else
|
66
|
+
notification.error_message = "Unknown command received from APNS server: #{command}"
|
67
|
+
end
|
68
|
+
end
|
65
69
|
end
|
70
|
+
ensure
|
71
|
+
ssl.close if ssl
|
72
|
+
sock.close if sock
|
66
73
|
end
|
67
74
|
end
|
68
|
-
|
69
|
-
ensure
|
70
|
-
ssl.close if ssl
|
71
|
-
sock.close if sock
|
72
75
|
end
|
73
76
|
|
74
77
|
private
|
@@ -3,29 +3,35 @@ require 'json'
|
|
3
3
|
module ApnsSimple
|
4
4
|
class Notification
|
5
5
|
|
6
|
-
|
6
|
+
PAYLOAD_MAX_BYTESIZE = 2048
|
7
|
+
|
8
|
+
attr_reader :payload
|
7
9
|
attr_accessor :error, :error_message, :error_code
|
8
10
|
|
9
11
|
def initialize(options, custom_payload = {})
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
12
|
+
token = options.fetch(:token)
|
13
|
+
alert = options[:alert]
|
14
|
+
badge = options[:badge]
|
15
|
+
sound = options[:sound] || 'default'
|
16
|
+
content_available = options[:content_available]
|
17
|
+
|
18
|
+
payload_hash = { aps: {} }
|
19
|
+
payload_hash[:aps][:alert] = alert if alert
|
20
|
+
payload_hash[:aps][:badge] = badge if badge
|
21
|
+
payload_hash[:aps][:sound] = sound if sound
|
22
|
+
payload_hash[:aps]['content-available'] = 1 if content_available
|
23
|
+
payload_hash.merge! custom_payload
|
24
|
+
|
25
|
+
packed_token = [token.gsub(/[<\s>]/,'')].pack('H*')
|
26
|
+
packed_message = payload_hash.to_json.gsub(/\\u([\da-fA-F]{4})/) {|m| [$1].pack("H*").unpack("n*").pack("U*")}
|
27
|
+
payload_size = packed_message.bytesize
|
28
|
+
|
29
|
+
if payload_size > PAYLOAD_MAX_BYTESIZE
|
30
|
+
self.error = true
|
31
|
+
self.error_message = "Payload size is #{payload_size} bytes but maximum #{PAYLOAD_MAX_BYTESIZE} bytes allowed."
|
32
|
+
end
|
17
33
|
|
18
|
-
|
19
|
-
payload = { aps: {} }
|
20
|
-
payload[:aps][:alert] = alert if alert
|
21
|
-
payload[:aps][:badge] = badge if badge
|
22
|
-
payload[:aps][:sound] = sound if sound
|
23
|
-
payload[:aps]['content-available'] = 1 if content_available
|
24
|
-
payload.merge! custom_payload
|
25
|
-
|
26
|
-
packed_message = payload.to_json.gsub(/\\u([\da-fA-F]{4})/) {|m| [$1].pack("H*").unpack("n*").pack("U*")}
|
27
|
-
packed_token = [token.gsub(/[\s|<|>]/,'')].pack('H*')
|
28
|
-
[0, 0, 32, packed_token, 0, packed_message.bytesize, packed_message].pack("ccca*cca*")
|
34
|
+
self.payload = [0, 0, 32, packed_token, 0, payload_size, packed_message].pack("ccca*cca*")
|
29
35
|
end
|
30
36
|
|
31
37
|
end
|
data/lib/apns_simple/version.rb
CHANGED