apn_on_rails 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README +45 -30
- data/lib/apn_on_rails/apn_on_rails.rb +4 -3
- data/lib/apn_on_rails/app/models/apn/device.rb +7 -0
- data/lib/apn_on_rails/app/models/apn/notification.rb +29 -0
- data/lib/apn_on_rails/db/migrate/20090723132058_create_apn_devices.rb +1 -1
- data/lib/apn_on_rails/db/migrate/20090723132059_create_apn_notifications.rb +1 -1
- metadata +1 -1
data/README
CHANGED
@@ -1,16 +1,17 @@
|
|
1
|
-
APN on Rails
|
2
|
-
|
3
|
-
|
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
|
+
==Acknowledgements:
|
4
7
|
|
5
|
-
Acknowledgements
|
6
|
-
----------------
|
7
8
|
This gem is a re-write of a plugin that was written by Fabien Penso and Sam Soffes.
|
8
9
|
Their plugin was a great start, but it just didn't quite reach the level I hoped it would.
|
9
10
|
I've re-written, as a gem, added a ton of tests, and I would like to think that I made it
|
10
11
|
a little nicer and easier to use.
|
11
12
|
|
12
|
-
Converting Your Certificate
|
13
|
-
|
13
|
+
==Converting Your Certificate:
|
14
|
+
|
14
15
|
Once you have the certificate from Apple for your application, export your key
|
15
16
|
and the apple certificate as p12 files. Here is a quick walkthrough on how to do this:
|
16
17
|
|
@@ -26,28 +27,38 @@ Put 'apple_push_notification_production.pem' in config/
|
|
26
27
|
|
27
28
|
If you are using a development certificate, then change the name to apple_push_notification_development.pem instead.
|
28
29
|
|
29
|
-
Installing
|
30
|
-
|
30
|
+
==Installing:
|
31
|
+
|
32
|
+
===Stable (RubyForge):
|
31
33
|
|
32
|
-
From RubyForge:
|
33
34
|
$ sudo gem install apn_on_rails
|
34
35
|
|
35
|
-
|
36
|
+
===Edge (GitHub):
|
37
|
+
|
36
38
|
$ sudo gem install markbates-apn_on_rails --source=http://gems.github.com
|
37
|
-
|
38
|
-
|
39
|
-
require 'apn_on_rails'
|
39
|
+
|
40
|
+
===Rails Gem Management:
|
40
41
|
|
41
42
|
If you like to use the built in Rails gem management:
|
43
|
+
|
42
44
|
config.gem 'apn_on_rails'
|
43
45
|
|
44
46
|
Or, if you like to live on the edge:
|
47
|
+
|
45
48
|
config.gem 'markbates-apn_on_rails', :lib => 'apn_on_rails', :source => 'http://gems.github.com'
|
46
|
-
|
47
|
-
Setup and Configuration
|
48
|
-
|
49
|
-
Once you have the gem installed
|
49
|
+
|
50
|
+
==Setup and Configuration:
|
51
|
+
|
52
|
+
Once you have the gem installed via your favorite gem installation, you need to require it so you can
|
53
|
+
start to use it:
|
54
|
+
|
55
|
+
Add the following require, wherever it makes sense to you:
|
56
|
+
|
57
|
+
require 'apn_on_rails'
|
58
|
+
|
59
|
+
You also need to add the following to your Rakefile so you can use the
|
50
60
|
Rake tasks that ship with APN on Rails:
|
61
|
+
|
51
62
|
begin
|
52
63
|
require 'apn_on_rails_tasks'
|
53
64
|
rescue MissingSourceFile => e
|
@@ -55,6 +66,7 @@ Rake tasks that ship with APN on Rails:
|
|
55
66
|
end
|
56
67
|
|
57
68
|
Now, to create the tables you need for APN on Rails, run the following task:
|
69
|
+
|
58
70
|
$ rake apn:db:migrate
|
59
71
|
|
60
72
|
APN on Rails uses the Configatron gem, http://github.com/markbates/configatron/tree/master,
|
@@ -72,17 +84,20 @@ see fit:
|
|
72
84
|
|
73
85
|
That's it, now you're ready to start creating notifications.
|
74
86
|
|
75
|
-
Example
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
+
==Example:
|
88
|
+
|
89
|
+
$ ./script/console
|
90
|
+
>> device = APN::Device.create(:token => "XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX")
|
91
|
+
>> notification = APN::Notification.new
|
92
|
+
>> notification.device = device
|
93
|
+
>> notification.badge = 5
|
94
|
+
>> notification.sound = true
|
95
|
+
>> notification.alert = "foobar"
|
96
|
+
>> notification.save
|
97
|
+
>> APN::Notification.send_notifications
|
98
|
+
|
99
|
+
You can also run the following Rake task to send your notifications:
|
100
|
+
|
101
|
+
$ rake apn:notifications:deliver
|
87
102
|
|
88
103
|
Released under the MIT license.
|
@@ -22,13 +22,14 @@ if rails_env == 'production'
|
|
22
22
|
configatron.apn.set_default(:cert, File.join(rails_root, 'config', 'apple_push_notification_production.pem'))
|
23
23
|
end
|
24
24
|
|
25
|
-
module APN
|
25
|
+
module APN # :nodoc:
|
26
26
|
|
27
|
-
module Errors
|
27
|
+
module Errors # :nodoc:
|
28
28
|
|
29
|
+
# Raised when a notification message to Apple is longer than 256 bytes.
|
29
30
|
class ExceededMessageSizeError < StandardError
|
30
31
|
|
31
|
-
def initialize(message)
|
32
|
+
def initialize(message) # :nodoc:
|
32
33
|
super("The maximum size allowed for a notification payload is 256 bytes: '#{message}'")
|
33
34
|
end
|
34
35
|
|
@@ -1,3 +1,8 @@
|
|
1
|
+
# Represents an iPhone (or other APN enabled device).
|
2
|
+
# An APN::Device can have many APN::Notification.
|
3
|
+
#
|
4
|
+
# Example:
|
5
|
+
# Device.create(:token => '5gxadhy6 6zmtxfl6 5zpbcxmw ez3w7ksf qscpr55t trknkzap 7yyt45sc g6jrw7qz')
|
1
6
|
class APN::Device < ActiveRecord::Base
|
2
7
|
set_table_name 'apn_devices'
|
3
8
|
|
@@ -6,6 +11,8 @@ class APN::Device < ActiveRecord::Base
|
|
6
11
|
validates_uniqueness_of :token
|
7
12
|
validates_format_of :token, :with => /^[a-z0-9]{8}\s[a-z0-9]{8}\s[a-z0-9]{8}\s[a-z0-9]{8}\s[a-z0-9]{8}\s[a-z0-9]{8}\s[a-z0-9]{8}\s[a-z0-9]{8}$/
|
8
13
|
|
14
|
+
# Stores the token (Apple's device ID) of the iPhone (device).
|
15
|
+
#
|
9
16
|
# If the token comes in like this:
|
10
17
|
# '<5gxadhy6 6zmtxfl6 5zpbcxmw ez3w7ksf qscpr55t trknkzap 7yyt45sc g6jrw7qz>'
|
11
18
|
# Then the '<' and '>' will be stripped off.
|
@@ -1,3 +1,19 @@
|
|
1
|
+
# Represents the message you wish to send.
|
2
|
+
# An APN::Notification belongs to an APN::Device.
|
3
|
+
#
|
4
|
+
# Example:
|
5
|
+
# apn = APN::Notification.new
|
6
|
+
# apn.badge = 5
|
7
|
+
# apn.sound = 'my_sound.aiff'
|
8
|
+
# apn.alert = 'Hello!'
|
9
|
+
# apn.device = APN::Device.find(1)
|
10
|
+
# apn.save
|
11
|
+
#
|
12
|
+
# To deliver call the following method:
|
13
|
+
# APN::Notification.send_notifications
|
14
|
+
#
|
15
|
+
# As each APN::Notification is sent the <tt>sent_at</tt> column will be timestamped,
|
16
|
+
# so as to not be sent again.
|
1
17
|
class APN::Notification < ActiveRecord::Base
|
2
18
|
include ::ActionView::Helpers::TextHelper
|
3
19
|
extend ::ActionView::Helpers::TextHelper
|
@@ -6,6 +22,10 @@ class APN::Notification < ActiveRecord::Base
|
|
6
22
|
|
7
23
|
belongs_to :device, :class_name => 'APN::Device'
|
8
24
|
|
25
|
+
# Stores the text alert message you want to send to the device.
|
26
|
+
#
|
27
|
+
# If the message is over 150 characters long it will get truncated
|
28
|
+
# to 150 characters with a <tt>...</tt>
|
9
29
|
def alert=(message)
|
10
30
|
if !message.blank? && message.size > 150
|
11
31
|
message = truncate(message, :length => 150)
|
@@ -55,6 +75,15 @@ class APN::Notification < ActiveRecord::Base
|
|
55
75
|
|
56
76
|
class << self
|
57
77
|
|
78
|
+
# Opens a connection to the Apple APN server and attempts to batch deliver
|
79
|
+
# an Array of notifications.
|
80
|
+
#
|
81
|
+
# This method expects an Array of APN::Notifications. If no parameter is passed
|
82
|
+
# in then it will use the following:
|
83
|
+
# APN::Notification.all(:conditions => {:sent_at => nil})
|
84
|
+
#
|
85
|
+
# As each APN::Notification is sent the <tt>sent_at</tt> column will be timestamped,
|
86
|
+
# so as to not be sent again.
|
58
87
|
def send_notifications(notifications = APN::Notification.all(:conditions => {:sent_at => nil}))
|
59
88
|
unless notifications.nil? || notifications.empty?
|
60
89
|
logger.info "APN: Attempting to deliver #{pluralize(notifications.size, 'notification')}."
|