最近,极光推出了新版本的服务器端sdk,但是仅针对ruby版本2.2以上,那么对于老项目如何才能使用极光推送服务呢?
前言:
对于ROR项目来说,应该使用 jpush-api-ruby-client
低版本的ruby,应该使用 3.2.1版本,具体步骤如下:
1.安装gem
gem 'jpush', '~> 3.2', '>= 3.2.1'
这点需要注意下,虽然是jpush-api-ruby-client,但安装的时候,默认是‘jpush’
2.新建一个.rb文件,需要手动引入‘jpush’,基本代码如下:
#encoding: utf-8
require 'jpush'
class PushV3
#app_key = "b983f98cef76f3dcbec26d95"
#master_secret = "e9ae28acea4fd7e7dd74534b"
#reg_id = '190e35f7e044c642639'
#send android WithExtrasMessage
def self.send_dev_android(app_key, secret, reg_id, msg)
begin
client = JPush::JPushClient.new(app_key, secret)
logger = Logger.new(STDOUT)
payload1 = JPush::PushPayload.build(
:platform => JPush::Platform.all,
:audience => JPush::Audience.build(:registration_id => [reg_id]),
:notification => JPush::Notification.build(
:android => JPush::AndroidNotification.build(
:alert => "服务单01201607122395,分派给了你。",
:title => "您有新的分配单!",
:builder_id => 1,
:extras => {:key1 => 'value1', :key2 => 'value2'})),
# :message => JPush::Message.build(
# :msg_content => "message content test",
# :title => "message title test",
# :content_type => "message content type test",
# :extras => {:key1 => "value1", :key2 => "value2"})
)
result = client.sendPush(payload1)
logger.debug("Got result " + result.toJSON)
rescue Exception => ex
if result
if result[:error]
Rails.logger.error "JPush send fail! response code: #{result[:error]}"
end
end
Rails.logger.error ex
end
end
end
推送有通知(notification)和消息(message)两种,通知,就是手机下滑导航处的提示,消息是应用内部的提示,具体细则去看官方文档
3.注意事项
1.不知什么原因,刚安装了gem包后,无法调用,过了一会才可以…………
2.……