Sample Transactional Push Message

The following Ruby code snippet illustrates how you can quickly start sending push messages to your customers.

# Using Ruby 2.1.2
require 'net/http'
require 'json'
require 'SecureRandom'
 
#
#### Configuration ####
#
 
# Replace these with your real app key, API key and secret:
APP_KEY = "YOUR LOCALYTICS APP KEY"
API_KEY = "THE PROFILE API KEY FOR YOUR ORGANIZATION"
API_SECRET = "THE PROFILE API SECRET FOR YOUR ORGANIZATION"
 
# Replace this with the CustomerID and alert message you'd like to send.
# Always cast it to a string
CUSTOMER_ID = "12345"

ALERT = "Bob, your order is ready for pickup!"
 
#
### End of Configuration ###
#
 

# All methods are available at this endpoint:
api_root = "https://messaging.localytics.com/v1/push";
uri = URI("#{api_root}/#{APP_KEY}")
Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
  post_request = Net::HTTP::Post.new(uri)
  post_request.basic_auth(API_KEY, API_SECRET)
  post_request.content_type = "application/json"
  # Set up the array of messages
  message = {
    :customer_id => CUSTOMER_ID,
    :alert => ALERT	
  }
  
  # The message(s) we want to send must be nested in an array under an "messages" JSON root node
  post_request.body = JSON.dump({:messages => [message], :campaign_key => nil, :request_id => SecureRandom.uuid})
  response = http.request(post_request)

  puts post_request.body
  puts response.code
end