Skip to content

Ruby

Push

StreetHawk provides 3 ways to push to devices:

"Publish" API - basic "Push" API - batch/userlists/traceable/reporting "Campaign" API - flexible segmentation, targeting, timing, reporting

Publish API: This article covers the most basic way to push to a device in StreetHawk is with the Publish API. This takes a single device "installid" and sends a push to it immediately:

require 'net/http'
require 'json'

# Get the <HOST>, <APP_KEY> and <AUTH TOKEN> from the "App Details" page in the StreetHawk Dashboard for your app_key.
HOST = 
APP_KEY = 
AUTH_TOKEN= 

INSTALLID = '7U2XULU76XGKDMWZ' # example only, this is the StreetHawk installid


def simple_push(installid)
  puts "Publishing to: #{HOST}"
  params = {
    "code"=>8010, # type of push to send
    "title"=>"this is the title",
    "message"=>"this is the message",
    #"alert"="this is alert",  # optional 
    #"data"="this is data", # optional 
    "app_key"=>APP_KEY,
    "auth_token"=>AUTH_TOKEN
  }
  if installid == nil
    params["installid"] = INSTALLID
  else
    params["installid"] = installid
  puts JSON::pretty_generate(JSON::parse(http_request('post', HOST + '/v1/installs/publish', params)))
end


# General Helper function for API calls
def http_request(request, host, params)
  uri = URI.parse(host)
  uri.query = URI.encode_www_form(params)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  if request == 'post'
    request = Net::HTTP::Post.new(uri.request_uri)
    response = http.request(request)
  elsif request == 'get'
    request = Net::HTTP::Get.new(uri.request_uri)
    response = http.request(request)
  end
  response.body
end

The push is for both iOS and Android and has a flexible number of options. For example "code"=>8010 is a "Simple Push", other types of push are: Rich Push (open a browser or Web View In-App), Open a Screen in the App, Open the Appstore for Rating or Upgrade, Custom JSON, Feedback Form etc etc

A common usage is to open a specific instance of a screen (e.g a product or place page). For this, use:

"code"=>8010, # Custom JSON 

data"=>"{\"Product\": \"Sample Product\", \"name\": \"this is my name\", \"my_product_number\": 123}",
# other fields

The flexible collection of Push Codes are here.

At the end of simple_push the puts is showing the return result in JSON, you'll probably want to parse it yourself. For example, you get a response like:

{
  "code": 0,
  "value": {
    "i": 49690,
    "aps": {
      "sound": "default",
      "alert": "this is title this is the message"
    },
    "c": 8010,
    "l": 13
  }
}

That gives you the message ID for this specific push in value.i and the actual data that was sent to the device.

Simple Push to a User Id We provide a simple way for targeting installs with a unique customer ID. In this full command-line example we are using an email address but you can use whatever is unique in your own backend. You need to tag the user with the sh_cuid at some time. NOTE: you can't push to anonymous/unregistered users with this method, you will need to user the above installid approach or use campaigns.

require 'optparse'
require 'net/http'
require 'json'

# Get the <HOST>, <APP_KEY> and <AUTH TOKEN> from the "App Details" page in the StreetHawk Dashboard for your app_key.
HOST = 
APP_KEY = 
AUTH_TOKEN= 

def http_request(request, host, params)
  uri = URI.parse(host)
  uri.query = URI.encode_www_form(params)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  if request == 'post'
    request = Net::HTTP::Post.new(uri.request_uri)
    response = http.request(request)
  elsif request == 'get'
    request = Net::HTTP::Get.new(uri.request_uri)
    response = http.request(request)
  end
  response.body
end

def publish(installid)
  params = {
    "code"=>8010,
    "title"=>"this is title",
    "message"=>"this is the message",
    "app_key"=>APP_KEY,
    "auth_token"=>AUTH_TOKEN
  }
  if installid == nil
    params["installid"] = INSTALLID
  else
    params["installid"]=installid
  end
  puts JSON::pretty_generate(JSON::parse(http_request('post', HOST + '/v1/installs/publish', params)))
end


def install_for_cuid(sh_cuid)
  params = {
    "app_key"=>APP_KEY,
    "auth_token"=>AUTH_TOKEN
  }
  if sh_cuid == nil
    puts "Please provide a sh_cuid"
  else
    params["identifier"]=sh_cuid
  end
  list_request = JSON::parse(http_request('post', HOST + '/v1/installs/list', params))
  install_for_cuid = list_request['value']
  if install_for_cuid.length
    install_for_cuid[0]['installid']
  end
end


options = {}
begin
        OptionParser.new do |opts|
          opts.on("-t", "--publish_to_sh_cuid [cuid]","publish for a specified user &amp; app_key") do |cuid|
            options[:publish_to_sh_cuid]=cuid
          end
        end.parse!
rescue OptionParser::InvalidOption => e
        puts "Error(#{e})"
        exit(-1)
end


if options.empty? then
  puts "Please provide an operation as an argument"
  exit(-1)
end
if options[:publish_to_sh_cuid]
  sh_cuid = options[:publish_to_sh_cuid]
  installid = install_for_cuid(sh_cuid)
  if installid
    puts "Will publish for: ",sh_cuid, installid
    publish(installid)
  end
else
  puts "Please provide a valid sh_cuid"
end

Querying/Finding Installs for a User

Users can have multiple installs (devices) - here is a way to get a list for a specified user. Here we are using the "sh_cuid" which should be a unique ID for the user in your CRM/Backend. You can read about it here and here.

You can also use this method to access the "installid" for the Publish (Simple Push) API.

require 'net/http'
require 'json'

# The following settings are found in StreetHawk Dashboard "App Details" section.
HOST = 
APP_KEY = # e.g SHSample
AUTH_TOKEN = 

def install_list(sh_cuid)
  params = {
    "app_key"=>APP_KEY,
    "auth_token"=>AUTH_TOKEN
  }
  if sh_cuid == nil
    install_list_count()
    # return
  else
    params["identifier"]=sh_cuid
  end
  list_request = JSON::parse(http_request('post', HOST + '/v1/installs/list', params))
  install_list = list_request['value']
  puts install_list.length
  if install_list.length
    puts install_list[0]
    puts "this is zeroth element"
  end
end

# General Helper function for API calls
def http_request(request, host, params)
  uri = URI.parse(host)
  uri.query = URI.encode_www_form(params)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  if request == 'post'
    request = Net::HTTP::Post.new(uri.request_uri)
    response = http.request(request)
  elsif request == 'get'
    request = Net::HTTP::Get.new(uri.request_uri)
    response = http.request(request)
  end
  response.body
end

All Installs

To iterate over the list of all installs, just make the call without a "sh_cuid" parameter. Because you will have thousands or millions of users, you need to use the following. Pagination/Iteration is explained here. count= true - to get the total number of installs as a count only. offset, limit - to paginate order_by - to define how results should be returned.

For example: order_by=-created will give you descending order of when the install was created. Refer the the Install API for the fields.

Advanced Push

StreetHawk provides 3 ways to push to devices:

"Publish" API - basic "Push" API - batch/userlists, traceable, reporting "Campaign" API - flexible segmentation, targeting, timing, reporting

Push API: This article covers how to Push to Users. StreetHawk takes a user-centric approach to push simplifying the link between your own CRM/Backend and the StreetHawk cloud. The recipients of this API call can be specified by:

"sh_cuid" - your CRM/Backend ID for the user. This could be a GUID or something like an email address (if that is how the user logs in) usernames - a list/array of user sh_cuids userlist_id - StreetHawk has a Userlist concept that can be accessed via the StreetHawk Console or Userlist API.

By setting the sh_cuid from your App is probably the most important initial step you can do - it allows you to tag users from your CRM/Backend, target and track them. For info about Tagging sh_cuid, see:

Tagging API Android, iOS, PhoneGap, Xamarin Android, Xamarin iOS, Titanium tagging

http://.streethawk.com/v1/docs/tagging.html?highlight=sh_cuid#tagging

require 'net/http'
require 'json'

# Get the <HOST>, <APP_KEY> and <AUTH_TOKEN> from the "App Details" page in the StreetHawk Dashboard for your app_key.
HOST = 
APP_KEY = 
AUTH_TOKEN= 

def push()
  puts "Pushing to: #{HOST}"
  params = {
    "usernames"=>"[\"olly@streethawk.com\", \"david@streethawk.com\"]",
    "code"=>8010,
    "title"=>"this is the title",
    "message"=>"this is the message",
    #"alert"=>"this is alert",
    #"data"=>"this is data",
    "app_key"=>APP_KEY,
    "auth_token"=>AUTH_TOKEN
  }
  puts JSON::pretty_generate(JSON::parse(http_request('post', HOST + '/v1/push', params)))
end


# General Helper function for API calls
def http_request(request, host, params)
uri = URI.parse(host)
uri.query = URI.encode_www_form(params)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
if request == 'post'
request = Net::HTTP::Post.new(uri.request_uri)
response = http.request(request)
elsif request == 'get'
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
end
response.body
end

The push is for both iOS and Android and has a flexible number of options. For example "code"=>8010 is a "Simple Push", other types of push are: Rich Push (open a browser or Web View In-App), Open a Screen in the App, Open the Appstore for Rating or Upgrade, Custom JSON, Feedback Form etc etc

A common usage is to open a specific instance of a screen (e.g a product or place page). For this, use: "code"=>8010, # Custom JSON "data"=> ,

other fields

The flexible collection of Push Codes are here. Documentation for the Push API is here.

At the end of the push function the puts is showing the return result in JSON, you'll probably want to parse it yourself. For example, you get a response like:

{
  "code": 0,
  "value": "5YPZ697CV5"
}

That gives you the "Campaign" ID for this specific push in value.