apns 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2009 James Pozdena
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,84 @@
1
+ h1. APNS
2
+
3
+ a gem for the Apple Push Notification Service.
4
+
5
+ h2. Install
6
+
7
+ gem sources -a http://gems.github.com
8
+
9
+ sudo gem install jpoz-apns
10
+
11
+
12
+ h2. Setup:
13
+
14
+ Set what host, port, pem file and password on the APNS class:
15
+
16
+ <pre>
17
+ <code>
18
+ APNS.host = 'gateway.push.apple.com'
19
+ # gateway.sandbox.push.apple.com is default
20
+
21
+ APNS.pem = '/path/to/pem/file'
22
+ APNS.pass = 'secret'
23
+ # openssl pkcs12 -in cert.p12 -out cert.pem -nodes -clcerts
24
+
25
+ APNS.port = 2195
26
+ # this is also the default. Shouldn't ever have to set this, but just in case Apple goes crazy, you can.
27
+ </code>
28
+ </pre>
29
+
30
+ h2. Example:
31
+
32
+ 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
+
34
+ <pre>
35
+ <code>
36
+ device_token = '123abc456def'
37
+
38
+ APNS.send_notification(device_token, 'Hello iPhone!' )
39
+
40
+ APNS.send_notification(device_token, :alert => 'Hello iPhone!', :badge => 1, :sound => 'default')
41
+ </code>
42
+ </pre>
43
+
44
+ h2. Send other info along with aps
45
+
46
+ You can send other application specific information as well.
47
+
48
+ <pre>
49
+ <code>
50
+ APNS.send_notification(device_token, :alert => 'Hello iPhone!', :badge => 1, :sound => 'default',
51
+ :other => {:sent => 'with apns gem'})
52
+ </code>
53
+ </pre>
54
+
55
+ This will add the other hash to the same level as the aps hash:
56
+
57
+ <pre>
58
+ <code>
59
+ {"aps":{"alert":"Hello iPhone!","badge":1,"sound":"default"},"sent":"with apns gem"}
60
+ </code>
61
+ </pre>
62
+
63
+
64
+ h2. Getting your iPhone's device token
65
+
66
+ After you setup push notification for your application with Apple. You need to ask Apple for you application specific device token.
67
+
68
+ ApplicationAppDelegate.m
69
+ <pre>
70
+ <code>
71
+ - (BOOL)application:(UIApplication *)application
72
+ didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
73
+ {
74
+ [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge)];
75
+ }
76
+
77
+ - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
78
+ NSLog(@"deviceToken: %@", deviceToken);
79
+ }
80
+ </code>
81
+ </pre>
82
+
83
+
84
+
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+ require 'rubygems/specification'
4
+ require 'date'
5
+ require 'spec/rake/spectask'
6
+
7
+ GEM = 'apns'
8
+ GEM_NAME = 'apns'
9
+ GEM_VERSION = '0.0.2'
10
+ AUTHORS = ['James Pozdena']
11
+ EMAIL = "jpoz@jpoz.net"
12
+ HOMEPAGE = "http://github.com/jpoz/apns"
13
+ SUMMARY = "Simple Apple push notification service gem"
14
+
15
+ spec = Gem::Specification.new do |s|
16
+ s.name = GEM
17
+ s.version = GEM_VERSION
18
+ s.platform = Gem::Platform::RUBY
19
+ s.has_rdoc = true
20
+ s.extra_rdoc_files = ["MIT-LICENSE"]
21
+ s.summary = SUMMARY
22
+ s.description = s.summary
23
+ s.authors = AUTHORS
24
+ s.email = EMAIL
25
+ s.homepage = HOMEPAGE
26
+ s.require_path = 'lib'
27
+ s.autorequire = GEM
28
+ s.files = %w(MIT-LICENSE README.textile Rakefile) + Dir.glob("{lib}/**/*")
29
+ end
30
+
31
+ task :default => :spec
32
+
33
+ desc "Run specs"
34
+ Spec::Rake::SpecTask.new do |t|
35
+ t.spec_files = FileList['spec/**/*_spec.rb']
36
+ t.spec_opts = %w(-fs --color)
37
+ end
38
+
39
+ Rake::GemPackageTask.new(spec) do |pkg|
40
+ pkg.gem_spec = spec
41
+ end
42
+
43
+ desc "install the gem locally"
44
+ task :install => [:package] do
45
+ sh %{sudo gem install pkg/#{GEM}-#{GEM_VERSION}}
46
+ end
47
+
48
+ desc "create a gemspec file"
49
+ task :make_spec do
50
+ File.open("#{GEM}.gemspec", "w") do |file|
51
+ file.puts spec.to_ruby
52
+ end
53
+ end
@@ -0,0 +1 @@
1
+ require 'apns/core'
@@ -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
@@ -0,0 +1,9 @@
1
+ module APNS
2
+ class Notification
3
+ attr_accessor :device_token, :alert, :badge, :sound, :other
4
+
5
+ def send_notification
6
+ APNS.send_notification(self.device_token, )
7
+ end
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: apns
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - James Pozdena
8
+ autorequire: apns
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-07-09 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Simple Apple push notification service gem
17
+ email: jpoz@jpoz.net
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - MIT-LICENSE
24
+ files:
25
+ - MIT-LICENSE
26
+ - README.textile
27
+ - Rakefile
28
+ - lib/apns/core.rb
29
+ - lib/apns/notification.rb
30
+ - lib/apns.rb
31
+ has_rdoc: true
32
+ homepage: http://github.com/jpoz/apns
33
+ licenses: []
34
+
35
+ post_install_message:
36
+ rdoc_options: []
37
+
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ requirements: []
53
+
54
+ rubyforge_project:
55
+ rubygems_version: 1.3.5
56
+ signing_key:
57
+ specification_version: 2
58
+ summary: Simple Apple push notification service gem
59
+ test_files: []
60
+