Last active
June 23, 2016 04:33
-
-
Save linjunpop/5202365 to your computer and use it in GitHub Desktop.
Sidekiq + Grocer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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