-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NewFeature] xinge ios,android notification push logic
- Loading branch information
Showing
5 changed files
with
198 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# == Schema Information | ||
# | ||
# Table name: user_devices | ||
# | ||
# id :integer not null, primary key | ||
# user_id :integer | ||
# device_type :string(255) | ||
# device_id :string(255) | ||
# device_token :string(255) | ||
# created_at :datetime | ||
# updated_at :datetime | ||
|
||
class UserDevice < ActiveRecord::Base | ||
belongs_to :user | ||
|
||
IOS = 'ios' | ||
ANDROID = 'android' | ||
|
||
def is_ios? | ||
device_type.present? && device_type == UserDevice::IOS | ||
end | ||
|
||
class << self | ||
|
||
def notification_device(user_id) | ||
where(user_id: user_id).order("updated_at desc").first | ||
end | ||
|
||
def register_device_token(user_id, device_token) | ||
device = where(user_id: user_id, device_token: device_token).first | ||
if device.present? | ||
device.touch | ||
else | ||
create(user_id: user_id, device_token: device_token, | ||
device_type: UserDevice::IOS) | ||
end | ||
end | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
module Xinge | ||
class Android < Base | ||
|
||
class << self | ||
|
||
def push_to_single_device(device_token, title, content) | ||
push_single_device(device_token, build_simple_message(title, content)) | ||
end | ||
|
||
protected | ||
|
||
def build_simple_message(title, content) | ||
{ title: title, content: content, vibrate: 1 }.to_json | ||
end | ||
|
||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
module Xinge | ||
class Base | ||
|
||
XINGE_HOST = 'openapi.xg.qq.com' | ||
XINGE_HOST_URI = 'http://openapi.xg.qq.com' | ||
XINGE_API_VERSION = 'v2' | ||
XINGE_ACCESS_ID = Settings.xinge_access_id | ||
XINGE_SECRET_KEY = Settings.xinge_secret_key | ||
XINGE_HTTP_METHOD = :post | ||
XINGE_MESSAGE_TYPE = 1 | ||
|
||
class << self | ||
|
||
def logger | ||
Yell.new do |l| | ||
l.adapter :datefile, "#{Settings.log_path}/push.log", level: 'gte.info' | ||
end | ||
end | ||
|
||
# push 消息(包括通知和透传消息)给单个设备 | ||
def push_single_device(device_token, message, params = {}) | ||
params.merge!({ | ||
device_token: device_token, | ||
message: message, | ||
message_type: XINGE_MESSAGE_TYPE | ||
}) | ||
send_request('push', 'single_device', params) | ||
end | ||
|
||
protected | ||
|
||
def send_request(type, method, params = {}) | ||
request_path = get_request_url(type, method) | ||
params.merge!({access_id: XINGE_ACCESS_ID, timestamp: Time.now.to_i}) | ||
|
||
# sort params and calculate sign | ||
params_string = params.sort.map{ |h| h.join('=') }.join | ||
sign_str = "POST#{XINGE_HOST}#{request_path}#{params_string}#{XINGE_SECRET_KEY}" | ||
sign = Digest::MD5.hexdigest(sign_str) | ||
params.merge!({ sign: sign }) | ||
|
||
begin | ||
logger.info "xinge start push: #{params}" | ||
request_url = "#{XINGE_HOST_URI}#{request_path}" | ||
reuqest_header = {'Content-Type' => 'application/x-www-form-urlencoded'} | ||
response = RestClient.post(request_url, params, reuqest_header) | ||
if response.present? | ||
result = JSON.parse(response) | ||
logger.info "xinge end push: #{result}" | ||
if result | ||
[result["ret_code"], result["err_msg"]] | ||
end | ||
end | ||
rescue => e | ||
puts e.response | ||
return false | ||
end | ||
end | ||
|
||
def get_request_url(type, method) | ||
"/#{XINGE_API_VERSION}/#{type}/#{method}" | ||
end | ||
|
||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
module Xinge | ||
class Ios < Base | ||
|
||
class << self | ||
|
||
def push_to_single_device(device_token, title, content) | ||
push_single_device(device_token, build_simple_message(title, content), | ||
environment_param) | ||
end | ||
|
||
protected | ||
|
||
def environment_param | ||
env_val = Rails.env.production? ? 1 : 2 | ||
{ environment: env_val } | ||
end | ||
|
||
def build_simple_message(title, content) | ||
{ | ||
aps: { | ||
alert: { | ||
title: title, | ||
body: content | ||
}, | ||
sound: 'default', | ||
badge: 5 | ||
} | ||
}.to_json | ||
end | ||
|
||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
module Xinge | ||
class Notification | ||
|
||
class << self | ||
|
||
def reply_push(reply_id) | ||
reply = Reply.where(id: reply_id).first | ||
if reply.present? | ||
content = "#{reply.to_user.name}: 对您发表了新的评论,快去看看吧~" | ||
do_push(reply.to_user_id, content) | ||
end | ||
end | ||
|
||
def do_push(user_id, content) | ||
title = "APP名称" | ||
device = UserDevice.notification_device(user_id) | ||
if device.present? | ||
if device.is_ios? | ||
Xinge::Ios.push_to_single_device(device.device_token, title, content) | ||
else | ||
Xinge::Android.push_to_single_device(device.device_token, title, content) | ||
end | ||
end | ||
end | ||
|
||
end | ||
|
||
end | ||
end |