From ed93f8f3b3319801dda0e5265e9242923dccad4e Mon Sep 17 00:00:00 2001 From: mallowlabs Date: Sat, 5 Jul 2014 21:50:04 +0900 Subject: [PATCH 1/5] Fixed: minor bugs for ZeroPush support refs #196 --- app/models/device.rb | 4 ++++ .../as_iphone_notifier/lib/as_iphone_notifier.rb | 5 ++--- .../asakusa_satellite/apn_service/zero_push.rb | 4 ++-- spec/models/device_spec.rb | 16 ++++++++++++++++ 4 files changed, 24 insertions(+), 5 deletions(-) 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/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/zero_push.rb b/plugins/as_iphone_notifier/lib/asakusa_satellite/apn_service/zero_push.rb index 090ca5c51..99d45021e 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) 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 From 98bbef90ee277295aca1cf3471cf37e3b97a94f9 Mon Sep 17 00:00:00 2001 From: mallowlabs Date: Sat, 5 Jul 2014 22:24:39 +0900 Subject: [PATCH 2/5] Added: PushIt support refs #196 https://github.com/codefirst/push-it/ --- plugins/as_iphone_notifier/init.rb | 1 + .../asakusa_satellite/apn_service/push_it.rb | 40 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 plugins/as_iphone_notifier/lib/asakusa_satellite/apn_service/push_it.rb 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/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..785475743 --- /dev/null +++ b/plugins/as_iphone_notifier/lib/asakusa_satellite/apn_service/push_it.rb @@ -0,0 +1,40 @@ +# -*- encoding: utf-8 -*- + +module AsakusaSatellite + module APNService + class PushIt < Base + def initialize + uri = URI.parse(ENV['PUSH_IT_URL']) + @client = Net::HTTP.new(uri.host, uri.port) + @client.use_ssl = uri.scheme == 'https' + end + + 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 json(device_tokens, room, text) + { + :tokens => device_tokens, + :payload => { + :aps => { + :alert => text, + :sound => "default" + }, + :id => room.id + } + } + end + + end if ENV['PUSH_IT_URL'] + end +end From 52735ba2e3efd979cc439b167d99f0b7dcf5f66a Mon Sep 17 00:00:00 2001 From: mallowlabs Date: Sun, 6 Jul 2014 00:01:14 +0900 Subject: [PATCH 3/5] Fixed: passed wrong room.id like BSON::ObjectId('xxxxxxxx') to 'xxxxxxxx' refs #196 --- .../lib/asakusa_satellite/apn_service/embed.rb | 2 +- .../lib/asakusa_satellite/apn_service/push_it.rb | 2 +- .../lib/asakusa_satellite/apn_service/zero_push.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) 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 index 785475743..c5ec4f194 100644 --- 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 @@ -30,7 +30,7 @@ def json(device_tokens, room, text) :alert => text, :sound => "default" }, - :id => room.id + :id => room.id.to_s } } 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 99d45021e..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 @@ -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 From 518e7a01c96a80a624018e261a443a43cada1bfa Mon Sep 17 00:00:00 2001 From: mallowlabs Date: Sun, 6 Jul 2014 00:06:38 +0900 Subject: [PATCH 4/5] Fixed: broken push notifications refs #196 --- .../lib/asakusa_satellite/apn_service/push_it.rb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) 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 index c5ec4f194..666a9588b 100644 --- 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 @@ -4,15 +4,12 @@ module AsakusaSatellite module APNService class PushIt < Base def initialize - uri = URI.parse(ENV['PUSH_IT_URL']) - @client = Net::HTTP.new(uri.host, uri.port) - @client.use_ssl = uri.scheme == 'https' end def send_message(device_tokens, room, text) body = json(device_tokens, room, text).to_json begin - @client.start do |connection| + client.start do |connection| connection.post("/message", body) end rescue Errno::ECONNREFUSED => e @@ -22,6 +19,13 @@ def send_message(device_tokens, room, text) 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, From e34c727f8fe8402141187b0daf8d01d7ec7a6935 Mon Sep 17 00:00:00 2001 From: mallowlabs Date: Sun, 6 Jul 2014 00:57:41 +0900 Subject: [PATCH 5/5] Refactored: remove unused code refs #196 --- .../lib/asakusa_satellite/apn_service/push_it.rb | 2 -- 1 file changed, 2 deletions(-) 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 index 666a9588b..3b98f4e65 100644 --- 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 @@ -3,8 +3,6 @@ module AsakusaSatellite module APNService class PushIt < Base - def initialize - end def send_message(device_tokens, room, text) body = json(device_tokens, room, text).to_json