rpush github文档中介绍了rpush安装使用的方法,现将步骤整理一下,并对某些环节进行说明:
一、服务器端设置
1.将 rpush加入到Gemfile中:
gem 'rpush'
$ bundle install
2.打开项目,执行rpush init 命令
$ cd /path/to/project
$ rpush init
$ bundle exec rake db:migrate
3.生成SSL证书(generate SSL certificates),可以参照外文文档或使用教程,然后将生成的.pem 文件存入项目的public下。
** 注意: 为了避免不必要的麻烦,请一定测试生成的证书是否有效!!! **
4.在seeds.rb文件中键入如下代码,用于将证书信息和app的信息存入rpush自动生成的数据表rpush_apps中:
app = Rpush::Apns::App.new
app.name = "app的包名"
# 将sandbox.pem 文件读入数据库
app.certificate = File.read("#{Rails.root}/public/sandbox.pem")
app.environment = "sandbox" # APNs environment.
app.password = "certificate password"
app.connections = 1
app.save!
5.在需要触发信息推送的方法下添加如下代码,作用是将信息和设备token存入rpush_notifications表,当开启rpush守护进程时,rpush就会将信息发送到设备上:
n = Rpush::Apns::Notification.new
n.app = Rpush::Apns::App.find_by_name("app包名")
n.device_token = "..." # 64-character hex string
n.alert = "hi uubpay!"
n.data = { foo: :bar }
n.save!
6.通过执行migration命令创建apn_devices表,保存ios客户端发送过来的设备信息,例如:
class CreateApnDevices < ActiveRecord::Migration
def change
create_table :apn_devices do |t|
t.integer :user_id
t.string :device_token
t.boolean :active
t.boolean :silent, :default=>false
t.string :sid
t.string :os
t.timestamps null: false
end
end
end
7.给ios客户端提供接口将设备信息保存到apn_devices表,用于设备订阅推送,例如:
#设备订阅推送
def subscribe
device = ApnDevice.find_by_device_token(params[:token])
if !device
device = ApnDevice.new(:device_token=>params[:token])
end
device.active = true
if params[:id]
user = User.find_by_id(params[:id])
if user
device.user_id = user.id
end
else
device.user_id = nil
end
device.os = params[:os]
device.sid = params[:sid]
device.save
render :text=>"ok"
end
routes.rb文件
resource :Users ,:only => [] do
collection do
get :subscribe
end
end
8.开启rpush守护进程
$ cd /path/to/project
$ bundle exec rpush start
二、ios端设置
var receive_ios_rpush;
receive_ios_rpush = function() {
var registerForPush, subscribe;
subscribe = function(e) {
var url;
url = Ti.App.host_url + '/interface/householders/subscribe?token=' + e.deviceToken;
url += '&app=' + Ti.App.id;
url += '&sid=' + Ti.Platform.id;
url += '&id=' + Ti.App.Properties.getObject("householder_id", "").toString();
http_call(url, function(f) {
return Ti.API.log('subscribe success : ' + e.deviceToken);
});
return Ti.App.Properties.setString('ios_token', e.deviceToken);
};
registerForPush = function() {
Ti.Network.registerForPushNotifications({
success: subscribe,
error: function(e) {
Ti.API.warn('push notifications disabled(ios8): ' + e);
return logEvent('push_notification_disable');
},
callback: function(e) {
var alert_dialog;
alert_dialog = Titanium.UI.createAlertDialog({
title: '',
message: e.data.alert,
buttonNames: ['好的'],
cancel: 0
});
return alert_dialog.show();
}
});
return Ti.App.iOS.removeEventListener('usernotificationsettings', registerForPush);
};
Ti.App.iOS.addEventListener('usernotificationsettings', registerForPush);
return Ti.App.iOS.registerUserNotificationSettings({
types: [Ti.App.iOS.USER_NOTIFICATION_TYPE_ALERT, Ti.App.iOS.USER_NOTIFICATION_TYPE_SOUND, Ti.App.iOS.USER_NOTIFICATION_TYPE_BADGE]
});
};
以上代码让应用安装时调用receive_ios_rpush方法,用于首次安装应用时弹出通知框让用户选择是否接受消息通知,接受则向苹果消息推送服务器发送订阅消息通知的请求,请求成功则调用subscribe方法,该方法用于将消息推送服务器返回来的deviceToken等设备信息通过http请求发送给我们的后台服务器。用于用户选择的弹出通知框只能在应用首次安装或卸载24小时(可能)后再次安装时弹出,但如果首次选择了接受推送服务,此后即使不弹出** 通知框 **也会调用subscribe方法.
**注意:使用模拟器是无法实现调用subscribe方法的,因此请一定用真机调试!!! **
赘述一点:如果想让通知框在短时间再次弹出,请做如下操作:
- 卸载应用; Delete your app from the device.
- 重启设备;Turn the device off completely and turn it back on.
- 在系统的设置>通用> 日期和时间中将时间设为一天以后或更久;Go to Settings > General > Date & Time and set the date ahead a day or more.
- 重启设备并重装应用。Turn the device off completely again and turn it back on.