jirapong-apn_on_rails 0.4.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.specification +80 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +47 -0
- data/LICENSE +21 -0
- data/README +179 -0
- data/README.textile +224 -0
- data/Rakefile +35 -0
- data/apn_on_rails.gemspec +32 -0
- data/autotest/discover.rb +1 -0
- data/generators/apn_migrations_generator.rb +33 -0
- data/generators/templates/apn_migrations/001_create_apn_devices.rb +13 -0
- data/generators/templates/apn_migrations/002_create_apn_notifications.rb +23 -0
- data/generators/templates/apn_migrations/003_alter_apn_devices.rb +23 -0
- data/generators/templates/apn_migrations/004_create_apn_apps.rb +25 -0
- data/generators/templates/apn_migrations/005_create_groups.rb +23 -0
- data/generators/templates/apn_migrations/006_alter_apn_groups.rb +11 -0
- data/generators/templates/apn_migrations/007_create_device_groups.rb +27 -0
- data/generators/templates/apn_migrations/008_create_apn_group_notifications.rb +23 -0
- data/generators/templates/apn_migrations/009_create_pull_notifications.rb +16 -0
- data/generators/templates/apn_migrations/010_alter_apn_notifications.rb +21 -0
- data/generators/templates/apn_migrations/011_make_device_token_index_nonunique.rb +11 -0
- data/generators/templates/apn_migrations/012_add_launch_notification_to_apn_pull_notifications.rb +9 -0
- data/lib/apn_on_rails.rb +4 -0
- data/lib/apn_on_rails/apn_on_rails.rb +81 -0
- data/lib/apn_on_rails/app/models/apn/app.rb +151 -0
- data/lib/apn_on_rails/app/models/apn/base.rb +11 -0
- data/lib/apn_on_rails/app/models/apn/device.rb +49 -0
- data/lib/apn_on_rails/app/models/apn/device_grouping.rb +16 -0
- data/lib/apn_on_rails/app/models/apn/group.rb +12 -0
- data/lib/apn_on_rails/app/models/apn/group_notification.rb +79 -0
- data/lib/apn_on_rails/app/models/apn/notification.rb +93 -0
- data/lib/apn_on_rails/app/models/apn/pull_notification.rb +28 -0
- data/lib/apn_on_rails/libs/connection.rb +70 -0
- data/lib/apn_on_rails/libs/feedback.rb +39 -0
- data/lib/apn_on_rails/rails/railtie.rb +15 -0
- data/lib/apn_on_rails/tasks/apn.rake +30 -0
- data/lib/apn_on_rails/tasks/db.rake +23 -0
- data/lib/apn_on_rails/version.rb +3 -0
- data/lib/apn_on_rails_tasks.rb +3 -0
- data/lib/generators/apn_on_rails/install/USAGE +8 -0
- data/lib/generators/apn_on_rails/install/install_generator.rb +38 -0
- data/lib/generators/apn_on_rails/install/templates/001_create_apn_devices.rb +13 -0
- data/lib/generators/apn_on_rails/install/templates/002_create_apn_notifications.rb +23 -0
- data/lib/generators/apn_on_rails/install/templates/003_alter_apn_devices.rb +25 -0
- data/lib/generators/apn_on_rails/install/templates/004_create_apn_apps.rb +18 -0
- data/lib/generators/apn_on_rails/install/templates/005_create_groups.rb +23 -0
- data/lib/generators/apn_on_rails/install/templates/006_alter_apn_groups.rb +11 -0
- data/lib/generators/apn_on_rails/install/templates/007_create_device_groups.rb +27 -0
- data/lib/generators/apn_on_rails/install/templates/008_create_apn_group_notifications.rb +23 -0
- data/lib/generators/apn_on_rails/install/templates/009_create_pull_notifications.rb +16 -0
- data/lib/generators/apn_on_rails/install/templates/010_alter_apn_notifications.rb +21 -0
- data/lib/generators/apn_on_rails/install/templates/011_make_device_token_index_nonunique.rb +11 -0
- data/lib/generators/apn_on_rails/install/templates/012_add_launch_notification_to_apn_pull_notifications.rb +9 -0
- data/spec/active_record/setup_ar.rb +20 -0
- data/spec/apn_on_rails/app/models/apn/app_spec.rb +230 -0
- data/spec/apn_on_rails/app/models/apn/device_spec.rb +61 -0
- data/spec/apn_on_rails/app/models/apn/group_notification_spec.rb +66 -0
- data/spec/apn_on_rails/app/models/apn/notification_spec.rb +71 -0
- data/spec/apn_on_rails/app/models/apn/pull_notification_spec.rb +100 -0
- data/spec/apn_on_rails/libs/connection_spec.rb +40 -0
- data/spec/apn_on_rails/libs/feedback_spec.rb +43 -0
- data/spec/extensions/string.rb +10 -0
- data/spec/factories/app_factory.rb +27 -0
- data/spec/factories/device_factory.rb +29 -0
- data/spec/factories/device_grouping_factory.rb +22 -0
- data/spec/factories/group_factory.rb +27 -0
- data/spec/factories/group_notification_factory.rb +22 -0
- data/spec/factories/notification_factory.rb +22 -0
- data/spec/factories/pull_notification_factory.rb +22 -0
- data/spec/fixtures/hexa.bin +1 -0
- data/spec/fixtures/message_for_sending.bin +0 -0
- data/spec/rails_root/config/apple_push_notification_development.pem +19 -0
- data/spec/spec_helper.rb +64 -0
- metadata +242 -0
@@ -0,0 +1,70 @@
|
|
1
|
+
module APN
|
2
|
+
module Connection
|
3
|
+
|
4
|
+
class << self
|
5
|
+
|
6
|
+
# Yields up an SSL socket to write notifications to.
|
7
|
+
# The connections are close automatically.
|
8
|
+
#
|
9
|
+
# Example:
|
10
|
+
# APN::Configuration.open_for_delivery do |conn|
|
11
|
+
# conn.write('my cool notification')
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# Configuration parameters are:
|
15
|
+
#
|
16
|
+
# configatron.apn.passphrase = ''
|
17
|
+
# configatron.apn.port = 2195
|
18
|
+
# configatron.apn.host = 'gateway.sandbox.push.apple.com' # Development
|
19
|
+
# configatron.apn.host = 'gateway.push.apple.com' # Production
|
20
|
+
# configatron.apn.cert = File.join(rails_root, 'config', 'apple_push_notification_development.pem')) # Development
|
21
|
+
# configatron.apn.cert = File.join(rails_root, 'config', 'apple_push_notification_production.pem')) # Production
|
22
|
+
def open_for_delivery(options = {}, &block)
|
23
|
+
open(options, &block)
|
24
|
+
end
|
25
|
+
|
26
|
+
# Yields up an SSL socket to receive feedback from.
|
27
|
+
# The connections are close automatically.
|
28
|
+
# Configuration parameters are:
|
29
|
+
#
|
30
|
+
# configatron.apn.feedback.passphrase = ''
|
31
|
+
# configatron.apn.feedback.port = 2196
|
32
|
+
# configatron.apn.feedback.host = 'feedback.sandbox.push.apple.com' # Development
|
33
|
+
# configatron.apn.feedback.host = 'feedback.push.apple.com' # Production
|
34
|
+
# configatron.apn.feedback.cert = File.join(rails_root, 'config', 'apple_push_notification_development.pem')) # Development
|
35
|
+
# configatron.apn.feedback.cert = File.join(rails_root, 'config', 'apple_push_notification_production.pem')) # Production
|
36
|
+
def open_for_feedback(options = {}, &block)
|
37
|
+
options = {:cert => configatron.apn.feedback.cert,
|
38
|
+
:passphrase => configatron.apn.feedback.passphrase,
|
39
|
+
:host => configatron.apn.feedback.host,
|
40
|
+
:port => configatron.apn.feedback.port}.merge(options)
|
41
|
+
open(options, &block)
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
def open(options = {}, &block) # :nodoc:
|
46
|
+
options = {:cert => configatron.apn.cert,
|
47
|
+
:passphrase => configatron.apn.passphrase,
|
48
|
+
:host => configatron.apn.host,
|
49
|
+
:port => configatron.apn.port}.merge(options)
|
50
|
+
#cert = File.read(options[:cert])
|
51
|
+
cert = options[:cert]
|
52
|
+
ctx = OpenSSL::SSL::SSLContext.new
|
53
|
+
ctx.key = OpenSSL::PKey::RSA.new(cert, options[:passphrase])
|
54
|
+
ctx.cert = OpenSSL::X509::Certificate.new(cert)
|
55
|
+
|
56
|
+
sock = TCPSocket.new(options[:host], options[:port])
|
57
|
+
ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
|
58
|
+
ssl.sync = true
|
59
|
+
ssl.connect
|
60
|
+
|
61
|
+
yield ssl, sock if block_given?
|
62
|
+
|
63
|
+
ssl.close
|
64
|
+
sock.close
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
end # Connection
|
70
|
+
end # APN
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module APN
|
2
|
+
# Module for talking to the Apple Feedback Service.
|
3
|
+
# The service is meant to let you know when a device is no longer
|
4
|
+
# registered to receive notifications for your application.
|
5
|
+
module Feedback
|
6
|
+
|
7
|
+
class << self
|
8
|
+
|
9
|
+
# Returns an Array of APN::Device objects that
|
10
|
+
# has received feedback from Apple. Each APN::Device will
|
11
|
+
# have it's <tt>feedback_at</tt> accessor marked with the time
|
12
|
+
# that Apple believes the device de-registered itself.
|
13
|
+
def devices(cert, &block)
|
14
|
+
devices = []
|
15
|
+
return if cert.nil?
|
16
|
+
APN::Connection.open_for_feedback({:cert => cert}) do |conn, sock|
|
17
|
+
while line = conn.read(38) # Read 38 bytes from the SSL socket
|
18
|
+
feedback = line.unpack('N1n1H140')
|
19
|
+
token = feedback[2].scan(/.{0,8}/).join(' ').strip
|
20
|
+
device = APN::Device.find(:first, :conditions => {:token => token})
|
21
|
+
if device
|
22
|
+
device.feedback_at = Time.at(feedback[0])
|
23
|
+
devices << device
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
devices.each(&block) if block_given?
|
28
|
+
return devices
|
29
|
+
end # devices
|
30
|
+
|
31
|
+
def process_devices
|
32
|
+
ActiveSupport::Deprecation.warn("The method APN::Feedback.process_devices is deprecated. Use APN::App.process_devices instead.")
|
33
|
+
APN::App.process_devices
|
34
|
+
end
|
35
|
+
|
36
|
+
end # class << self
|
37
|
+
|
38
|
+
end # Feedback
|
39
|
+
end # APN
|
@@ -0,0 +1,15 @@
|
|
1
|
+
if defined?(::Rails::Railtie) # backwards compatible
|
2
|
+
|
3
|
+
module APN
|
4
|
+
module Rails
|
5
|
+
|
6
|
+
class Railtie < ::Rails::Railtie
|
7
|
+
rake_tasks do
|
8
|
+
Dir[File.join(File.dirname(__FILE__),'..', 'tasks/*.rake')].each { |f| load f }
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
namespace :apn do
|
2
|
+
|
3
|
+
namespace :notifications do
|
4
|
+
|
5
|
+
desc "Deliver all unsent APN notifications."
|
6
|
+
task :deliver => [:environment] do
|
7
|
+
APN::App.send_notifications
|
8
|
+
end
|
9
|
+
|
10
|
+
end # notifications
|
11
|
+
|
12
|
+
namespace :group_notifications do
|
13
|
+
|
14
|
+
desc "Deliver all unsent APN Group notifications."
|
15
|
+
task :deliver => [:environment] do
|
16
|
+
APN::App.send_group_notifications
|
17
|
+
end
|
18
|
+
|
19
|
+
end # group_notifications
|
20
|
+
|
21
|
+
namespace :feedback do
|
22
|
+
|
23
|
+
desc "Process all devices that have feedback from APN."
|
24
|
+
task :process => [:environment] do
|
25
|
+
APN::App.process_devices
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
end # apn
|
@@ -0,0 +1,23 @@
|
|
1
|
+
namespace :apn do
|
2
|
+
|
3
|
+
namespace :db do
|
4
|
+
|
5
|
+
task :migrate do
|
6
|
+
puts %{
|
7
|
+
This task no longer exists. Please generate the migrations like this:
|
8
|
+
|
9
|
+
$ ruby script/generate apn_migrations
|
10
|
+
|
11
|
+
or
|
12
|
+
|
13
|
+
$ rails g apn_on_rails:install
|
14
|
+
|
15
|
+
Then just run the migrations like you would normally:
|
16
|
+
|
17
|
+
$ rake db:migrate
|
18
|
+
}.strip
|
19
|
+
end
|
20
|
+
|
21
|
+
end # db
|
22
|
+
|
23
|
+
end # apn
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
require 'rails/generators/migration'
|
3
|
+
|
4
|
+
# Generates the migrations necessary for APN on Rails.
|
5
|
+
# This should be run upon install and upgrade of the
|
6
|
+
# APN on Rails gem.
|
7
|
+
#
|
8
|
+
# $ rails generate apn:apn_migrations
|
9
|
+
module ApnOnRails
|
10
|
+
module Generators
|
11
|
+
|
12
|
+
class InstallGenerator < Rails::Generators::Base
|
13
|
+
include Rails::Generators::Migration
|
14
|
+
source_root(File.expand_path(File.join(File.dirname(__FILE__), 'templates')))
|
15
|
+
desc "add the migrations"
|
16
|
+
|
17
|
+
def self.next_migration_number(path)
|
18
|
+
unless @prev_migration_nr
|
19
|
+
@prev_migration_nr = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i
|
20
|
+
else
|
21
|
+
@prev_migration_nr += 1
|
22
|
+
end
|
23
|
+
@prev_migration_nr.to_s
|
24
|
+
end
|
25
|
+
|
26
|
+
def create_migrations
|
27
|
+
Dir.glob(File.join(self.class.source_root, '*.rb')).sort.each_with_index do |f, i|
|
28
|
+
source = File.basename(f)
|
29
|
+
source.match(/\d+\_(.+)/)
|
30
|
+
destination = "db/migrate/#{$1}"
|
31
|
+
migration_template source, destination
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class CreateApnNotifications < ActiveRecord::Migration # :nodoc:
|
2
|
+
|
3
|
+
def self.up
|
4
|
+
|
5
|
+
create_table :apn_notifications do |t|
|
6
|
+
t.integer :device_id, :null => false
|
7
|
+
t.integer :errors_nb, :default => 0 # used for storing errors from apple feedbacks
|
8
|
+
t.string :device_language, :size => 5 # if you don't want to send localized strings
|
9
|
+
t.string :sound
|
10
|
+
t.string :alert, :size => 150
|
11
|
+
t.integer :badge
|
12
|
+
t.datetime :sent_at
|
13
|
+
t.timestamps
|
14
|
+
end
|
15
|
+
|
16
|
+
add_index :apn_notifications, :device_id
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.down
|
20
|
+
drop_table :apn_notifications
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
class AlterApnDevices < ActiveRecord::Migration # :nodoc:
|
2
|
+
|
3
|
+
module APN # :nodoc:
|
4
|
+
class Device < ActiveRecord::Base # :nodoc:
|
5
|
+
set_table_name 'apn_devices'
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.up
|
10
|
+
add_column :apn_devices, :last_registered_at, :datetime
|
11
|
+
|
12
|
+
APN::Device.all.each do |device|
|
13
|
+
device.last_registered_at = device.created_at
|
14
|
+
device.save!
|
15
|
+
end
|
16
|
+
change_column :apn_devices, :token, :string, :size => 100, :null => false
|
17
|
+
add_index :apn_devices, :token, :unique => true
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.down
|
21
|
+
change_column :apn_devices, :token, :string
|
22
|
+
remove_index :apn_devices, :column => :token
|
23
|
+
remove_column :apn_devices, :last_registered_at
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class CreateApnApps < ActiveRecord::Migration # :nodoc:
|
2
|
+
def self.up
|
3
|
+
create_table :apn_apps do |t|
|
4
|
+
t.text :apn_dev_cert
|
5
|
+
t.text :apn_prod_cert
|
6
|
+
|
7
|
+
t.timestamps
|
8
|
+
end
|
9
|
+
|
10
|
+
add_column :apn_devices, :app_id, :integer
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.down
|
15
|
+
drop_table :apn_apps
|
16
|
+
remove_column :apn_devices, :app_id
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class CreateGroups < ActiveRecord::Migration # :nodoc:
|
2
|
+
def self.up
|
3
|
+
create_table :apn_groups do |t|
|
4
|
+
t.column :name, :string
|
5
|
+
|
6
|
+
t.timestamps
|
7
|
+
end
|
8
|
+
|
9
|
+
create_table :apn_devices_apn_groups, :id => false do |t|
|
10
|
+
t.column :group_id, :integer
|
11
|
+
t.column :device_id, :integer
|
12
|
+
end
|
13
|
+
|
14
|
+
add_index :apn_devices_apn_groups, [:group_id, :device_id]
|
15
|
+
add_index :apn_devices_apn_groups, :device_id
|
16
|
+
add_index :apn_devices_apn_groups, :group_id
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.down
|
20
|
+
drop_table :apn_groups
|
21
|
+
drop_table :apn_devices_apn_groups
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
class CreateDeviceGroups < ActiveRecord::Migration # :nodoc:
|
2
|
+
def self.up
|
3
|
+
drop_table :apn_devices_apn_groups
|
4
|
+
|
5
|
+
create_table :apn_device_groupings do |t|
|
6
|
+
t.column :group_id, :integer
|
7
|
+
t.column :device_id, :integer
|
8
|
+
end
|
9
|
+
|
10
|
+
add_index :apn_device_groupings, [:group_id, :device_id]
|
11
|
+
add_index :apn_device_groupings, :device_id
|
12
|
+
add_index :apn_device_groupings, :group_id
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.down
|
16
|
+
drop_table :apn_device_groupings
|
17
|
+
|
18
|
+
create_table :apn_devices_apn_groups, :id => false do |t|
|
19
|
+
t.column :group_id, :integer
|
20
|
+
t.column :device_id, :integer
|
21
|
+
end
|
22
|
+
|
23
|
+
add_index :apn_devices_apn_groups, [:group_id, :device_id]
|
24
|
+
add_index :apn_devices_apn_groups, :device_id
|
25
|
+
add_index :apn_devices_apn_groups, :group_id
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class CreateApnGroupNotifications < ActiveRecord::Migration # :nodoc:
|
2
|
+
|
3
|
+
def self.up
|
4
|
+
|
5
|
+
create_table :apn_group_notifications do |t|
|
6
|
+
t.integer :group_id, :null => false
|
7
|
+
t.string :device_language, :size => 5 # if you don't want to send localized strings
|
8
|
+
t.string :sound
|
9
|
+
t.string :alert, :size => 150
|
10
|
+
t.integer :badge
|
11
|
+
t.text :custom_properties
|
12
|
+
t.datetime :sent_at
|
13
|
+
t.timestamps
|
14
|
+
end
|
15
|
+
|
16
|
+
add_index :apn_group_notifications, :group_id
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.down
|
20
|
+
drop_table :apn_group_notifications
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class CreatePullNotifications < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :apn_pull_notifications do |t|
|
4
|
+
t.integer :app_id
|
5
|
+
t.string :title
|
6
|
+
t.string :content
|
7
|
+
t.string :link
|
8
|
+
|
9
|
+
t.timestamps
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.down
|
14
|
+
drop_table :apn_pull_notifications
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class AlterApnNotifications < ActiveRecord::Migration # :nodoc:
|
2
|
+
|
3
|
+
module APN # :nodoc:
|
4
|
+
class Notification < ActiveRecord::Base # :nodoc:
|
5
|
+
set_table_name 'apn_notifications'
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.up
|
10
|
+
unless APN::Notification.column_names.include?("custom_properties")
|
11
|
+
add_column :apn_notifications, :custom_properties, :text
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.down
|
16
|
+
if APN::Notification.column_names.include?("custom_properties")
|
17
|
+
remove_column :apn_notifications, :custom_properties
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|