Commit 66a6f5b9 by Victor Wang

支持 ios 推送

parent 4a122fa4
...@@ -29,7 +29,6 @@ end ...@@ -29,7 +29,6 @@ end
require "igetui/version" require "igetui/version"
require 'protobuf/GtReq.pb' require 'protobuf/GtReq.pb'
require "igetui/push_info"
require "igetui/template" require "igetui/template"
require "igetui/validate" require "igetui/validate"
require "igetui/message" require "igetui/message"
......
module IGeTui
# PushInfo support Apple Push Notification
class PushInfo < GtReq::PushInfo
STRING_ATTRIBUTES = %i(
action_loc_key badge message sound
action_key payload loc_key loc_args launch_image
).freeze
attr_accessor *STRING_ATTRIBUTES
def initialize
STRING_ATTRIBUTES.each { |attr| instance_variable_set("@#{attr}", '') }
end
def update_properties(args)
args.each { |k, v| instance_variable_set("@#{k}", v) }
@actionLocKey = action_loc_key
@locKey = loc_key
@locArgs = loc_args
@launchImage = launch_image
@actionKey = action_key
end
# alias_method :actionLocKey, :action_loc_key
# alias_method :locKey, :loc_key
# alias_method :locArgs, :loc_args
# alias_method :launchImage, :launch_image
# alias_method :actionKey, :action_key
end
end
...@@ -14,7 +14,7 @@ module IGeTui ...@@ -14,7 +14,7 @@ module IGeTui
data = { data = {
'action' => 'pushMessageToSingleAction', 'action' => 'pushMessageToSingleAction',
'appkey' => app_key, 'appkey' => app_key,
'clientData' => base64Str(template), 'clientData' => template.get_client_data(self),
'transmissionContent' => template.transmission_content, 'transmissionContent' => template.transmission_content,
'isOffline' => message.is_offline, 'isOffline' => message.is_offline,
'offlineExpireTime' => message.offline_expire_time, 'offlineExpireTime' => message.offline_expire_time,
...@@ -50,7 +50,7 @@ module IGeTui ...@@ -50,7 +50,7 @@ module IGeTui
data = { data = {
'action' => 'pushMessageToAppAction', 'action' => 'pushMessageToAppAction',
'appkey' => app_key, 'appkey' => app_key,
'clientData' => base64Str(template), 'clientData' => template.get_client_data(self),
'transmissionContent' => template.transmission_content, 'transmissionContent' => template.transmission_content,
'isOffline' => message.is_offline, 'isOffline' => message.is_offline,
'offlineExpireTime' => message.offline_expire_time, 'offlineExpireTime' => message.offline_expire_time,
...@@ -91,7 +91,7 @@ module IGeTui ...@@ -91,7 +91,7 @@ module IGeTui
data = { data = {
'action' => 'getContentIdAction', 'action' => 'getContentIdAction',
'appkey' => app_key, 'appkey' => app_key,
'clientData' => base64Str(template), 'clientData' => template.get_client_data(self),
'transmissionContent' => template.transmission_content, 'transmissionContent' => template.transmission_content,
'isOffline' => message.is_offline, 'isOffline' => message.is_offline,
'offlineExpireTime' => message.offline_expire_time, 'offlineExpireTime' => message.offline_expire_time,
...@@ -113,11 +113,6 @@ module IGeTui ...@@ -113,11 +113,6 @@ module IGeTui
private private
def base64Str(template)
string = template.get_transparent(self).serialize_to_string
Base64.strict_encode64 string
end
def connect def connect
time_stamp = Time.now.to_i time_stamp = Time.now.to_i
sign = Digest::MD5.hexdigest(app_key + time_stamp.to_s + master_secret) sign = Digest::MD5.hexdigest(app_key + time_stamp.to_s + master_secret)
......
...@@ -5,7 +5,6 @@ module IGeTui ...@@ -5,7 +5,6 @@ module IGeTui
def initialize def initialize
@transmission_type = 0 @transmission_type = 0
@transmission_content = '' @transmission_content = ''
@push_info = PushInfo.new
end end
def get_transparent(pusher) def get_transparent(pusher)
...@@ -15,7 +14,7 @@ module IGeTui ...@@ -15,7 +14,7 @@ module IGeTui
transparent.taskId = '' transparent.taskId = ''
transparent.action = 'pushmessage' transparent.action = 'pushmessage'
transparent.actionChain = get_action_chain transparent.actionChain = get_action_chain
transparent.pushInfo = push_info transparent.pushInfo = get_push_info
transparent.appId = pusher.app_id transparent.appId = pusher.app_id
transparent.appKey = pusher.app_key transparent.appKey = pusher.app_key
transparent transparent
...@@ -29,20 +28,41 @@ module IGeTui ...@@ -29,20 +28,41 @@ module IGeTui
raise NotImplementedError, 'Must be implemented by subtypes.' raise NotImplementedError, 'Must be implemented by subtypes.'
end end
def get_client_data(pusher)
string = self.get_transparent(pusher).serialize_to_string
Base64.strict_encode64 string
end
def get_push_info
@push_info || init_push_info
end
# NOTE: # NOTE:
# iOS Pusher need the top four fields of 'push_info' are required. # iOS Pusher need the top four fields of 'push_info' are required.
# options can be includes [:payload, :loc_key, :loc_args, :launch_image] # options can be includes [:payload, :loc_key, :loc_args, :launch_image]
# http://docs.igetui.com/pages/viewpage.action?pageId=590588 # http://docs.igetui.com/pages/viewpage.action?pageId=590588
def set_push_info(action_loc_key, badge, message, sound, options = {}) def set_push_info(action_loc_key, badge, message, sound, options = {})
args = options.merge({ init_push_info
action_loc_key: action_loc_key, @push_info.actionLocKey = action_loc_key
badge: badge, @push_info.badge = badge.to_s
message: message, @push_info.message = message
sound: sound @push_info.sound = sound
}) @push_info.payload = options[:payload]
@push_info.update_properties(args) @push_info.locKey = options[:loc_key]
@push_info.locArgs = options[:loc_args]
@push_info.launchImage = options[:launch_image]
# validate method need refactoring. # validate method need refactoring.
Validate.new.validate(args) # Validate.new.validate(args)
end
private
def init_push_info
@push_info = GtReq::PushInfo.new
@push_info.message = ''
@push_info.actionKey = ''
@push_info.sound = ''
@push_info.badge = ''
@push_info @push_info
end end
......
require 'json'
module IGeTui module IGeTui
class Validate class Validate
def validate(args = {}) def validate(args = {})
payload_map = get_payload(args) # payload_map = get_payload(args)
json = JSON.generate payload_map # json = JSON.generate payload_map
if (json.length > 256) # if (json.length > 256)
raise ArgumentError.new("PushInfo length over limit: #{json.length}. Allowed: 256.") # raise ArgumentError.new("PushInfo length over limit: #{json.length}. Allowed: 256.")
# end
is_validate = validate_payload(args)
unless is_validate
payload_len = validate_payload_length(args)
raise ArgumentError.new("PushInfo length over limit: #{payload_len.length}. Allowed: 256.")
end
end
def validate_payload(args)
length = validate_payload_length(args)
length <= 256
end
def validate_payload_length(args)
json = process_payload(args)
json.length
end
def process_payload(args)
is_valid = false
pb = Payload.new
if !args[:loc_key].nil? && args[:loc_key].length > 0
pb.alert_loc_key = args[:loc_key]
if !args[:loc_args].nil? && args[:loc_args].length > 0
pb.alert_loc_args = args[:loc_args].split(",")
end
is_valid = true
end
if !args[:message].nil? && args[:message].length > 0
pb.alert_body = args[:message]
is_valid = true
end
if !args[:action_loc_key].nil? && args[:action_loc_key].length > 0
pb.alert_action_loc_key = args[:action_loc_key]
end
if !args[:launch_image].nil? && args[:launch_image].length > 0
pb.alert_launch_image = args[:launch_image]
end end
badge_num = args[:badge].to_i
if badge_num >= 0
pb.badge = badge_num
is_valid = true
end
if !args[:sound].nil? && args[:sound].length > 0
pb.sound = args[:sound]
end
if !args[:payload].nil? && args[:payload].length > 0
pb.add_param("payload", payload)
end
unless is_valid
puts "one of the params(locKey,message,badge) must not be null"
end
json = pb.to_s
if json.nil?
puts "payload json is null"
end
json
# do something
end end
def get_payload(args = {}) def get_payload(args = {})
......
module IGeTui module IGeTui
VERSION = "1.1.0" VERSION = "1.2.0"
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