Skip to content

Instantly share code, notes, and snippets.

@gregblake
Last active March 10, 2023 13:13
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 gregblake/89302973e08ecc9195ed289433960f66 to your computer and use it in GitHub Desktop.
Save gregblake/89302973e08ecc9195ed289433960f66 to your computer and use it in GitHub Desktop.
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