ja-apns 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2009 James Pozdena, 2013 Yuriy Kharchenko
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,120 @@
1
+ h1. APNS
2
+
3
+ a gem for the Apple Push Notification Service.
4
+
5
+ h2. Install
6
+
7
+ sudo gem install ja-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
+ sender = APNS::Sender.new({
26
+ # gateway.sandbox.push.apple.com is default
27
+ host: 'gateway.push.apple.com',
28
+ # this is the file you just created
29
+ pem: '/path/to/pem/file',
30
+ # this is also the default. Shouldn't ever have to set this, but just in case Apple goes crazy, you can.
31
+ port: 2195
32
+ })
33
+ </code>
34
+ </pre>
35
+
36
+ h2. Example:
37
+
38
+ You will need to create a notification class and provide the following instance methods:
39
+ <code>device_token</code>, <code>alert</code>, <code>badge</code>, <code>sound</code>, <code>other</code>, <code>valid?</code>
40
+
41
+ <pre>
42
+ <code>
43
+ class MyNotification
44
+ def device_token
45
+ 'your token'
46
+ end
47
+ alert
48
+ 'Hello iPhone'
49
+ end
50
+ badge
51
+ 1
52
+ end
53
+ sound
54
+ 'default'
55
+ end
56
+ def other
57
+ {foo: 'bar'}
58
+ end
59
+ def valid?
60
+ true
61
+ end
62
+ end
63
+
64
+ sender = APNS::Sender.new
65
+
66
+ notification = MyNotification.new
67
+
68
+ sender.send_notifications([notification])
69
+ </code>
70
+ </pre>
71
+
72
+ h2. Getting your iPhone's device token
73
+
74
+ After you setup push notification for your application with Apple. You need to ask Apple for you application specific device token.
75
+
76
+ h3. ApplicationAppDelegate.m
77
+
78
+ <pre>
79
+ <code>
80
+ - (void)applicationDidFinishLaunching:(UIApplication *)application
81
+ {
82
+ // Register with apple that this app will use push notification
83
+ [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert |
84
+ UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge)];
85
+
86
+ // Your app startup logic...
87
+ return YES;
88
+ }
89
+
90
+ - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
91
+ {
92
+ // Convert the binary data token into an NSString (see below for the implementation of this function)
93
+ NSString *deviceTokenAsString = stringFromDeviceTokenData(deviceToken);
94
+
95
+ // Show the device token obtained from apple to the log
96
+ NSLog(@"deviceToken: %@", deviceTokenAsString);
97
+ }
98
+ </code>
99
+ </pre>
100
+
101
+ h3. stringFromDeviceTokenData function
102
+
103
+ This snippet comes from "this stackoverflow post's anwser":http://stackoverflow.com/a/1990880/855846.
104
+ <pre>
105
+ <code>
106
+ NSString* stringFromDeviceTokenData(NSData *deviceToken)
107
+ {
108
+ const char *data = [deviceToken bytes];
109
+ NSMutableString* token = [NSMutableString string];
110
+
111
+ for (int i = 0; i < [deviceToken length]; i++) {
112
+ [token appendFormat:@"%02.2hhX", data[i]];
113
+ }
114
+
115
+ return [[token copy] autorelease];
116
+ }
117
+ </code>
118
+ </pre>
119
+
120
+ 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/lib/apns.rb ADDED
@@ -0,0 +1,8 @@
1
+ module APNS
2
+ require 'socket'
3
+ require 'openssl'
4
+ require 'json'
5
+
6
+ require 'apns/packager'
7
+ require 'apns/sender'
8
+ end
@@ -0,0 +1,42 @@
1
+ module APNS
2
+ class Packager
3
+
4
+ attr_reader :notification
5
+
6
+ def initialize(notification)
7
+ @notification = notification
8
+ end
9
+
10
+ def method_missing(meth, *args, &block)
11
+ if %w[device_token alert badge sound other].include?(meth.to_s)
12
+ notification.send(meth, *args, &block)
13
+ else
14
+ super
15
+ end
16
+ end
17
+
18
+ def package
19
+ [0, 0, 32, token, 0, message.bytesize, message].pack('ccca*cca*')
20
+ end
21
+
22
+ private
23
+
24
+ def token
25
+ @token ||= [device_token.gsub(/[\s|<|>]/,'')].pack('H*')
26
+ end
27
+
28
+ def message
29
+ @message ||= pack_message
30
+ end
31
+
32
+ def pack_message
33
+ aps = {'aps'=> {} }
34
+ aps['aps']['alert'] = alert if alert
35
+ aps['aps']['badge'] = badge if badge
36
+ aps['aps']['sound'] = sound if sound
37
+ aps.merge!(notification.other) if other
38
+ aps.to_json
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,44 @@
1
+ module APNS
2
+ class Sender
3
+
4
+ attr_reader :host, :port, :pem, :pass
5
+
6
+ def initialize(params = {})
7
+ @host = params[:host] || 'gateway.sandbox.push.apple.com'
8
+ @port = params[:port] || 2195
9
+ @pem = params[:pem] || raise("Path to your pem file required!")
10
+ @pass = params[:pass]
11
+ end
12
+
13
+ def send_notifications(notifications)
14
+ packages = notifications.select(&:valid?).map { |n| Packager.new(n).package }
15
+ return if packages.none?
16
+ connection do |ssl|
17
+ packages.each { |package| ssl.write(package) }
18
+ end
19
+ true
20
+ end
21
+
22
+ private
23
+
24
+ def connection
25
+ raise "pem file does not exist!" unless File.exist?(self.pem)
26
+
27
+ context = OpenSSL::SSL::SSLContext.new
28
+ context.cert = OpenSSL::X509::Certificate.new(File.read(self.pem))
29
+ context.key = OpenSSL::PKey::RSA.new(File.read(self.pem), self.pass)
30
+
31
+ sock = TCPSocket.new(self.host, self.port)
32
+ ssl = OpenSSL::SSL::SSLSocket.new(sock, context)
33
+ ssl.connect
34
+
35
+ yield(ssl)
36
+
37
+ ssl.close
38
+ sock.close
39
+
40
+ true
41
+ end
42
+
43
+ end
44
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ja-apns
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Yuriy Kharchenko
9
+ - James Pozdena
10
+ autorequire: apns
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-06-26 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: '0'
31
+ description: Simple Apple push notification service gem
32
+ email: yuri.kharchenko@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files:
36
+ - MIT-LICENSE
37
+ files:
38
+ - MIT-LICENSE
39
+ - README.textile
40
+ - Rakefile
41
+ - lib/apns/packager.rb
42
+ - lib/apns/sender.rb
43
+ - lib/apns.rb
44
+ homepage: http://github.com/letmein/ja-apns
45
+ licenses: []
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project:
64
+ rubygems_version: 1.8.25
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: Just another simple Apple push notification service gem
68
+ test_files: []