Last active
August 29, 2015 14:04
-
-
Save ChengLong/fd628a24b8b28a2cf4f1 to your computer and use it in GitHub Desktop.
Push Module
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
require 'houston' | |
require 'gcm' | |
module Pusher | |
APN = Houston::Client.production | |
APN.certificate = File.read(File.expand_path('path/to/push_cert.pem', __FILE__)) | |
# Apple has hard limit on the payload of push notification. The maximum size allowed for a notification payload is 256 bytes; | |
# If you have other fields in the notification, you need to tweak this value to fit your needs. Be careful about this. | |
MAX_ALERT_LENGTH = 140 | |
def push_to_ios(ios_tokens, alert, custom_data) | |
# create all notifications | |
notifications = ios_tokens.compact.uniq.map do |token| | |
notification = Houston::Notification.new( | |
device: token, | |
alert: (alert || 'No Title').truncate(MAX_ALERT_LENGTH), | |
sound: custom_data['sound'] || 'default.aif', | |
badge: 1 # set app badge to the number of unread alerts | |
) | |
notification.custom_data = custom_data | |
notification | |
end | |
APN.push notifications | |
end | |
# GCM can only push 1k tokens at a time | |
GCM_BATCH_LIMIT = 1000 | |
GCM_API_KEY = '...' | |
def push_to_android(android_tokens, data) | |
clean_tokens = android_tokens.compact.uniq | |
gcm = GCM.new(GCM_API_KEY) | |
clean_tokens.each_slice(GCM_BATCH_LIMIT) {|batch| gcm.send_notification(batch, data: data)} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment