Commit 4af508aa by zmj

集成华为推送

parent e4fb872f
...@@ -4,6 +4,7 @@ require 'app_push/igetui' ...@@ -4,6 +4,7 @@ require 'app_push/igetui'
require 'app_push/igetui/push' require 'app_push/igetui/push'
require 'app_push/log' require 'app_push/log'
require 'igetui' require 'igetui'
require 'app_push/huawei'
module AppPush module AppPush
class Push class Push
class << self class << self
...@@ -27,6 +28,10 @@ module AppPush ...@@ -27,6 +28,10 @@ module AppPush
# } # }
# } # }
@igetui_config = opts['igetui'] @igetui_config = opts['igetui']
if @igetui_config.present?
Igetui.config = @igetui_config['opts']
Igetui::Push.config = @igetui_config['base']
end
# xiaomi default config # xiaomi default config
# { # {
# restricted_package_name: 'com.vcooline.aike', # restricted_package_name: 'com.vcooline.aike',
...@@ -36,11 +41,11 @@ module AppPush ...@@ -36,11 +41,11 @@ module AppPush
# notify_type: 1 # notify_type: 1
# } # }
@xiaomi_config = opts['xiaomi'] @xiaomi_config = opts['xiaomi']
if @igetui_config.present?
Igetui.config = @igetui_config['opts']
Igetui::Push.config = @igetui_config['base']
end
Xiaomi.config = @xiaomi_config Xiaomi.config = @xiaomi_config
# huawei default config
@huawei_config = opts['huawei']
Huawei.config = @huawei_config
end end
# transmission_content 是真正的推送信息 # transmission_content 是真正的推送信息
...@@ -58,6 +63,7 @@ module AppPush ...@@ -58,6 +63,7 @@ module AppPush
Xiaomi.push(user_device_ids, message['transmission_content']) Xiaomi.push(user_device_ids, message['transmission_content'])
# huawei # huawei
Huawei.push(user_device_ids, message['transmission_content'])
# etc # etc
rescue StandardError => err rescue StandardError => err
......
module AppPush
module Huawei
class << self
attr_accessor :config, :access_token, :expire_time
def config
@config = {} if @config.blank?
@config.merge!(default_config)
end
def default_config
{
grant_type: 'client_credentials'
}
end
def push(device_tokens, message)
handle_token_res if access_token.blank? || (expire_time.present? && Time.now >= expire_time)
device_tokens.each_slice(100) do |device_token_array|
res_push(message, device_token_array)
end
rescue StandardError => err
AppPush::Log.error(err)
end
def res_push(message, device_tokens)
body = message(message, device_tokens)
headers = {
'Content-Type' => 'application/x-www-form-urlencoded'
}
nsp_ctx = {
ver: '1',
appId: config['app_id']
}
url = "https://api.push.hicloud.com/pushsend.do?nsp_ctx=#{URI.escape(nsp_ctx.to_s)}"
AppPush::Log.info("url: #{url}")
AppPush::Log.info("body: #{body}")
response = HTTParty.post(url, body: body, headers: headers, timeout: 5)
AppPush::Log.info("response: #{response}")
response
end
# 推送信息
def message(message, device_tokens)
payload = {
hps: {
msg: {
type: 1,
body: message
}
}
}
{
access_token: 'access_token',
nsp_svc: 'openpush.message.api.send',
nsp_ts: Time.now.to_i,
device_token_list: device_tokens, # 单次最多100
payload: payload
}
end
def handle_token_res
loop do
res = req_token
if res['error'].blank?
@access_token = res['access_token']
break if @access_token.present?
end
end
@expire_time = Time.now + 58.minutes
end
def req_token
url = 'https://login.cloud.huawei.com/oauth2/v2/token'
body = {
grant_type: 'client_credentials',
client_id: config['app_id'],
client_secret: config['app_secret']
}
headers = {
'Content-Type' => 'application/x-www-form-urlencoded'
}
AppPush::Log.info("url: #{url}")
AppPush::Log.info("body: #{body}")
response = HTTParty.post(url, body: body, headers: headers, timeout: 5)
AppPush::Log.info("response: #{response}")
JSON.parse(response)
end
end
end
end
module AppPush module AppPush
VERSION = "0.11" VERSION = "0.12"
end end
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