apns 0.0.4 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +32 -8
- data/Rakefile +1 -1
- data/lib/apns.rb +1 -0
- data/lib/apns/core.rb +11 -0
- data/lib/apns/notification.rb +32 -2
- metadata +2 -2
data/README.textile
CHANGED
@@ -4,14 +4,22 @@ a gem for the Apple Push Notification Service.
|
|
4
4
|
|
5
5
|
h2. Install
|
6
6
|
|
7
|
-
gem
|
8
|
-
|
9
|
-
sudo gem install jpoz-apns
|
10
|
-
|
7
|
+
sudo gem install apns
|
11
8
|
|
12
9
|
h2. Setup:
|
13
10
|
|
14
|
-
|
11
|
+
Convert your certificate
|
12
|
+
|
13
|
+
In Keychain access export your certificate as a p12. Then run the following command to convert it to a .pem
|
14
|
+
|
15
|
+
|
16
|
+
<pre>
|
17
|
+
<code>
|
18
|
+
openssl pkcs12 -in cert.p12 -out cert.pem -nodes -clcerts
|
19
|
+
</code>
|
20
|
+
</pre>
|
21
|
+
|
22
|
+
After you have your .pem file. Set what host, port, certificate file location on the APNS class:
|
15
23
|
|
16
24
|
<pre>
|
17
25
|
<code>
|
@@ -19,15 +27,14 @@ Set what host, port, pem file and password on the APNS class:
|
|
19
27
|
# gateway.sandbox.push.apple.com is default
|
20
28
|
|
21
29
|
APNS.pem = '/path/to/pem/file'
|
22
|
-
|
23
|
-
# openssl pkcs12 -in cert.p12 -out cert.pem -nodes -clcerts
|
30
|
+
# this is the file you just created
|
24
31
|
|
25
32
|
APNS.port = 2195
|
26
33
|
# this is also the default. Shouldn't ever have to set this, but just in case Apple goes crazy, you can.
|
27
34
|
</code>
|
28
35
|
</pre>
|
29
36
|
|
30
|
-
h2. Example:
|
37
|
+
h2. Example (Single notification):
|
31
38
|
|
32
39
|
Then to send a push notification you can either just send a string as the alert or give it a hash for the alert, badge and sound.
|
33
40
|
|
@@ -41,6 +48,23 @@ Then to send a push notification you can either just send a string as the alert
|
|
41
48
|
</code>
|
42
49
|
</pre>
|
43
50
|
|
51
|
+
h2. Example (Multiple notifications):
|
52
|
+
|
53
|
+
You can also send multiple notifications using the same connection to Apple:
|
54
|
+
|
55
|
+
<pre>
|
56
|
+
<code>
|
57
|
+
device_token = '123abc456def'
|
58
|
+
|
59
|
+
n1 = APNS::Notification.new(device_token, 'Hello iPhone!' )
|
60
|
+
|
61
|
+
n2 = APNS::Notification.new(device_token, :alert => 'Hello iPhone!', :badge => 1, :sound => 'default')
|
62
|
+
|
63
|
+
APNS.send_notifications([n1, n2])
|
64
|
+
</code>
|
65
|
+
</pre>
|
66
|
+
|
67
|
+
|
44
68
|
h2. Send other info along with aps
|
45
69
|
|
46
70
|
You can send other application specific information as well.
|
data/Rakefile
CHANGED
data/lib/apns.rb
CHANGED
data/lib/apns/core.rb
CHANGED
@@ -21,6 +21,17 @@ module APNS
|
|
21
21
|
sock.close
|
22
22
|
end
|
23
23
|
|
24
|
+
def self.send_notifications(notifications)
|
25
|
+
sock, ssl = self.open_connection
|
26
|
+
|
27
|
+
notifications.each do |n|
|
28
|
+
ssl.write(n.packaged_notification)
|
29
|
+
end
|
30
|
+
|
31
|
+
ssl.close
|
32
|
+
sock.close
|
33
|
+
end
|
34
|
+
|
24
35
|
protected
|
25
36
|
|
26
37
|
def self.packaged_notification(device_token, message)
|
data/lib/apns/notification.rb
CHANGED
@@ -2,8 +2,38 @@ module APNS
|
|
2
2
|
class Notification
|
3
3
|
attr_accessor :device_token, :alert, :badge, :sound, :other
|
4
4
|
|
5
|
-
def
|
6
|
-
|
5
|
+
def initialize(device_token, message)
|
6
|
+
self.device_token = device_token
|
7
|
+
if message.is_a?(Hash)
|
8
|
+
self.alert = message[:alert]
|
9
|
+
self.badge = message[:badge]
|
10
|
+
self.sound = message[:sound]
|
11
|
+
self.other = message[:other]
|
12
|
+
elsif message.is_a?(String)
|
13
|
+
self.alert = message
|
14
|
+
else
|
15
|
+
raise "Notification needs to have either a hash or string"
|
16
|
+
end
|
7
17
|
end
|
18
|
+
|
19
|
+
def packaged_notification
|
20
|
+
pt = self.packaged_token
|
21
|
+
pm = self.packaged_message
|
22
|
+
[0, 0, 32, pt, 0, pm.size, pm].pack("ccca*cca*")
|
23
|
+
end
|
24
|
+
|
25
|
+
def packaged_token
|
26
|
+
[device_token.gsub(/[\s|<|>]/,'')].pack('H*')
|
27
|
+
end
|
28
|
+
|
29
|
+
def packaged_message
|
30
|
+
aps = {'aps'=> {} }
|
31
|
+
aps['aps']['alert'] = self.alert if self.alert
|
32
|
+
aps['aps']['badge'] = self.badge if self.badge
|
33
|
+
aps['aps']['sound'] = self.sound if self.sound
|
34
|
+
aps.merge!(self.other) if self.other
|
35
|
+
aps.to_json
|
36
|
+
end
|
37
|
+
|
8
38
|
end
|
9
39
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: apns
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.9.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Pozdena
|
@@ -9,7 +9,7 @@ autorequire: apns
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-
|
12
|
+
date: 2010-03-22 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|