apns 0.9.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -12,7 +12,6 @@ Convert your certificate
12
12
 
13
13
  In Keychain access export your certificate as a p12. Then run the following command to convert it to a .pem
14
14
 
15
-
16
15
  <pre>
17
16
  <code>
18
17
  openssl pkcs12 -in cert.p12 -out cert.pem -nodes -clcerts
@@ -89,20 +88,48 @@ h2. Getting your iPhone's device token
89
88
 
90
89
  After you setup push notification for your application with Apple. You need to ask Apple for you application specific device token.
91
90
 
92
- ApplicationAppDelegate.m
91
+ h3. ApplicationAppDelegate.m
92
+
93
93
  <pre>
94
94
  <code>
95
- - (BOOL)application:(UIApplication *)application
96
- didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
97
- {
98
- [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge)];
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;
99
103
  }
100
-
101
- - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
102
- NSLog(@"deviceToken: %@", deviceToken);
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);
103
112
  }
104
113
  </code>
105
114
  </pre>
106
-
107
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>
108
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.
@@ -8,63 +8,49 @@ module APNS
8
8
  # openssl pkcs12 -in mycert.p12 -out client-cert.pem -nodes -clcerts
9
9
  @pem = nil # this should be the path of the pem file not the contentes
10
10
  @pass = nil
11
-
11
+
12
12
  class << self
13
13
  attr_accessor :host, :pem, :port, :pass
14
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
15
 
20
- ssl.close
21
- sock.close
16
+ def self.send_notification(device_token, message)
17
+ n = APNS::Notification.new(device_token, message)
18
+ self.send_notifications([n])
22
19
  end
23
-
20
+
24
21
  def self.send_notifications(notifications)
25
22
  sock, ssl = self.open_connection
26
-
23
+
27
24
  notifications.each do |n|
28
25
  ssl.write(n.packaged_notification)
29
26
  end
30
-
27
+
31
28
  ssl.close
32
29
  sock.close
33
30
  end
34
-
35
- protected
36
31
 
37
- def self.packaged_notification(device_token, message)
38
- pt = self.packaged_token(device_token)
39
- pm = self.packaged_message(message)
40
- [0, 0, 32, pt, 0, pm.size, pm].pack("ccca*cca*")
41
- end
42
-
43
- def self.packaged_token(device_token)
44
- [device_token.gsub(/[\s|<|>]/,'')].pack('H*')
45
- end
46
-
47
- def self.packaged_message(message)
48
- if message.is_a?(Hash)
49
- apns_from_hash(message)
50
- elsif message.is_a?(String)
51
- '{"aps":{"alert":"'+ message + '"}}'
52
- else
53
- raise "Message needs to be either a hash or string"
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]
54
40
  end
41
+
42
+ ssl.close
43
+ sock.close
44
+
45
+ return apns_feedback
55
46
  end
56
-
57
- def self.apns_from_hash(hash)
58
- other = hash.delete(:other)
59
- aps = {'aps'=> hash }
60
- aps.merge!(other) if other
61
- aps.to_json
62
- end
63
-
47
+
48
+ protected
49
+
64
50
  def self.open_connection
65
51
  raise "The path to your pem file is not set. (APNS.pem = /path/to/cert.pem)" unless self.pem
66
52
  raise "The path to your pem file does not exist!" unless File.exist?(self.pem)
67
-
53
+
68
54
  context = OpenSSL::SSL::SSLContext.new
69
55
  context.cert = OpenSSL::X509::Certificate.new(File.read(self.pem))
70
56
  context.key = OpenSSL::PKey::RSA.new(File.read(self.pem), self.pass)
@@ -75,5 +61,22 @@ module APNS
75
61
 
76
62
  return sock, ssl
77
63
  end
78
-
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
79
82
  end
@@ -19,7 +19,7 @@ module APNS
19
19
  def packaged_notification
20
20
  pt = self.packaged_token
21
21
  pm = self.packaged_message
22
- [0, 0, 32, pt, 0, pm.size, pm].pack("ccca*cca*")
22
+ [0, 0, 32, pt, 0, pm.bytesize, pm].pack("ccca*cca*")
23
23
  end
24
24
 
25
25
  def packaged_token
metadata CHANGED
@@ -1,60 +1,68 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: apns
3
- version: !ruby/object:Gem::Version
4
- version: 0.9.0
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
5
6
  platform: ruby
6
- authors:
7
+ authors:
7
8
  - James Pozdena
8
9
  autorequire: apns
9
10
  bindir: bin
10
11
  cert_chain: []
11
-
12
- date: 2010-03-22 00:00:00 -07:00
13
- default_executable:
14
- dependencies: []
15
-
12
+ date: 2010-03-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
16
30
  description: Simple Apple push notification service gem
17
31
  email: jpoz@jpoz.net
18
32
  executables: []
19
-
20
33
  extensions: []
21
-
22
- extra_rdoc_files:
34
+ extra_rdoc_files:
23
35
  - MIT-LICENSE
24
- files:
36
+ files:
25
37
  - MIT-LICENSE
26
38
  - README.textile
27
39
  - Rakefile
28
40
  - lib/apns/core.rb
29
41
  - lib/apns/notification.rb
30
42
  - lib/apns.rb
31
- has_rdoc: true
32
43
  homepage: http://github.com/jpoz/apns
33
44
  licenses: []
34
-
35
45
  post_install_message:
36
46
  rdoc_options: []
37
-
38
- require_paths:
47
+ require_paths:
39
48
  - 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:
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
52
61
  requirements: []
53
-
54
62
  rubyforge_project:
55
- rubygems_version: 1.3.5
63
+ rubygems_version: 1.8.24
56
64
  signing_key:
57
65
  specification_version: 3
58
66
  summary: Simple Apple push notification service gem
59
67
  test_files: []
60
-
68
+ has_rdoc: