Skip to content

Instantly share code, notes, and snippets.

@gregblake
Last active Dec 18, 2020
Embed
What would you like to do?
Push Notifications from the Rails console
Rpush.embed # to deliver push notifications in the Rails console on notification save
# Selecting the mobile device that'll receive the notification:
greg = User.find_by(login: "greg.blake")
devices = greg.mobile_devices
@mobile_device = greg.mobile_devices.last
# Sending a notification to the device
n = Rpush::Apns2::Notification.new;
n.app = @mobile_device.app;
n.device_token = @mobile_device.device_token;
n.alert = Faker::Matz.quote;
apns_topic = @mobile_device.app&.name;
n.data = {'headers' => { 'apns-topic' => apns_topic }};
n.save # This delivers the push notification
# If n.save returns false, you can see what went wrong with n.errors.full_messages
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment