Skip to content

Instantly share code, notes, and snippets.

@schmidp
Created May 19, 2013 16:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schmidp/5608224 to your computer and use it in GitHub Desktop.
Save schmidp/5608224 to your computer and use it in GitHub Desktop.
Our example code for our grocer fork
def send_push_message(text, badge_count=0, environment='production', async=true)
data = {}
data[:alert] = text if text
data[:badge] = badge_count if badge_count
if async
Resque.enqueue(SendPushJob, self.id, data, environment)
else
self.send_push(data, environment)
end
end
def send_push(data, environment='production')
notifications = []
self.ios_push_devices.where(environment:environment).collect {|d| d.device_token }.each do |token|
data_to_send = data.merge({:device_token => token})
notifications << Grocer::Notification.new(data_to_send)
end
real_send_push(notifications, environment)
end
def real_send_push(notifications, environment='production')
pusher = Grocer.pusher(
certificate: StringIO.new(self.push_cert_for_env(environment)),
gateway: self.push_gateway(environment)
)
sent = []
maybe_sent = []
failed = []
not_sent = notifications
while not_sent.count > 0
sent, maybe_sent, failed, not_sent = pusher.push(not_sent)
puts "sent: #{sent.count}"
puts "maybe sent: #{maybe_sent.count}"
puts "failed: #{failed.count}"
puts "not_sent: #{not_sent.count}"
puts
if failed.count > 0 and failed[0].error_response
failed_notification = failed[0]
if failed_notification.error_response.status_code == 8 # invalid token
# delete token
ios_push_device = self.ios_push_devices.where(device_token: failed_notification.device_token).first
ios_push_device.destroy
else
# ignore push notifiation for now
end
end
end
true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment