Skip to content

Instantly share code, notes, and snippets.

@nakiostudio
Last active May 17, 2016 08:13
Show Gist options
  • Save nakiostudio/43734c375c287bde1cf8b7038ea8b8c7 to your computer and use it in GitHub Desktop.
Save nakiostudio/43734c375c287bde1cf8b7038ea8b8c7 to your computer and use it in GitHub Desktop.
Easily customisable Rake task to send APNS push notifications
require "rake"
require "houston"
namespace :push do
# Enables console logging
STDOUT.sync = true
# Instructions
# $> sudo gem install rake
# $> sudo gem install houston
# $> rake push:send[development,'This is a test',03a31ada5eae18cc5a71715abbf7abe5c5d943af48e264cb042e506c4adbfcca]
desc "Sends a push notification to the specified enviroment"
task :send, [:enviroment, :alert, :token] do |t, args|
raise "Missing environment, alert or device token" if args.alert.nil? || args.token.nil? || args.environment.nil?
# Create APN client
apn = Houston::Client.development
apn = Houston::Client.production if args.environment == "production"
apn.certificate = File.read(ENV["APN_CERTIFICATE_PATH"])
# Create notification
notification = create_push(
args.token,
args.alert,
custom_payload
)
# Log
puts "\nSending push notification:\n\n\t- Alert: \"#{args.alert}\"\n\t- Token: #{args.token}\n"
# Send push
apn.push(notification)
end
# Aux methods
def create_push(token, title, body)
notification = Houston::Notification.new(device: token)
notification.alert = title
notification.custom_data = body
notification.content_available = true
return notification
end
def custom_payload()
payload = {
custom: {
url: "deeplink://something?something=something"
}
}
return payload
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment