Commit 0c60a2b2 by zmj

app_push 现在集成了个推和小米推送

parents
/.bundle/
/.yardoc
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
# Specify your gem's dependencies in app_push.gemspec
gemspec
PATH
remote: .
specs:
app_push (0.1.07)
activesupport (~> 4.2.1)
igetui-ruby (~> 1.2.2)
GEM
remote: https://rubygems.org/
specs:
activesupport (4.2.10)
i18n (~> 0.7)
minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
concurrent-ruby (1.1.5)
i18n (0.9.5)
concurrent-ruby (~> 1.0)
igetui-ruby (1.2.2)
json (~> 1.8, >= 1.8.1)
ruby-protocol-buffers (~> 1.5, >= 1.5.1)
json (1.8.6)
minitest (5.11.3)
ruby-protocol-buffers (1.6.1)
thread_safe (0.3.6)
tzinfo (1.2.5)
thread_safe (~> 0.1)
PLATFORMS
ruby
DEPENDENCIES
app_push!
BUNDLED WITH
1.16.6
# AppPush
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/app_push`. To experiment with that code, run `bin/console` for an interactive prompt.
TODO: Delete this and the text above, and describe your gem
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'app_push'
```
And then execute:
$ bundle
Or install it yourself as:
$ gem install app_push
## Usage
TODO: Write usage instructions here
## Development
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/app_push.
require "bundler/gem_tasks"
task :default => :spec
lib = File.expand_path('lib', __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'app_push/version'
Gem::Specification.new do |spec|
spec.name = 'app_push'
spec.version = AppPush::VERSION
spec.authors = ['zmj']
spec.email = ['zhang.mj@ikcrm.com']
spec.summary = '消息推送项目,当前集成了个推和小米推送'
spec.description = 'T消息推送项目,当前集成了个推和小米推送'
spec.homepage = 'http://gitlab.ikcrm.com/ikcrm_server/app_push'
# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
spec.files = Dir.chdir(File.expand_path(__dir__)) do
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
end
spec.bindir = 'exe'
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ['lib']
spec.add_dependency 'activesupport', '~> 4.2.1'
spec.add_dependency 'igetui-ruby', '~> 1.2.2'
end
require 'app_push/version'
require 'app_push/xiaomi'
require 'app_push/igetui'
require 'app_push/igetui/push'
require 'app_push/log'
require 'igetui'
module AppPush
class Push
attr_accessor :igetui_config, :xiaomi_config
def initialize(opts = {})
opts = opts.with_indifferent_access
# {
# base: {
# app_id: '',
# app_key: '',
# master_secret: ''
# template_base: {
# logo: 'ik_logo.png',
# logo_url: 'http://www.ikcrm.com/assets/img/ik_logo.png',
# title: '',
# text: ''
# }
# },
# opts: {
# push_type: 2
# }
# }
@igetui_config = opts['igetui']
# xiaomi default config
# {
# restricted_package_name: 'com.vcooline.aike',
# pass_through: 1,
# title: 'xiaomi push',
# description: 'xiaomi push',
# notify_type: 1
# }
@xiaomi_config = opts['xiaomi']
if @igetui_config.present?
Igetui.config = @igetui_config['opts']
Igetui::Push.config = @igetui_config['base']
end
Xiaomi.config = @xiaomi_config
end
# transmission_content 是真正的推送信息
# {
# transmission_content: opts,
# transmission_type: transmission_type
# }
def push(user_device_ids, message)
message = message.with_indifferent_access
# igetui
Igetui.push(user_device_ids, message)
# xiaomi
Xiaomi.push(user_device_ids, message['transmission_content'])
# huawei
# etc
rescue StandardError => err
Log.error(err)
end
end
end
module AppPush
module Igetui
class << self
attr_accessor :config
def push(user_device_ids, template_data = {})
if user_device_ids.is_a? Array
push_transmission_to_list_igetui(user_device_ids, template_data)
else
push_transmission_to_igetui(user_device_ids, template_data)
end
rescue StandardError => err
AppPush::Log.error(err)
end
def config
(@config || default_config).with_indifferent_access
end
def default_config
{
push_type: 2
}
end
def push_transmission_to_igetui(user_device_id, template_data = {})
template_data = handle_template_data(template_data)
push_igetui_with_asnyc(
user_device_id,
template_data,
template_type: 'TransmissionTemplate'
)
end
def push_transmission_to_list_igetui(user_device_ids, template_data = {})
template_data = handle_template_data(template_data)
push_list_igetui_with_asnyc(
user_device_ids,
template_data,
template_type: 'TransmissionTemplate', pusher_type: 'push_message_to_list'
)
end
private
def handle_template_data(template_data)
template_data = HashWithIndifferentAccess.new(template_data)
transmission_content = HashWithIndifferentAccess.new(template_data.delete(:transmission_content))
transmission_content[:push_type] ||= config[:push_type]
template_data[:transmission_type] ||= 0
template_data[:transmission_content] = transmission_content.as_json
template_data
end
def push_igetui_with_asnyc(user_device_id, template_data = {}, opts = nil)
return if template_data.blank?
opts ||= HashWithIndifferentAccess.new(template_type: 'TransmissionTemplate')
template_data = HashWithIndifferentAccess.new(template_data)
perform(template_data, opts, user_device_id)
end
def push_list_igetui_with_asnyc(user_device_ids, template_data = {}, opts = { pusher_type: 'push_message_to_list' })
return if user_device_ids.blank? || template_data.blank?
opts ||= HashWithIndifferentAccess.new(template_type: 'TransmissionTemplate')
perform(template_data, opts, user_device_ids)
end
def perform(template_data, opts, user_device_ids)
AppPush::Log.info("template_data: #{template_data}")
AppPush::Log.info("opts: #{opts}")
AppPush::Log.info("user_device_ids: #{user_device_ids}")
opts = HashWithIndifferentAccess.new(opts)
igetui = Igetui::Push.new(template_data, opts)
result = igetui.push_message_to(user_device_ids)
AppPush::Log.info("result: #{result}")
end
end
end
end
\ No newline at end of file
module AppPush
module Igetui
class Push
class << self
attr_accessor :pusher, :config
def pusher
@pusher ||= IGeTui.pusher(
config[:app_id],
config[:app_key],
config[:master_secret]
)
end
def config
(@config || default_config).with_indifferent_access
end
def default_config
{
app_id: '',
app_key: '',
master_secret: '',
template_base: {
logo: 'ik_logo.png',
logo_url: 'http://www.ikcrm.com/assets/img/ik_logo.png',
title: '',
text: ''
}
}
end
end
attr_accessor :pusher_type, :template_type, :template, :template_data
PUSHERTYPES = %w[push_message_to_single push_message_to_list push_message_to_app].freeze
TEMPLATETYPES = %w[LinkTemplate NotificationTemplate TransmissionTemplate NotyPopLoadTemplate].freeze
delegate :pusher, to: 'self.class'
delegate :config, to: 'self.class'
def initialize(template_data, opts = {})
opts = HashWithIndifferentAccess.new(opts)
@template_data = HashWithIndifferentAccess.new(template_data || {})
@pusher_type = PUSHERTYPES.include?((opts[:pusher_type]).to_s) ? opts[:pusher_type] : 'push_message_to_single'
@template_type = TEMPLATETYPES.include?((opts[:template_type]).to_s) ? opts[:template_type] : 'NotificationTemplate'
set_message
set_template
set_template_data
set_message_data
end
def push_message_to(client_id)
client_id_list = Array(client_id)
case pusher_type
when 'push_message_to_single'
client_id_list.map do |client_id|
client = IGeTui::Client.new(client_id)
pusher.push_message_to_single(@message, client)
end
when 'push_message_to_list'
content_id = pusher.get_content_id(@message)
clients = client_id_list.map { |client_id| IGeTui::Client.new(client_id) }
pusher.push_message_to_list(content_id, clients)
when 'push_message_to_app'
@message.app_id_list = client_id_list
pusher.push_message_to_app(@message)
end
end
private
def set_template
@template = "IGeTui::#{template_type}".constantize.new
end
def set_message
@message = case pusher_type
when 'push_message_to_single'
IGeTui::SingleMessage.new
when 'push_message_to_list'
IGeTui::ListMessage.new
when 'push_message_to_app'
IGeTui::AppMessage.new
end
end
def set_template_data
case template_type
when 'NotificationTemplate', 'NotyPopLoadTemplate', 'LinkTemplate'
@template_data.reverse_merge!(config[:template_base])
@template_data.each do |k, v|
@template.send("#{k}=", v)
end
@template.set_push_info(*@template_data[:push_info]) if @template_data.key? :push_info
when 'TransmissionTemplate'
@template.transmission_content = @template_data[:transmission_content].is_a?(Hash) ? @template_data[:transmission_content].to_json : @template_data[:transmission_content]
@template.transmission_type = @template_data[:transmission_type] if @template_data.key?(:transmission_type)
@template.set_push_info(*@template_data[:push_info]) if @template_data.key? :push_info
end
end
def set_message_data
@message.data = @template
end
end
end
end
###### useage ##############
# igetui = AppPush::Igetui.new({title: "共产党万岁", text: "中国人民大团结万岁"})
# igetui.push_message_to("ec7ef0d56cf9fa3acddacaf7730b76fd") # client_id should bundle to app_id
#
############################
module AppPush
module Log
class << self
def info(info)
logger.tagged(Time.zone.now) { logger.info info }
end
def error(e)
logger.tagged(Time.zone.now) { logger.error e.message.to_s }
e.backtrace.each { |message| logger.tagged(Time.zone.now) { logger.error message.to_s } }
end
private
def logger
@logger ||= ActiveSupport::TaggedLogging.new(::Logger.new("#{Rails.root}/log/various_push.log", 'weekly'))
end
end
end
end
module AppPush
VERSION = "0.1"
end
module AppPush
module Xiaomi
class << self
attr_accessor :config
def push(registration_id, payload)
if registration_id.present?
registration_id = registration_id.join(',') if registration_id.instance_of? Array
post(message(payload, registration_id))
end
rescue StandardError => err
AppPush::Log.error(err)
end
def message(payload, registration_id)
message = config
message[:registration_id] = registration_id
message[:payload] = payload.to_json
message
end
def config
@config || default_config
end
def default_config
{
restricted_package_name: 'com.vcooline.aike',
pass_through: 1,
title: 'xiaomi push',
description: 'xiaomi push',
notify_type: 1
}
end
def post(params)
AppPush::Log.info("params: #{params.to_json}")
url = 'https://api.xmpush.xiaomi.com/v3/message/regid'
AppPush::Log.info("url: #{url}")
response = HTTParty.post(url, query: params,
headers: {
'Accept' => 'application/json;',
"Authorization": 'key=tQWrpyjb+47hdH/MSmfMGQ=='
},
timeout: 5)
AppPush::Log.info("response: #{response}")
response
end
end
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