apns2 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0ac0c780f0cde30860df46c813c72aeb7609384d
4
+ data.tar.gz: b868ddb5134f4413338de61d1798a163d06987a8
5
+ SHA512:
6
+ metadata.gz: 4885e71a0cfef0a0facda6dbf69ebbf56d03451864b1942e0ab93c1d26b4eeb7f2060ed309b76d83af874e7ca3b3a3ccfa54e5ea98758c60ff78264e43735588
7
+ data.tar.gz: 7a356b6994a0c66e9b7675d0f13da6876621078d119d08e03e55df9101ca8a2543b397192d1474119d8507a18d31c82c2826145393f603db1d537d7db7630048
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ /nbproject/
data/MIT-LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2009 James Pozdena, 2015 Jordi Cazorla, Oliver Valls
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.
data/README.textile ADDED
@@ -0,0 +1,135 @@
1
+ h1. APNS
2
+
3
+ A gem for the Apple Push Notification Service, based on apns popular gem (https://github.com/jpoz/APNS) but with optimizations for big batches connection failure tolerance.
4
+
5
+ h2. Install
6
+
7
+ sudo gem install apns
8
+
9
+ h2. Setup:
10
+
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
+ <pre>
16
+ <code>
17
+ openssl pkcs12 -in cert.p12 -out cert.pem -nodes -clcerts
18
+ </code>
19
+ </pre>
20
+
21
+ After you have your .pem file. Set what host, port, certificate file location on the APNS class:
22
+
23
+ <pre>
24
+ <code>
25
+ APNS.host = 'gateway.push.apple.com'
26
+ # gateway.sandbox.push.apple.com is default
27
+
28
+ APNS.pem = '/path/to/pem/file'
29
+ # this is the file you just created
30
+
31
+ APNS.port = 2195
32
+ # this is also the default. Shouldn't ever have to set this, but just in case Apple goes crazy, you can.
33
+ </code>
34
+ </pre>
35
+
36
+ h2. Example (Single notification):
37
+
38
+ 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.
39
+
40
+ <pre>
41
+ <code>
42
+ device_token = '123abc456def'
43
+
44
+ APNS.send_notification(device_token, 'Hello iPhone!' )
45
+
46
+ APNS.send_notification(device_token, :alert => 'Hello iPhone!', :badge => 1, :sound => 'default')
47
+ </code>
48
+ </pre>
49
+
50
+ h2. Example (Multiple notifications):
51
+
52
+ You can also send multiple notifications using the same connection to Apple:
53
+
54
+ <pre>
55
+ <code>
56
+ device_token = '123abc456def'
57
+
58
+ n1 = APNS::Notification.new(device_token, 'Hello iPhone!' )
59
+
60
+ n2 = APNS::Notification.new(device_token, :alert => 'Hello iPhone!', :badge => 1, :sound => 'default')
61
+
62
+ APNS.send_notifications([n1, n2])
63
+ </code>
64
+ </pre>
65
+
66
+
67
+ h2. Send other info along with aps
68
+
69
+ You can send other application specific information as well.
70
+
71
+ <pre>
72
+ <code>
73
+ APNS.send_notification(device_token, :alert => 'Hello iPhone!', :badge => 1, :sound => 'default',
74
+ :other => {:sent => 'with apns gem'})
75
+ </code>
76
+ </pre>
77
+
78
+ This will add the other hash to the same level as the aps hash:
79
+
80
+ <pre>
81
+ <code>
82
+ {"aps":{"alert":"Hello iPhone!","badge":1,"sound":"default"},"sent":"with apns gem"}
83
+ </code>
84
+ </pre>
85
+
86
+
87
+ h2. Getting your iPhone's device token
88
+
89
+ After you setup push notification for your application with Apple. You need to ask Apple for you application specific device token.
90
+
91
+ h3. ApplicationAppDelegate.m
92
+
93
+ <pre>
94
+ <code>
95
+ - (void)applicationDidFinishLaunching:(UIApplication *)application
96
+ {
97
+ // Register with apple that this app will use push notification
98
+ [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert |
99
+ UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge)];
100
+
101
+ // Your app startup logic...
102
+ return YES;
103
+ }
104
+
105
+ - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
106
+ {
107
+ // Convert the binary data token into an NSString (see below for the implementation of this function)
108
+ NSString *deviceTokenAsString = stringFromDeviceTokenData(deviceToken);
109
+
110
+ // Show the device token obtained from apple to the log
111
+ NSLog(@"deviceToken: %@", deviceTokenAsString);
112
+ }
113
+ </code>
114
+ </pre>
115
+
116
+ h3. stringFromDeviceTokenData function
117
+
118
+ This snippet comes from "this stackoverflow post's anwser":http://stackoverflow.com/a/1990880/855846.
119
+ <pre>
120
+ <code>
121
+ NSString* stringFromDeviceTokenData(NSData *deviceToken)
122
+ {
123
+ const char *data = [deviceToken bytes];
124
+ NSMutableString* token = [NSMutableString string];
125
+
126
+ for (int i = 0; i < [deviceToken length]; i++) {
127
+ [token appendFormat:@"%02.2hhX", data[i]];
128
+ }
129
+
130
+ return [[token copy] autorelease];
131
+ }
132
+ </code>
133
+ </pre>
134
+
135
+ For more information on Apple Push Notifications you can see Apple Developer Documentation "here":http://developer.apple.com/library/mac/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html#//apple_ref/doc/uid/TP40008194-CH103-SW2.
data/Rakefile ADDED
@@ -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.9.0'
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
data/apns2.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'apns2'
7
+ spec.version = '0.0.1'
8
+ spec.authors = ['jordi', 'oliver']
9
+ spec.email = ['oliver.vh@coditramuntana.com', 'jordi.cr@coditramuntana.com']
10
+ spec.summary = %q{Optimization to apns gem for big batches.}
11
+ spec.description = %q{Supports failures during big batch sendings.}
12
+ spec.homepage = 'https://github.com/tramuntanal/apns2'
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ['lib']
19
+
20
+ end
data/lib/apns.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'apns/core'
2
+ require 'apns/notification'
data/lib/apns/core.rb ADDED
@@ -0,0 +1,91 @@
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
+ n = APNS::Notification.new(device_token, message)
18
+ self.send_notifications([n])
19
+ end
20
+
21
+ def self.send_notifications(notifications)
22
+ sock, ssl = self.open_connection
23
+
24
+ notifications.each do |n|
25
+ sock, ssl= send_to_apns(sock, ssl, n)
26
+ end
27
+
28
+ ssl.close
29
+ sock.close
30
+ end
31
+
32
+ def self.feedback
33
+ sock, ssl = self.feedback_connection
34
+
35
+ apns_feedback = []
36
+
37
+ while message = ssl.read(38)
38
+ timestamp, token_size, token = message.unpack('N1n1H*')
39
+ apns_feedback << [Time.at(timestamp), token]
40
+ end
41
+
42
+ ssl.close
43
+ sock.close
44
+
45
+ return apns_feedback
46
+ end
47
+
48
+ protected
49
+
50
+ def self.open_connection
51
+ raise "The path to your pem file is not set. (APNS.pem = /path/to/cert.pem)" unless self.pem
52
+ raise "The path to your pem file does not exist!" unless File.exist?(self.pem)
53
+
54
+ context = OpenSSL::SSL::SSLContext.new
55
+ context.cert = OpenSSL::X509::Certificate.new(File.read(self.pem))
56
+ context.key = OpenSSL::PKey::RSA.new(File.read(self.pem), self.pass)
57
+
58
+ sock = TCPSocket.new(self.host, self.port)
59
+ ssl = OpenSSL::SSL::SSLSocket.new(sock,context)
60
+ ssl.connect
61
+
62
+ return sock, ssl
63
+ end
64
+
65
+ def self.feedback_connection
66
+ raise "The path to your pem file is not set. (APNS.pem = /path/to/cert.pem)" unless self.pem
67
+ raise "The path to your pem file does not exist!" unless File.exist?(self.pem)
68
+
69
+ context = OpenSSL::SSL::SSLContext.new
70
+ context.cert = OpenSSL::X509::Certificate.new(File.read(self.pem))
71
+ context.key = OpenSSL::PKey::RSA.new(File.read(self.pem), self.pass)
72
+
73
+ fhost = self.host.gsub('gateway','feedback')
74
+ puts fhost
75
+
76
+ sock = TCPSocket.new(fhost, 2196)
77
+ ssl = OpenSSL::SSL::SSLSocket.new(sock,context)
78
+ ssl.connect
79
+
80
+ return sock, ssl
81
+ end
82
+
83
+ def self.send_to_apns(sock, ssl, n, first_try=true)
84
+ ssl.write(n.packaged_notification)
85
+ rescue Exception => e
86
+ sock, ssl= self.open_connection unless ssl.closed?
87
+ sock, ssl= send_to_apns(sock, ssl, n, false) if first_try
88
+ ensure
89
+ return sock, ssl
90
+ end
91
+ end
@@ -0,0 +1,39 @@
1
+ module APNS
2
+ class Notification
3
+ attr_accessor :device_token, :alert, :badge, :sound, :other
4
+
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
17
+ end
18
+
19
+ def packaged_notification
20
+ pt = self.packaged_token
21
+ pm = self.packaged_message
22
+ [0, 0, 32, pt, 0, pm.bytesize, 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
+
38
+ end
39
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: apns2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - jordi
8
+ - oliver
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-11-06 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Supports failures during big batch sendings.
15
+ email:
16
+ - oliver.vh@coditramuntana.com
17
+ - jordi.cr@coditramuntana.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - ".gitignore"
23
+ - MIT-LICENSE
24
+ - README.textile
25
+ - Rakefile
26
+ - apns2.gemspec
27
+ - lib/apns.rb
28
+ - lib/apns/core.rb
29
+ - lib/apns/notification.rb
30
+ homepage: https://github.com/tramuntanal/apns2
31
+ licenses:
32
+ - MIT
33
+ metadata: {}
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 2.4.5
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: Optimization to apns gem for big batches.
54
+ test_files: []