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