Skip to content

Instantly share code, notes, and snippets.

@linjunpop
Last active June 23, 2016 04:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save linjunpop/5202365 to your computer and use it in GitHub Desktop.
Save linjunpop/5202365 to your computer and use it in GitHub Desktop.
Sidekiq + Grocer
class Message
include Mongoid::Document
include Mongoid::Timestamps
field :alert, type: String
field :badge, type: Integer, default: 0
field :sound, type: String
field :schedule, type: DateTime
field :custom, type: Hash
after_create :deliver
private
def deliver
MessageWorker.perform_async(self.id.to_s)
end
end
class MessageWorker
include Sidekiq::Worker
sidekiq_options :retry => false
def perform(message_id)
message = Message.find(message_id)
pusher = Grocer.pusher(
certificate: @message.project_pem_file,
gateway: ENV["APN_GATEWAY"],
port: 2195
)
message.devices.each do |device|
notification = Grocer::Notification.new(
device_token: device.token,
alert: message.alert,
badge: message.badge,
sound: message.sound,
custom: message.custom
)
pusher.push(notification)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment