jpoz-apns 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.textile +31 -28
- data/Rakefile +1 -1
- data/lib/apns/core.rb +68 -0
- data/lib/apns/notification.rb +9 -0
- metadata +5 -2
data/README.textile
CHANGED
@@ -1,53 +1,55 @@
|
|
1
|
-
|
1
|
+
h1. APNS
|
2
2
|
|
3
|
-
|
3
|
+
a gem for the Apple Push Notification Service
|
4
|
+
|
5
|
+
h2. Install
|
4
6
|
|
5
7
|
gem sources -a http://gems.github.com
|
6
8
|
|
7
9
|
sudo gem install jpoz-apns
|
8
10
|
|
9
11
|
|
10
|
-
|
12
|
+
h2. Setup:
|
11
13
|
|
12
14
|
Set what host, port, pem file and password on the APNS class:
|
13
15
|
|
14
16
|
<pre>
|
15
17
|
<code>
|
16
|
-
APNS.host = 'gateway.push.apple.com'
|
17
|
-
# gateway.sandbox.push.apple.com is default
|
18
|
+
APNS.host = 'gateway.push.apple.com'
|
19
|
+
# gateway.sandbox.push.apple.com is default
|
18
20
|
|
19
|
-
APNS.port = 2195
|
20
|
-
# this is also the default. Shouldn't ever have to set this, but just Apple goes crazy, you can.
|
21
|
+
APNS.port = 2195
|
22
|
+
# this is also the default. Shouldn't ever have to set this, but just Apple goes crazy, you can.
|
21
23
|
|
22
|
-
APNS.pem = '/path/to/pem/file'
|
23
|
-
# openssl pkcs12 -in cert.p12 -out cert.pem -nodes -clcerts
|
24
|
+
APNS.pem = '/path/to/pem/file'
|
25
|
+
# openssl pkcs12 -in cert.p12 -out cert.pem -nodes -clcerts
|
24
26
|
|
25
|
-
APNS.pass = 'secret'
|
27
|
+
APNS.pass = 'secret'
|
26
28
|
</code>
|
27
29
|
</pre>
|
28
30
|
|
29
|
-
|
31
|
+
h2. Example:
|
30
32
|
|
31
33
|
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.
|
32
34
|
|
33
35
|
<pre>
|
34
36
|
<code>
|
35
|
-
device_token = '123abc456def'
|
37
|
+
device_token = '123abc456def'
|
36
38
|
|
37
|
-
APNS.send_notification(device_token, 'Hello iPhone!' )
|
39
|
+
APNS.send_notification(device_token, 'Hello iPhone!' )
|
38
40
|
|
39
|
-
APNS.send_notification(device_token, :alert => 'Hello iPhone!', :badge => 1, :sound => 'default')
|
41
|
+
APNS.send_notification(device_token, :alert => 'Hello iPhone!', :badge => 1, :sound => 'default')
|
40
42
|
</code>
|
41
43
|
</pre>
|
42
44
|
|
43
|
-
|
45
|
+
h2. Send other info along with aps
|
44
46
|
|
45
47
|
You can send other application specific information as well.
|
46
48
|
|
47
49
|
<pre>
|
48
50
|
<code>
|
49
|
-
APNS.send_notification(device_token, :alert => 'Hello iPhone!', :badge => 1, :sound => 'default',
|
50
|
-
|
51
|
+
APNS.send_notification(device_token, :alert => 'Hello iPhone!', :badge => 1, :sound => 'default',
|
52
|
+
:other => {:sent => 'with apns gem'})
|
51
53
|
</code>
|
52
54
|
</pre>
|
53
55
|
|
@@ -55,27 +57,28 @@ This will add the other hash to the same level as the aps hash:
|
|
55
57
|
|
56
58
|
<pre>
|
57
59
|
<code>
|
58
|
-
{"aps":{"alert":"Hello iPhone!","badge":1,"sound":"default"},"sent":"with apns gem"}
|
60
|
+
{"aps":{"alert":"Hello iPhone!","badge":1,"sound":"default"},"sent":"with apns gem"}
|
59
61
|
</code>
|
60
62
|
</pre>
|
61
63
|
|
62
64
|
|
63
|
-
|
65
|
+
h2. Getting your iPhone's device token
|
64
66
|
|
65
67
|
After you setup push notification for your application with Apple. You need to ask Apple for you application specific device token.
|
66
68
|
|
67
69
|
ApplicationAppDelegate.m
|
68
70
|
<pre>
|
69
71
|
<code>
|
70
|
-
- (BOOL)application:(UIApplication *)application
|
71
|
-
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
|
72
|
-
{
|
73
|
-
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge)];
|
74
|
-
}
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
72
|
+
- (BOOL)application:(UIApplication *)application
|
73
|
+
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
|
74
|
+
{
|
75
|
+
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge)];
|
76
|
+
}
|
77
|
+
|
78
|
+
|
79
|
+
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
|
80
|
+
NSLog(@"deviceToken: %@", deviceToken);
|
81
|
+
}
|
79
82
|
</code>
|
80
83
|
</pre>
|
81
84
|
|
data/Rakefile
CHANGED
data/lib/apns/core.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
module APNS
|
2
|
+
require 'socket'
|
3
|
+
require 'openssl'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
@host = 'gateway.sandbox.push.apple.com'
|
7
|
+
@port = 2195
|
8
|
+
# openssl pkcs12 -in mycert.p12 -out client-cert.pem -nodes -clcerts
|
9
|
+
@pem = nil # this should be the path of the pem file not the contentes
|
10
|
+
@pass = nil
|
11
|
+
|
12
|
+
class << self
|
13
|
+
attr_accessor :host, :pem, :port, :pass
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.send_notification(device_token, message)
|
17
|
+
sock, ssl = self.open_connection
|
18
|
+
ssl.write(self.packaged_notification(device_token, message))
|
19
|
+
|
20
|
+
ssl.close
|
21
|
+
sock.close
|
22
|
+
end
|
23
|
+
|
24
|
+
protected
|
25
|
+
|
26
|
+
def self.packaged_notification(device_token, message)
|
27
|
+
pt = self.packaged_token(device_token)
|
28
|
+
pm = self.packaged_message(message)
|
29
|
+
[0, 0, 32, pt, 0, pm.size, pm].pack("ccca*cca*")
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.packaged_token(device_token)
|
33
|
+
[device_token.gsub(/[\s|<|>]/,'')].pack('H*')
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.packaged_message(message)
|
37
|
+
if message.is_a?(Hash)
|
38
|
+
apns_from_hash(message)
|
39
|
+
elsif message.is_a?(String)
|
40
|
+
'{"aps":{"alert":"'+ message + '"}}'
|
41
|
+
else
|
42
|
+
raise "Message needs to be either a hash or string"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.apns_from_hash(hash)
|
47
|
+
other = hash.delete(:other)
|
48
|
+
aps = {'aps'=> hash }
|
49
|
+
aps.merge(other) if other
|
50
|
+
JSON.generate(aps)
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.open_connection
|
54
|
+
raise "The path to your pem file is not set. (APNS.pem = /path/to/cert.pem)" unless self.pem
|
55
|
+
raise "The path to your pem file does not exist!" unless File.exist?(self.pem)
|
56
|
+
|
57
|
+
context = OpenSSL::SSL::SSLContext.new
|
58
|
+
context.cert = OpenSSL::X509::Certificate.new(File.read(self.pem))
|
59
|
+
context.key = OpenSSL::PKey::RSA.new(File.read(self.pem), self.pass)
|
60
|
+
|
61
|
+
sock = TCPSocket.new(self.host, self.port)
|
62
|
+
ssl = OpenSSL::SSL::SSLSocket.new(sock,context)
|
63
|
+
ssl.connect
|
64
|
+
|
65
|
+
return sock, ssl
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jpoz-apns
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
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: 2009-07-
|
12
|
+
date: 2009-07-09 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -25,6 +25,9 @@ files:
|
|
25
25
|
- MIT-LICENSE
|
26
26
|
- README.textile
|
27
27
|
- Rakefile
|
28
|
+
- lib/apns
|
29
|
+
- lib/apns/core.rb
|
30
|
+
- lib/apns/notification.rb
|
28
31
|
- lib/apns.rb
|
29
32
|
has_rdoc: true
|
30
33
|
homepage: http://github.com/jpoz/apns
|