gs-apns 0.2.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,24 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ gs-apns (0.2.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.2.4)
10
+ rspec (2.13.0)
11
+ rspec-core (~> 2.13.0)
12
+ rspec-expectations (~> 2.13.0)
13
+ rspec-mocks (~> 2.13.0)
14
+ rspec-core (2.13.1)
15
+ rspec-expectations (2.13.0)
16
+ diff-lcs (>= 1.1.3, < 2.0)
17
+ rspec-mocks (2.13.1)
18
+
19
+ PLATFORMS
20
+ ruby
21
+
22
+ DEPENDENCIES
23
+ gs-apns!
24
+ rspec (~> 2.6)
@@ -1,20 +1,36 @@
1
1
  h1. gs-apns
2
2
 
3
- gs-apns is a gem for accessing the Apple Push Notification Service that allows
4
- both sending notifications and reading from apple's feedback service. This gem
5
- is based heavily on the work of James Pozdena (http://github.com/jpoz/APNS) and
6
- "Paul Gebheim" (http://github.com/jpoz/APNS).
3
+ gs-apns is a gem for accessing the Apple Push Notification Service that allows both sending notifications and reading from apple's feedback service. This gem extends on the prior work of "James Pozdena":http://github.com/jpoz/APNS and "Paul Gebheim":http://github.com/jpoz/APNS.
7
4
 
8
- Updates by William Denniss of Geospike: Apple's APNS service does not support JSON that isn't ascii encoded. Prior to rails 3.2.13
9
- it was encoded, but now it isn't.
5
+ Improvements in *gs-apns*, by William Denniss of "Geospike":http://geospike.com/ :
6
+
7
+ # Automatic truncation of messages to fit within the 256 byte limit, and an exception thrown if the server payload is too big (better than dying silently)
8
+ # Truncation can truncate on word or character boundaries, and UTF-8 is fully supported (specifically, Chinese/Japanese character messages are supported)
9
+ # JSON generation uses the JSON gem directly rather than to_json which is overridden by Rails with the potential for "bugs to be introduced":http://omegadelta.net/2013/04/28/changes-to-how-rails-3-2-13-and-4-0-encodes-unicode-in-json/ .
10
+ # Uses Apple's new 'enhanced' notification protocol allowing for an expiration date to be sent on notifications
11
+ # All 256 of available notification payload can be used (jtv-apns & apns are currently limited to 255)
10
12
 
11
13
 
12
14
  h2. Install
13
15
 
14
16
  <pre>
15
- sudo gem install jtv-apns
17
+ sudo gem install gs-apns
18
+ </pre>
19
+
20
+ h3. Gemfile
21
+
22
+ <pre>
23
+ gem "gs-apns", "~> 1.0.0"
24
+ </pre>
25
+
26
+ Bleeding edge:
27
+
28
+ <pre>
29
+ gem 'gs-apns', :git => 'git://github.com/WilliamDenniss/APNS.git'
16
30
  </pre>
17
31
 
32
+
33
+
18
34
  h2. Setup:
19
35
 
20
36
  First, you will need to export your development/production iphone push service
@@ -23,11 +39,12 @@ certificate, and select File -> Export from your keychain. By default a .p12
23
39
  file will be generated containing certificate and key.
24
40
 
25
41
  Next, run the following command to convert this .p12 file into a .pem file.
42
+
26
43
  <pre>
27
44
  openssl pkcs12 -in cert.p12 -out cert.pem -nodes -clcerts
28
45
  </pre>
29
46
 
30
- This pem file should be stored somewhere secure that your application can access. Next, set the jtv-apns configuration parameters:
47
+ This pem file should be stored somewhere secure that your application can access. Next, set the configuration parameters:
31
48
 
32
49
  <pre>
33
50
  ###################
@@ -71,6 +88,31 @@ APNS.pass = 'xxxx'
71
88
 
72
89
  # (default: false)
73
90
  # APNS.cache_connections = true
91
+
92
+ #######################
93
+ # Message trunctation
94
+ #######################
95
+
96
+ # soft (word break), or hard truncation (default: Truncate::TRUNCATE_METHOD_SOFT).
97
+ APNS.truncate_mode = APNS::Truncate::TRUNCATE_METHOD_SOFT
98
+
99
+ # for soft truncation, the maximum number of characters that will be chopped (default: 15)
100
+ APNS.truncate_soft_max_chopped = 15
101
+
102
+ # if true (default: false), replaces all whitespace characters in the message with single spaces.
103
+ # e.g. "\n" becomes " ", and "\n\t\n \n" becomes " ".
104
+ APNS.message_clean_whitespace = false
105
+
106
+ # when a message is truncated, this string is written to the end. Defaults to … (the unicode ellipsis)
107
+ APNS.truncate_ellipsis_str = "\u2026"
108
+
109
+ #######################
110
+ # Logging
111
+ #######################
112
+
113
+ # if true (default: true), will output 1 log message for every notification in a batch, and 1 message for the batch
114
+ APNS.logging = true
115
+
74
116
  </pre>
75
117
 
76
118
  h2. Example (Single notification):
@@ -122,7 +164,7 @@ end
122
164
 
123
165
  h2. Accessing the feedback service
124
166
 
125
- jtv-apns provides a simple api to access Apple's feedback service. Below is an example for setting the feedback time on an ActiveRecord object corresponding to a device token.
167
+ gs-apns provides a simple api to access Apple's feedback service. Below is an example for setting the feedback time on an ActiveRecord object corresponding to a device token.
126
168
 
127
169
  <pre>
128
170
  # APNS.feedback_each returns an array of Hash objects with the following keys
@@ -146,7 +188,7 @@ ApplicationAppDelegate.m
146
188
  - (void)applicationDidFinishLaunching:(UIApplication *)application {
147
189
  // Register with apple that this app will use push notification
148
190
  [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert |
149
- UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge)];
191
+ UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge)];
150
192
  }
151
193
 
152
194
  - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
@@ -155,4 +197,12 @@ ApplicationAppDelegate.m
155
197
  }
156
198
  </pre>
157
199
 
200
+ h2. Test Cases
201
+
202
+ Execute:
203
+
204
+ <pre>
205
+ bundle exec rspec spec
206
+ </pre>
158
207
 
208
+ To run all the rspec test-cases.
@@ -45,13 +45,15 @@ module APNS
45
45
  @connections = {}
46
46
 
47
47
  @truncate_mode = Truncate::TRUNCATE_METHOD_SOFT
48
- @truncate_max_chopped = 10
49
- @clean_whitespace = true
48
+ @truncate_soft_max_chopped = 15
49
+ @truncate_ellipsis_str = "\u2026"
50
+
51
+ @message_clean_whitespace = false
50
52
 
51
53
  @logging = true
52
54
 
53
55
  class << self
54
- attr_accessor :host, :port, :feedback_host, :feedback_port, :pem, :pass, :cache_connections, :clean_whitespace, :truncate_mode, :truncate_max_chopped, :logging
56
+ attr_accessor :host, :port, :feedback_host, :feedback_port, :pem, :pass, :cache_connections, :message_clean_whitespace, :truncate_mode, :truncate_soft_max_chopped, :truncate_ellipsis_str, :logging
55
57
  end
56
58
 
57
59
  def self.establish_notification_connection
@@ -73,6 +75,7 @@ module APNS
73
75
  self.with_notification_connection do |conn|
74
76
  conn.write(self.packaged_notification(device_token, message, notification_id, expiry))
75
77
  conn.flush
78
+ puts "[APNS] send 1 notification package to #{@host}" if @logging
76
79
  end
77
80
  end
78
81
 
@@ -82,6 +85,7 @@ module APNS
82
85
  conn.write(self.packaged_notification(n[0], n[1], (n[2] or rand(9999)), (n[3] or (Time.now + 1.year))))
83
86
  end
84
87
  conn.flush
88
+ puts "[APNS] sent #{notifications.count} notification package(s) to #{@host}" if @logging
85
89
  end
86
90
  end
87
91
 
@@ -139,7 +143,7 @@ module APNS
139
143
  else
140
144
  raise "Message needs to be either a hash or string"
141
145
  end
142
- hash = Truncate.truncate_notification(hash, @clean_whitespace, @truncate_mode, @truncate_max_chopped)
146
+ hash = Truncate.truncate_notification(hash, @message_clean_whitespace, @truncate_mode, @truncate_soft_max_chopped, @truncate_ellipsis_str)
143
147
  ApnsJSON.apns_json(hash)
144
148
  end
145
149
 
@@ -31,7 +31,7 @@ module APNS
31
31
  NOTIFICATION_MAX_BYTE_SIZE = 256
32
32
 
33
33
  # forces a notification to fit within Apple's payload limits by truncating the message as required
34
- def self.truncate_notification(notification, clean_whitespace = true, truncate_mode = TRUNCATE_METHOD_SOFT, truncate_soft_max_chopped = 10)
34
+ def self.truncate_notification(notification, clean_whitespace = true, truncate_mode = TRUNCATE_METHOD_SOFT, truncate_soft_max_chopped = 10, ellipsis = "\u2026")
35
35
 
36
36
  raise ArgumentError, "notification is not a hash" unless notification.is_a?(Hash)
37
37
  raise ArgumentError, "notification hash should contain :aps key" unless notification[:aps]
@@ -54,7 +54,7 @@ module APNS
54
54
  raise TrucateException, "notification would only fit within 256 byte limit by completely truncating the message which changes the presentation in iOS"
55
55
  end
56
56
 
57
- notification[:aps][:alert] = truncate_string(notification[:aps][:alert], message_target_byte_size, truncate_mode, truncate_soft_max_chopped)
57
+ notification[:aps][:alert] = truncate_string(notification[:aps][:alert], message_target_byte_size, truncate_mode, truncate_soft_max_chopped, ellipsis)
58
58
  end
59
59
 
60
60
  return notification
@@ -1,3 +1,3 @@
1
1
  module APNS
2
- VERSION = "0.2.0"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -25,6 +25,8 @@ require 'spec_helper'
25
25
 
26
26
  describe APNS do
27
27
 
28
+ APNS.logging = false
29
+
28
30
  it "should encode unicode to ascii-only json" do
29
31
  string = "\u2601"
30
32
  json = ApnsJSON.apns_json([string])
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gs-apns
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2013-04-28 00:00:00.000000000 Z
14
+ date: 2013-04-29 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rspec
@@ -39,6 +39,7 @@ extra_rdoc_files:
39
39
  files:
40
40
  - .gitignore
41
41
  - Gemfile
42
+ - Gemfile.lock
42
43
  - MIT-LICENSE
43
44
  - README.textile
44
45
  - Rakefile