gwong-apn_on_rails 0.4.2

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.
Files changed (60) hide show
  1. data/.rspec +2 -0
  2. data/.specification +80 -0
  3. data/Gemfile +19 -0
  4. data/Gemfile.lock +47 -0
  5. data/LICENSE +21 -0
  6. data/README +179 -0
  7. data/README.textile +209 -0
  8. data/Rakefile +49 -0
  9. data/VERSION +1 -0
  10. data/apn_on_rails.gemspec +144 -0
  11. data/autotest/discover.rb +1 -0
  12. data/generators/apn_migrations_generator.rb +31 -0
  13. data/generators/templates/apn_migrations/001_create_apn_devices.rb +13 -0
  14. data/generators/templates/apn_migrations/002_create_apn_notifications.rb +23 -0
  15. data/generators/templates/apn_migrations/003_alter_apn_devices.rb +25 -0
  16. data/generators/templates/apn_migrations/004_create_apn_apps.rb +18 -0
  17. data/generators/templates/apn_migrations/005_create_groups.rb +23 -0
  18. data/generators/templates/apn_migrations/006_alter_apn_groups.rb +11 -0
  19. data/generators/templates/apn_migrations/007_create_device_groups.rb +27 -0
  20. data/generators/templates/apn_migrations/008_create_apn_group_notifications.rb +23 -0
  21. data/generators/templates/apn_migrations/009_create_pull_notifications.rb +16 -0
  22. data/generators/templates/apn_migrations/010_alter_apn_notifications.rb +21 -0
  23. data/generators/templates/apn_migrations/011_make_device_token_index_nonunique.rb +11 -0
  24. data/generators/templates/apn_migrations/012_add_launch_notification_to_apn_pull_notifications.rb +9 -0
  25. data/lib/apn_on_rails.rb +4 -0
  26. data/lib/apn_on_rails/apn_on_rails.rb +81 -0
  27. data/lib/apn_on_rails/app/models/apn/app.rb +150 -0
  28. data/lib/apn_on_rails/app/models/apn/base.rb +9 -0
  29. data/lib/apn_on_rails/app/models/apn/device.rb +49 -0
  30. data/lib/apn_on_rails/app/models/apn/device_grouping.rb +16 -0
  31. data/lib/apn_on_rails/app/models/apn/group.rb +12 -0
  32. data/lib/apn_on_rails/app/models/apn/group_notification.rb +79 -0
  33. data/lib/apn_on_rails/app/models/apn/notification.rb +93 -0
  34. data/lib/apn_on_rails/app/models/apn/pull_notification.rb +28 -0
  35. data/lib/apn_on_rails/libs/connection.rb +70 -0
  36. data/lib/apn_on_rails/libs/feedback.rb +39 -0
  37. data/lib/apn_on_rails/tasks/apn.rake +30 -0
  38. data/lib/apn_on_rails/tasks/db.rake +19 -0
  39. data/lib/apn_on_rails_tasks.rb +3 -0
  40. data/spec/active_record/setup_ar.rb +19 -0
  41. data/spec/apn_on_rails/app/models/apn/app_spec.rb +226 -0
  42. data/spec/apn_on_rails/app/models/apn/device_spec.rb +60 -0
  43. data/spec/apn_on_rails/app/models/apn/group_notification_spec.rb +66 -0
  44. data/spec/apn_on_rails/app/models/apn/notification_spec.rb +71 -0
  45. data/spec/apn_on_rails/app/models/apn/pull_notification_spec.rb +100 -0
  46. data/spec/apn_on_rails/libs/connection_spec.rb +40 -0
  47. data/spec/apn_on_rails/libs/feedback_spec.rb +43 -0
  48. data/spec/extensions/string.rb +10 -0
  49. data/spec/factories/app_factory.rb +27 -0
  50. data/spec/factories/device_factory.rb +29 -0
  51. data/spec/factories/device_grouping_factory.rb +22 -0
  52. data/spec/factories/group_factory.rb +27 -0
  53. data/spec/factories/group_notification_factory.rb +22 -0
  54. data/spec/factories/notification_factory.rb +22 -0
  55. data/spec/factories/pull_notification_factory.rb +22 -0
  56. data/spec/fixtures/hexa.bin +1 -0
  57. data/spec/fixtures/message_for_sending.bin +0 -0
  58. data/spec/rails_root/config/apple_push_notification_development.pem +19 -0
  59. data/spec/spec_helper.rb +55 -0
  60. metadata +282 -0
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ -f d
2
+ --colour
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: apn_on_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.1
5
+ platform: ruby
6
+ authors:
7
+ - markbates
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-26 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: configatron
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: "apn_on_rails was developed by: markbates"
26
+ email: mark@markbates.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README
33
+ - LICENSE
34
+ files:
35
+ - lib/apn_on_rails/apn_on_rails.rb
36
+ - lib/apn_on_rails/app/models/apn/base.rb
37
+ - lib/apn_on_rails/app/models/apn/device.rb
38
+ - lib/apn_on_rails/app/models/apn/notification.rb
39
+ - lib/apn_on_rails/libs/connection.rb
40
+ - lib/apn_on_rails/libs/feedback.rb
41
+ - lib/apn_on_rails/tasks/apn.rake
42
+ - lib/apn_on_rails/tasks/db.rake
43
+ - lib/apn_on_rails.rb
44
+ - lib/apn_on_rails_tasks.rb
45
+ - README
46
+ - LICENSE
47
+ - generators/apn_migrations_generator.rb
48
+ - generators/templates/apn_migrations/001_create_apn_devices.rb
49
+ - generators/templates/apn_migrations/002_create_apn_notifications.rb
50
+ - generators/templates/apn_migrations/003_alter_apn_devices.rb
51
+ has_rdoc: true
52
+ homepage: http://www.metabates.com
53
+ licenses: []
54
+
55
+ post_install_message:
56
+ rdoc_options: []
57
+
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: "0"
71
+ version:
72
+ requirements: []
73
+
74
+ rubyforge_project: magrathea
75
+ rubygems_version: 1.3.5
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: apn_on_rails
79
+ test_files: []
80
+
data/Gemfile ADDED
@@ -0,0 +1,19 @@
1
+ source :gemcutter
2
+ gem 'configatron'
3
+
4
+ # Add dependencies required to use your gem here.
5
+ # Example:
6
+ # gem "activesupport", ">= 2.3.5"
7
+
8
+ # Add dependencies to develop your gem here.
9
+ # Include everything needed to run rake, tests, features, etc.
10
+ group :development do
11
+ gem 'autotest'
12
+ gem 'sqlite3-ruby'
13
+ gem "rspec", ">= 2.0.0"
14
+ gem "bundler", "~> 1.0.0"
15
+ gem "jeweler", "~> 1.5.0"
16
+ gem "rcov", ">= 0"
17
+ gem "actionpack", '~> 2.3.0'
18
+ gem 'activerecord', "~> 2.3.0", :require => 'active_record'
19
+ end
@@ -0,0 +1,47 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ ZenTest (4.4.2)
5
+ actionpack (2.3.10)
6
+ activesupport (= 2.3.10)
7
+ rack (~> 1.1.0)
8
+ activerecord (2.3.10)
9
+ activesupport (= 2.3.10)
10
+ activesupport (2.3.10)
11
+ autotest (4.4.6)
12
+ ZenTest (>= 4.4.1)
13
+ configatron (2.6.4)
14
+ yamler (>= 0.1.0)
15
+ diff-lcs (1.1.2)
16
+ git (1.2.5)
17
+ jeweler (1.5.2)
18
+ bundler (~> 1.0.0)
19
+ git (>= 1.2.5)
20
+ rake
21
+ rack (1.1.0)
22
+ rake (0.8.7)
23
+ rcov (0.9.9)
24
+ rspec (2.4.0)
25
+ rspec-core (~> 2.4.0)
26
+ rspec-expectations (~> 2.4.0)
27
+ rspec-mocks (~> 2.4.0)
28
+ rspec-core (2.4.0)
29
+ rspec-expectations (2.4.0)
30
+ diff-lcs (~> 1.1.2)
31
+ rspec-mocks (2.4.0)
32
+ sqlite3-ruby (1.3.2)
33
+ yamler (0.1.0)
34
+
35
+ PLATFORMS
36
+ ruby
37
+
38
+ DEPENDENCIES
39
+ actionpack (~> 2.3.0)
40
+ activerecord (~> 2.3.0)
41
+ autotest
42
+ bundler (~> 1.0.0)
43
+ configatron
44
+ jeweler (~> 1.5.0)
45
+ rcov
46
+ rspec (>= 2.0.0)
47
+ sqlite3-ruby
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2009 markbates
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,179 @@
1
+ =APN on Rails (Apple Push Notifications on Rails)
2
+
3
+ APN on Rails is a Ruby on Rails gem that allows you to easily add Apple Push Notification (iPhone)
4
+ support to your Rails application.
5
+
6
+ It supports:
7
+ * Multiple iPhone apps managed from the same Rails application as well as a legacy default "app" with certs stored in config
8
+ * Individual notifications and group notifications
9
+ * Alerts, badges, sounds, and custom properties in notifications
10
+ * Pull notifications
11
+
12
+ == Feature Descriptions
13
+
14
+ Multiple iPhone Apps: In previous versions of this gem a single Rails application was set up to
15
+ manage push notifications for a single iPhone app. In many cases it is useful to have a single Rails
16
+ app manage push notifications for multiple iPhone apps. With the addition of an APN::App model, this
17
+ is now possible. The certificates are now stored on instances of APN::App and devices are intended to be associated
18
+ with a particular app. For compatibility with existing implementations it is still possible to create devices that
19
+ are not associated with an APN::App and to send individual notifications to them using the certs stored in the
20
+ config directory.
21
+
22
+ Individual and Group Notifications: Previous versions of this gem treated each notification individually
23
+ and did not provide a built-in way to send a broadcast notification to a group of devices. Group notifications
24
+ are now built into the gem. A group notification is associated with a group of devices and shares its
25
+ contents across the entire group of devices. (Group notifications are only available for groups of devices associated
26
+ with an APN::App)
27
+
28
+ Notification Content Areas: Notifications may contain alerts, badges, sounds, and custom properties.
29
+
30
+ Pull Notifications: This version of the gem supports an alternative notification method that relies
31
+ on pulls from client devices and does not interact with the Apple Push Notification servers. This feature
32
+ may be used entirely independently of the push notification features. Pull notifications may be
33
+ created for an app. A client app can query for the most recent pull notification available since a
34
+ given date to retrieve any notifications waiting for it.
35
+
36
+ ==Version 0.4.1 Notes
37
+
38
+ * Backwards compatibility. 0.4.0 required a manual upgrade to associate existing and new devices with an APN::App model. This version allows continued use of devices that are associated with a default "app" that stores its certificates in the config directory. This ought to allow upgrade to this version without code changes.
39
+ * Batched finds. Finds on the APN::Device model that can return large numbers of records have been batched to limit memory impact.
40
+ * Custom properties migration. At a pre-0.4.0 version the custom_properties attribute was added to the migration template that created the notifications table. This introduced a potential problem for gem users who had previously run this migration. The custom_properties alteration to the apn_notifications table has been moved to its own migration and should work regardless of whether your apn_notifications table already has a custom_properties attribute.
41
+ * last_registered_at changed to work intuitively. The last_registered_at attribute of devices was being updated only on creation potentially causing a bug in which a device that opts out of APNs and then opts back in before apn_on_rails received feedback about it might miss a period of APNs that it should receive.
42
+
43
+ ==Acknowledgements:
44
+
45
+ From Mark Bates:
46
+
47
+ This gem is a re-write of a plugin that was written by Fabien Penso and Sam Soffes.
48
+ Their plugin was a great start, but it just didn't quite reach the level I hoped it would.
49
+ I've re-written, as a gem, added a ton of tests, and I would like to think that I made it
50
+ a little nicer and easier to use.
51
+
52
+ From Rebecca Nesson (PRX.org):
53
+
54
+ This gem extends the original version that Mark Bates adapted. His gem did the hard
55
+ work of setting up and handling all communication with the Apple push notification servers.
56
+
57
+ ==Converting Your Certificate:
58
+
59
+ Once you have the certificate from Apple for your application, export your key
60
+ and the apple certificate as p12 files. Here is a quick walkthrough on how to do this:
61
+
62
+ 1. Click the disclosure arrow next to your certificate in Keychain Access and select the certificate and the key.
63
+ 2. Right click and choose `Export 2 items...`.
64
+ 3. Choose the p12 format from the drop down and name it `cert.p12`.
65
+
66
+ Now covert the p12 file to a pem file:
67
+
68
+ $ openssl pkcs12 -in cert.p12 -out apple_push_notification_production.pem -nodes -clcerts
69
+
70
+ If you are using a development certificate, then change the name to apple_push_notification_development.pem instead.
71
+
72
+ Store the contents of the certificate files on the app model for the app you want to send notifications to.
73
+
74
+ ==Installing:
75
+
76
+ ===Stable (RubyForge):
77
+
78
+ $ sudo gem install apn_on_rails
79
+
80
+ ===Edge (GitHub):
81
+
82
+ $ sudo gem install PRX-apn_on_rails.git --source=http://gems.github.com
83
+
84
+ ===Rails Gem Management:
85
+
86
+ If you like to use the built in Rails gem management:
87
+
88
+ config.gem 'apn_on_rails'
89
+
90
+ Or, if you like to live on the edge:
91
+
92
+ config.gem 'PRX-apn_on_rails', :lib => 'apn_on_rails', :source => 'http://gems.github.com'
93
+
94
+ ==Setup and Configuration:
95
+
96
+ Once you have the gem installed via your favorite gem installation, you need to require it so you can
97
+ start to use it:
98
+
99
+ Add the following require, wherever it makes sense to you:
100
+
101
+ require 'apn_on_rails'
102
+
103
+ You also need to add the following to your Rakefile so you can use the
104
+ Rake tasks that ship with APN on Rails:
105
+
106
+ begin
107
+ require 'apn_on_rails_tasks'
108
+ rescue MissingSourceFile => e
109
+ puts e.message
110
+ end
111
+
112
+ Now, to create the tables you need for APN on Rails, run the following task:
113
+
114
+ $ ruby script/generate apn_migrations
115
+
116
+ APN on Rails uses the Configatron gem, http://github.com/markbates/configatron/tree/master,
117
+ to configure itself. (With the change to multi-app support, the certifications are stored in the
118
+ database rather than in the config directory, however, it is still possible to use the default "app" and the certificates
119
+ stored in the config directory. For this setup, the following configurations apply.)
120
+ APN on Rails has the following default configurations that you change as you see fit:
121
+
122
+ # development (delivery):
123
+ configatron.apn.passphrase # => ''
124
+ configatron.apn.port # => 2195
125
+ configatron.apn.host # => 'gateway.sandbox.push.apple.com'
126
+ configatron.apn.cert #=> File.join(RAILS_ROOT, 'config', 'apple_push_notification_development.pem')
127
+
128
+ # production (delivery):
129
+ configatron.apn.host # => 'gateway.push.apple.com'
130
+ configatron.apn.cert #=> File.join(RAILS_ROOT, 'config', 'apple_push_notification_production.pem')
131
+
132
+ # development (feedback):
133
+ configatron.apn.feedback.passphrase # => ''
134
+ configatron.apn.feedback.port # => 2196
135
+ configatron.apn.feedback.host # => 'feedback.sandbox.push.apple.com'
136
+ configatron.apn.feedback.cert #=> File.join(RAILS_ROOT, 'config', 'apple_push_notification_development.pem')
137
+
138
+ # production (feedback):
139
+ configatron.apn.feedback.host # => 'feedback.push.apple.com'
140
+ configatron.apn.feedback.cert #=> File.join(RAILS_ROOT, 'config', 'apple_push_notification_production.pem')
141
+
142
+ That's it, now you're ready to start creating notifications.
143
+
144
+ ===Upgrade Notes:
145
+
146
+ If you are upgrading to a new version of APN on Rails you should always run:
147
+
148
+ $ ruby script/generate apn_migrations
149
+
150
+ That way you ensure you have the latest version of the database tables needed.
151
+
152
+ ==Example (assuming you have created an app and stored your keys on it):
153
+
154
+ $ ./script/console
155
+ >> app = APN::App.create(:name => "My App", :apn_dev_cert => "PASTE YOUR DEV CERT HERE", :apn_prod_cert => "PASTE YOUR PROD CERT HERE")
156
+ >> device = APN::Device.create(:token => "XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX",:app_id => app.id)
157
+ >> notification = APN::Notification.new
158
+ >> notification.device = device
159
+ >> notification.badge = 5
160
+ >> notification.sound = true
161
+ >> notification.alert = "foobar"
162
+ >> notification.custom_properties = {:link => "http://www.prx.org"}
163
+ >> notification.save
164
+
165
+ You can use the following Rake task to deliver your individual notifications:
166
+
167
+ $ rake apn:notifications:deliver
168
+
169
+ And the following task to deliver your group notifications:
170
+
171
+ $ rake apn:group_notifications:deliver
172
+
173
+ The Rake task will find any unsent notifications in the database. If there aren't any notifications
174
+ it will simply do nothing. If there are notifications waiting to be delivered it will open a single connection
175
+ to Apple and push all the notifications through that one connection. Apple does not like people opening/closing
176
+ connections constantly, so it's pretty important that you are careful about batching up your notifications so
177
+ Apple doesn't shut you down.
178
+
179
+ Released under the MIT license.
@@ -0,0 +1,209 @@
1
+ h1. APN on Rails (Apple Push Notifications on Rails)
2
+
3
+ APN on Rails is a Ruby on Rails gem that allows you to easily add Apple Push Notification (iPhone)
4
+ support to your Rails application.
5
+
6
+ It supports:
7
+ * Multiple iPhone apps managed from the same Rails application as well as a legacy default "app" with certs stored in config
8
+ * Individual notifications and group notifications
9
+ * Alerts, badges, sounds, and custom properties in notifications
10
+ * Pull notifications
11
+
12
+ h2. Feature Descriptions
13
+
14
+ Multiple iPhone Apps: In previous versions of this gem a single Rails application was set up to
15
+ manage push notifications for a single iPhone app. In many cases it is useful to have a single Rails
16
+ app manage push notifications for multiple iPhone apps. With the addition of an APN::App model, this
17
+ is now possible. The certificates are now stored on instances of APN::App and all devices are intended to be associated
18
+ with a particular app. For compatibility with existing implementations it is still possible to create devices that
19
+ are not associated with an APN::App and to send individual notifications to them using the certs stored in the
20
+ config directory.
21
+
22
+ Individual and Group Notifications: Previous versions of this gem treated each notification individually
23
+ and did not provide a built-in way to send a broadcast notification to a group of devices. Group notifications
24
+ are now built into the gem. A group notification is associated with a group of devices and shares its
25
+ contents across the entire group of devices. (Group notifications are only available for groups of devices associated
26
+ with an APN::App)
27
+
28
+ Notification Content Areas: Notifications may contain alerts, badges, sounds, and custom properties.
29
+
30
+ Pull Notifications: This version of the gem supports an alternative notification method that relies
31
+ on pulls from client devices and does not interact with the Apple Push Notification servers. This feature
32
+ may be used entirely independently of the push notification features. Pull notifications may be
33
+ created for an app. A client app can query for the most recent pull notification available since a
34
+ given date to retrieve any notifications waiting for it.
35
+
36
+ h2. Version 0.4.1 Notes
37
+
38
+ * Backwards compatibility. 0.4.0 required a manual upgrade to associate existing and new devices with an APN::App model. This version allows continued use of devices that are associated with a default "app" that stores its certificates in the config directory. This ought to allow upgrade to this version without code changes.
39
+ * Batched finds. Finds on the APN::Device model that can return large numbers of records have been batched to limit memory impact.
40
+ * Custom properties migration. At a pre-0.4.0 version the custom_properties attribute was added to the migration template that created the notifications table. This introduced a potential problem for gem users who had previously run this migration. The custom_properties alteration to the apn_notifications table has been moved to its own migration and should work regardless of whether your apn_notifications table already has a custom_properties attribute.
41
+ * last_registered_at changed to work intuitively. The last_registered_at attribute of devices was being updated only on creation potentially causing a bug in which a device that opts out of APNs and then opts back in before apn_on_rails received feedback about it might miss a period of APNs that it should receive.
42
+
43
+ h2. Acknowledgements:
44
+
45
+ From Mark Bates:
46
+
47
+ This gem is a re-write of a plugin that was written by Fabien Penso and Sam Soffes.
48
+ Their plugin was a great start, but it just didn't quite reach the level I hoped it would.
49
+ I've re-written, as a gem, added a ton of tests, and I would like to think that I made it
50
+ a little nicer and easier to use.
51
+
52
+ From Rebecca Nesson (PRX.org):
53
+
54
+ This gem extends the original version that Mark Bates adapted. His gem did the hard
55
+ work of setting up and handling all communication with the Apple push notification servers.
56
+
57
+ h2. Converting Your Certificate:
58
+
59
+ Once you have the certificate from Apple for your application, export your key
60
+ and the apple certificate as p12 files. Here is a quick walkthrough on how to do this:
61
+
62
+ 1. Click the disclosure arrow next to your certificate in Keychain Access and select the certificate and the key.
63
+ 2. Right click and choose `Export 2 items...`.
64
+ 3. Choose the p12 format from the drop down and name it `cert.p12`.
65
+
66
+ Now covert the p12 file to a pem file:
67
+
68
+ <pre><code>
69
+ $ openssl pkcs12 -in cert.p12 -out apple_push_notification_production.pem -nodes -clcerts
70
+ </pre></code>
71
+
72
+ If you are using a development certificate, then change the name to apple_push_notification_development.pem instead.
73
+
74
+ Store the contents of the certificate files on the app model for the app you want to send notifications to.
75
+
76
+ h2. Installing:
77
+
78
+ h3. Stable (RubyForge):
79
+
80
+ <pre><code>
81
+ $ sudo gem install apn_on_rails
82
+ </pre></code>
83
+
84
+ h3. Edge (GitHub):
85
+
86
+ <pre><code>
87
+ $ sudo gem install PRX-apn_on_rails.git --source=http://gems.github.com
88
+ </pre></code>
89
+
90
+ h3. Rails Gem Management:
91
+
92
+ If you like to use the built in Rails gem management:
93
+
94
+ <pre><code>
95
+ config.gem 'apn_on_rails'
96
+ </pre></code>
97
+
98
+ Or, if you like to live on the edge:
99
+
100
+ <pre><code>
101
+ config.gem 'PRX-apn_on_rails', :lib => 'apn_on_rails', :source => 'http://gems.github.com'
102
+ </pre></code>
103
+
104
+ h2. Setup and Configuration:
105
+
106
+ Once you have the gem installed via your favorite gem installation, you need to require it so you can
107
+ start to use it:
108
+
109
+ Add the following require, wherever it makes sense to you:
110
+
111
+ <pre><code>
112
+ require 'apn_on_rails'
113
+ </pre></code>
114
+
115
+ You also need to add the following to your Rakefile so you can use the
116
+ Rake tasks that ship with APN on Rails:
117
+
118
+ <pre><code>
119
+ begin
120
+ require 'apn_on_rails_tasks'
121
+ rescue MissingSourceFile => e
122
+ puts e.message
123
+ end
124
+ </pre></code>
125
+
126
+ Now, to create the tables you need for APN on Rails, run the following task:
127
+
128
+ <pre><code>
129
+ $ ruby script/generate apn_migrations
130
+ </pre></code>
131
+
132
+ APN on Rails uses the Configatron gem, http://github.com/markbates/configatron/tree/master,
133
+ to configure itself. (With the change to multi-app support, the certifications are stored in the
134
+ database rather than in the config directory, however, it is still possible to use the default "app" and the certificates
135
+ stored in the config directory. For this setup, the following configurations apply.)
136
+ APN on Rails has the following default configurations that you change as you
137
+ see fit:
138
+
139
+ <pre><code>
140
+ # development (delivery):
141
+ configatron.apn.passphrase # => ''
142
+ configatron.apn.port # => 2195
143
+ configatron.apn.host # => 'gateway.sandbox.push.apple.com'
144
+ configatron.apn.cert #=> File.join(RAILS_ROOT, 'config', 'apple_push_notification_development.pem')
145
+
146
+ # production (delivery):
147
+ configatron.apn.host # => 'gateway.push.apple.com'
148
+ configatron.apn.cert #=> File.join(RAILS_ROOT, 'config', 'apple_push_notification_production.pem')
149
+
150
+ # development (feedback):
151
+ configatron.apn.feedback.passphrase # => ''
152
+ configatron.apn.feedback.port # => 2196
153
+ configatron.apn.feedback.host # => 'feedback.sandbox.push.apple.com'
154
+ configatron.apn.feedback.cert #=> File.join(RAILS_ROOT, 'config', 'apple_push_notification_development.pem')
155
+
156
+ # production (feedback):
157
+ configatron.apn.feedback.host # => 'feedback.push.apple.com'
158
+ configatron.apn.feedback.cert #=> File.join(RAILS_ROOT, 'config', 'apple_push_notification_production.pem')
159
+ </pre></code>
160
+
161
+ That's it, now you're ready to start creating notifications.
162
+
163
+ h3. Upgrade Notes:
164
+
165
+ If you are upgrading to a new version of APN on Rails you should always run:
166
+
167
+ <pre><code>
168
+ $ ruby script/generate apn_migrations
169
+ </pre></code>
170
+
171
+ That way you ensure you have the latest version of the database tables needed.
172
+ (There is an unaddressed problem in which migration 002 was modified in the repo to add the column custom_properties.
173
+ If you installed the gem prior to that change and try to upgrade following this path you will have to add the
174
+ custom_properties column to the apn_notifications table by hand.)
175
+
176
+ h2. Example (assuming you have created an app and stored your keys on it):
177
+
178
+ <pre><code>
179
+ $ ./script/console
180
+ >> app = APN::App.create(:name => "My App", :apn_dev_cert => "PASTE YOUR DEV CERT HERE", :apn_prod_cert => "PASTE YOUR PROD CERT HERE")
181
+ >> device = APN::Device.create(:token => "XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX",:app_id => app.id)
182
+ >> notification = APN::Notification.new
183
+ >> notification.device = device
184
+ >> notification.badge = 5
185
+ >> notification.sound = true
186
+ >> notification.alert = "foobar"
187
+ >> notification.custom_properties = {:link => "http://www.prx.org"}
188
+ >> notification.save
189
+ </pre></code>
190
+
191
+ You can use the following Rake task to deliver your individual notifications:
192
+
193
+ <pre><code>
194
+ $ rake apn:notifications:deliver
195
+ </pre></code>
196
+
197
+ And the following task to deliver your group notifications:
198
+
199
+ <pre><code>
200
+ $ rake apn:group_notifications:deliver
201
+ </pre></code>
202
+
203
+ The Rake task will find any unsent notifications in the database. If there aren't any notifications
204
+ it will simply do nothing. If there are notifications waiting to be delivered it will open a single connection
205
+ to Apple and push all the notifications through that one connection. Apple does not like people opening/closing
206
+ connections constantly, so it's pretty important that you are careful about batching up your notifications so
207
+ Apple doesn't shut you down.
208
+
209
+ Released under the MIT license.