diff --git a/app/models/device.rb b/app/models/device.rb index 5618268df..b2f4270fa 100644 --- a/app/models/device.rb +++ b/app/models/device.rb @@ -6,6 +6,10 @@ class Device field :device_type embedded_in :User, :inverse_of => :devices + def ios? + device_type.nil? or device_type == "iphone" + end + def self.register_callback(event) class_eval <<-EOS @@#{event.to_s}_procs = [] diff --git a/plugins/as_iphone_notifier/init.rb b/plugins/as_iphone_notifier/init.rb index f84c8cdb4..9b53a0ce0 100644 --- a/plugins/as_iphone_notifier/init.rb +++ b/plugins/as_iphone_notifier/init.rb @@ -2,4 +2,5 @@ require 'asakusa_satellite/apn_service/base' require 'asakusa_satellite/apn_service/embed' require 'asakusa_satellite/apn_service/zero_push' +require 'asakusa_satellite/apn_service/push_it' diff --git a/plugins/as_iphone_notifier/lib/as_iphone_notifier.rb b/plugins/as_iphone_notifier/lib/as_iphone_notifier.rb index cfe491d8a..5f6b8cab1 100644 --- a/plugins/as_iphone_notifier/lib/as_iphone_notifier.rb +++ b/plugins/as_iphone_notifier/lib/as_iphone_notifier.rb @@ -27,9 +27,8 @@ def after_create_message(context) user.devices }.flatten - device_tokens = devices.select { |device| - device.device_type.nil? or device.device_type == 'iphone' - }.map(&:name) + device_tokens = devices.select(&:ios?).map(&:name) + return if device_tokens.empty? apn_service = AsakusaSatellite::APNService.instance diff --git a/plugins/as_iphone_notifier/lib/asakusa_satellite/apn_service/embed.rb b/plugins/as_iphone_notifier/lib/asakusa_satellite/apn_service/embed.rb index f4ec44a3a..6ceef9e97 100644 --- a/plugins/as_iphone_notifier/lib/asakusa_satellite/apn_service/embed.rb +++ b/plugins/as_iphone_notifier/lib/asakusa_satellite/apn_service/embed.rb @@ -28,7 +28,7 @@ def send_message(device_tokens, room, text) APNS::Notification.new(device_token, :alert => text, :sound => 'default', - :other => { :id => room.id } + :other => { :id => room.id.to_s } ) }.tap { |notification| APNS.send_notifications notification diff --git a/plugins/as_iphone_notifier/lib/asakusa_satellite/apn_service/push_it.rb b/plugins/as_iphone_notifier/lib/asakusa_satellite/apn_service/push_it.rb new file mode 100644 index 000000000..3b98f4e65 --- /dev/null +++ b/plugins/as_iphone_notifier/lib/asakusa_satellite/apn_service/push_it.rb @@ -0,0 +1,42 @@ +# -*- encoding: utf-8 -*- + +module AsakusaSatellite + module APNService + class PushIt < Base + + def send_message(device_tokens, room, text) + body = json(device_tokens, room, text).to_json + begin + client.start do |connection| + connection.post("/message", body) + end + rescue Errno::ECONNREFUSED => e + Rails.logger.error e + end + end + + private + + def client + uri = URI.parse(ENV['PUSH_IT_URL']) + client = Net::HTTP.new(uri.host, uri.port) + client.use_ssl = uri.scheme == 'https' + client + end + + def json(device_tokens, room, text) + { + :tokens => device_tokens, + :payload => { + :aps => { + :alert => text, + :sound => "default" + }, + :id => room.id.to_s + } + } + end + + end if ENV['PUSH_IT_URL'] + end +end diff --git a/plugins/as_iphone_notifier/lib/asakusa_satellite/apn_service/zero_push.rb b/plugins/as_iphone_notifier/lib/asakusa_satellite/apn_service/zero_push.rb index 090ca5c51..361af83aa 100644 --- a/plugins/as_iphone_notifier/lib/asakusa_satellite/apn_service/zero_push.rb +++ b/plugins/as_iphone_notifier/lib/asakusa_satellite/apn_service/zero_push.rb @@ -11,11 +11,11 @@ def initialize end def register(device) - ::ZeroPush.register(normalize(device.name)) + ::ZeroPush.register(normalize(device.name)) if device.ios? end def unregister(device) - ::ZeroPush.unregister(normalize(device.name)) + ::ZeroPush.unregister(normalize(device.name)) if device.ios? end def send_message(device_tokens, room, text) @@ -23,7 +23,7 @@ def send_message(device_tokens, room, text) :device_tokens => device_tokens.map(&method(:normalize)), :alert => text, :sound => "default", - :info => { :id => room.id } + :info => { :id => room.id.to_s } }) end diff --git a/spec/models/device_spec.rb b/spec/models/device_spec.rb index 318463cce..2966e7199 100644 --- a/spec/models/device_spec.rb +++ b/spec/models/device_spec.rb @@ -45,5 +45,21 @@ subject { @destroyed } it { should be } end + + context "ios?" do + context 'nil' do + subject { Device.new(:device_type => nil) } + its(:ios?) { should be_true } + end + context 'iphone' do + subject { Device.new(:device_type => 'iphone') } + its(:ios?) { should be_true } + end + context 'android' do + subject { Device.new(:device_type => 'indroid') } + its(:ios?) { should be_false } + end + end + end end