Class: Api::V3::DevicesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/api/v3/devices_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#admin?, #can?, #current_ability, #current_user, #error!, #error_404!, #optional!, #requires!

Instance Method Details

#createObject

记录用户 Device 信息,用于 Push 通知。 请在每次用户打开 App 的时候调用此 API 以便更新 Token 的 last_actived_at 让服务端知道这个设备还活着。 Push 将会忽略那些超过两周的未更新的设备。

POST /api/v3/devices

params:

platform - [ios, android]
token - 用于 Push 的设备信息

22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/api/v3/devices_controller.rb', line 22

def create
  requires! :platform, type: String, values: %w(ios android)
  requires! :token, type: String

  @device = current_user.devices.find_or_initialize_by(platform: params[:platform].downcase,
                                          token: params[:token])
  @device.last_actived_at = Time.now
  @device.save!

  render json: { ok: 1 }
end

#destroyObject

删除 Device 信息,请注意在用户登出或删除应用的时候调用,以便能确保清理掉

DELETE /api/v3/devices

params:

platform - [ios, android]
token - 用于 Push 的设备信息

43
44
45
46
47
48
# File 'app/controllers/api/v3/devices_controller.rb', line 43

def destroy
  requires! :platform, type: String, values: %w(ios android)
  requires! :token, type: String
  current_user.devices.where(platform: params[:platform].downcase, token: params[:token]).delete_all
  render json: { ok: 1 }
end