Class: PushJob

Inherits:
ApplicationJob show all
Defined in:
app/jobs/push_job.rb

Instance Method Summary collapse

Instance Method Details

#perform(user_ids, note = {}) ⇒ Object

user_ids: 用户编号列表 note: { alert: 'Hello APNS World!', sound: 'true', badge: 1 }


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/jobs/push_job.rb', line 6

def perform(user_ids, note = {})
  return false if Setting.apns_pem.blank?

  note[:sound] ||= 'true'
  devices = Device.where(user_id: user_ids).all.to_a
  devices.reject! { |d| !d.alive? }
  tokens = devices.collect(&:token)
  return false if tokens.blank?

  notification = RubyPushNotifications::APNS::APNSNotification.new tokens, { aps: note }
  pusher = RubyPushNotifications::APNS::APNSPusher.new(Setting.apns_pem, !Rails.env.production?)
  pusher.push [notification]
  Rails.logger.tagged("PushJob") { Rails.logger.info "send to #{tokens.size} devices #{note} status: #{notification.success}"  }
  notification.success
end