Commit 12ad5125 by 李福中

Enhance push service can push to android and ios by device

parent ddb1aa07
...@@ -26,6 +26,7 @@ class PushsController < ApplicationController ...@@ -26,6 +26,7 @@ class PushsController < ApplicationController
param! :igetui_opts, String param! :igetui_opts, String
param! :app_type, String param! :app_type, String
param! :sync_push, String param! :sync_push, String
param! :device, String, required: false # optionals: 'android', 'ios'
igetui_opts = JSON.parse(params[:igetui_opts]) rescue {} igetui_opts = JSON.parse(params[:igetui_opts]) rescue {}
message = JSON.parse(params[:message]) rescue {} message = JSON.parse(params[:message]) rescue {}
...@@ -33,15 +34,21 @@ class PushsController < ApplicationController ...@@ -33,15 +34,21 @@ class PushsController < ApplicationController
token = token_and_options(request).first token = token_and_options(request).first
if token == Token.token(params[:app_name]) if token == Token.token(params[:app_name])
if params.has_key?(:sync_push) if params.has_key?(:sync_push)
Timeout.timeout(3) { timeout_seconds = $redis.get('push_timeout').to_i
::PushWorker.new.perform(device_ids_opts, message, igetui_opts, params[:app_type]) if timeout_seconds > 0
} Timeout.timeout(timeout_seconds) {
::PushWorker.new.perform(device_ids_opts, message, igetui_opts, params[:app_type], device: params[:device])
}
else
::PushWorker.new.perform(device_ids_opts, message, igetui_opts, params[:app_type], device: params[:device])
end
else else
::PushWorker.perform_async(device_ids_opts, message, igetui_opts, params[:app_type]) ::PushWorker.perform_async(device_ids_opts, message, igetui_opts, params[:app_type], device: params[:device])
end end
render json: { code: 0, message: 'success', describe: '异步任务正在处理'} render json: { code: 0, message: 'success', describe: '异步任务正在处理'}
else else
render json: { code: -1, message: 'token 错误/过期'} render json: { code: -1, message: 'token 错误/过期'}
end end
end end
end end
...@@ -12,8 +12,8 @@ module Push ...@@ -12,8 +12,8 @@ module Push
# xiaomi: [], # xiaomi: [],
# huawei: [] # huawei: []
# } # }
def push(device_ids_opts, message, igetui_opts = {}, app_type = nil) def push(device_ids_opts, message, igetui_opts = {}, app_type = nil, device: nil)
Settings.platform = app_type || Settings::DEFAULT Settings.set_platform_device(app_type || Settings::DEFAULT, device_suffix: device_suffix(device))
Push::Log.info("*************** push_service ***************:\n device_ids_opts: #{device_ids_opts}, message: #{message}, igetui_opts: #{igetui_opts} platform: #{Settings.platform}") Push::Log.info("*************** push_service ***************:\n device_ids_opts: #{device_ids_opts}, message: #{message}, igetui_opts: #{igetui_opts} platform: #{Settings.platform}")
message = message.with_indifferent_access message = message.with_indifferent_access
igetui_opts = igetui_opts.with_indifferent_access igetui_opts = igetui_opts.with_indifferent_access
...@@ -33,5 +33,14 @@ module Push ...@@ -33,5 +33,14 @@ module Push
rescue StandardError => err rescue StandardError => err
Push::Log.error(err) Push::Log.error(err)
end end
private
def device_suffix(device)
case device.to_s
when /android/i then '_android'
when /ios/i then '_ios'
end
end
end end
end end
...@@ -49,6 +49,22 @@ module Push ...@@ -49,6 +49,22 @@ module Push
) )
end end
def android_pusher
@android_pusher ||= IGeTui.pusher(
Settings.platform_settings.igetui['app_id'],
Settings.platform_settings.igetui['app_key'],
Settings.platform_settings.igetui['master_secret']
)
end
def ios_pusher
@ios_pusher ||= IGeTui.pusher(
Settings.platform_settings.igetui['app_id'],
Settings.platform_settings.igetui['app_key'],
Settings.platform_settings.igetui['master_secret']
)
end
def set_message def set_message
@message = case pusher_type @message = case pusher_type
when 'push_message_to_single' when 'push_message_to_single'
......
...@@ -9,12 +9,27 @@ class Settings < Settingslogic ...@@ -9,12 +9,27 @@ class Settings < Settingslogic
@platform = platform if PLATFORM.include? platform @platform = platform if PLATFORM.include? platform
end end
def set_platform_device(platform, device_suffix: '')
if PLATFORM.include?(platform)
@platform = platform
end
@device_suffix = device_suffix
end
def self.platform def self.platform
@platform || DEFAULT @platform || DEFAULT
end end
def self.platform_with_device
"#{@platform || DEFAULT}#{@device_suffix}"
end
def self.platform_settings def self.platform_settings
self.send(platform) if Settings[platform_with_device].present?
self.send(platform_with_device)
else
self.send(platform)
end
end end
end end
\ No newline at end of file
...@@ -2,9 +2,9 @@ class PushWorker ...@@ -2,9 +2,9 @@ class PushWorker
include Sidekiq::Worker include Sidekiq::Worker
sidekiq_options queue: :push sidekiq_options queue: :push
def perform(device_ids_opts, message, igetui_opts, platform = nil) def perform(device_ids_opts, message, igetui_opts, platform = nil, device: nil)
Push.push(device_ids_opts, message, igetui_opts, platform) Push.push(device_ids_opts, message, igetui_opts, platform, device: device)
rescue StandardError => e rescue StandardError => e
ErrorLog.error(e) ErrorLog.error(e)
end end
end end
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment