Skip to content

Instantly share code, notes, and snippets.

@yuki24
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuki24/3c990031cd6740d7bb53 to your computer and use it in GitHub Desktop.
Save yuki24/3c990031cd6740d7bb53 to your computer and use it in GitHub Desktop.
Active Push API example
class BaseNotifier < ActivePush::Base
self.notifier_name = "base_notifier"
default gcm: {collapse_key: '...', delay_while_idle: false},
wp: {
message_id: 'message id',
notification_class: 'notification class',
windowsphone_target: 'target'
}
def welcome(user)
@user = user # `@user` will be accessible from the view template.
ios_device = user.ios_devices.first
android_device = user.android_devices.first
wp_device = user.wp_devices.first
push apn: ios_device.device_token, # uses app/views/user_notifier/welcome.json+apn.jbuilder
gcm: androird_device.registration_id, # uses app/views/user_notifier/welcome.json+gcm.jbuilder
wp: wp_device.uri # uses app/views/user_notifier/welcome.xml+wp.erb
end
def some_complicated_notification(user)
@user = user
ios_device = user.ios_devices.first
wp_device = user.wp_devices.first
push apn: ios_device.device_token,
gcm: {notification_key: user.notification_key},
wp: {
uri: wp_device.uri,
message_id: 'message id',
notification_class: 'notification class',
windowsphone_target: 'target'
}
end
end
notifiaction = BaseNotifier.welcome(user)
# Pushes all the notifications immediately.
notification.push_all_now!
# Pushes a specific notification immediately. Change `apn` to `gcm` or `wp` if needed.
notification.apn.push_now!
# Enqueues a job that pushes a specific notification.
notification.apn.push_later!
notification.push_all_later!
# is the same as doing the following(enqueues 3 jobs)
notification.apn.push_later!
notification.gcm.push_later!
notification.wp.push_later!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment