7

What is the best way to send push notifications through Urban AirShip using a Rails 3 web app?

From Urban Airship documentation:

An HTTP POST to /api/push/broadcast/ sends the given notification to all registered device tokens for the application. The payload is in JSON with content-type application/json, with this structure:

{
    "aps": {
         "badge": 15,
         "alert": "Hello from Urban Airship!",
         "sound": "cat.caf"
    },
    "exclude_tokens": [
        "device token you want to skip",
        "another device token you want to skip"
    ]
}

I really don't know where to start!

Mohit Jain
  • 43,139
  • 57
  • 169
  • 274
Abramodj
  • 5,709
  • 9
  • 49
  • 75
  • Please, who gave me the "-1" can explain me why he did it? It would be more constructive – Abramodj Jul 08 '11 at 10:05
  • Well, my guess is that you got it because your question did not show any research effort. Any Google query for a combination of the words Rails, JSON, Urban Airship and POST would have given you plenty of starting points. – Martijn Jul 08 '11 at 11:02
  • But it's hours I'm looking for something on the web! And there's nothing useful. Ok, if t is as simple as doing a google search... why nobody is answering? – Abramodj Jul 08 '11 at 11:16
  • Common dude. I got in the very first shot. Make sure to search adding github keyword too. – Mohit Jain Aug 24 '11 at 08:09

4 Answers4

23

Better way is to use UrbanAirship Groupon version. Their docs specify every thing very clearly and it will much neater code. Worked and tested in my application.

From the read me file, (with some more comments and all):-

Note: if you are using Ruby 1.8, you should also install the system_timer gem for more reliable timeout behaviour. See http://ph7spot.com/musings/system-timer for more information. Baically

  gem install system_timer

Installation

 gem install urbanairship 
 # or specify in your gem file 
 gem "urbanairship"

Configuration define all this in initializes directory and then make sure u restart your application.

    Urbanairship.application_key = 'application-key'
    Urbanairship.application_secret = 'application-secret'
    Urbanairship.master_secret = 'master-secret'
    Urbanairship.logger = Rails.logger
    Urbanairship.request_timeout = 5 # default

Usage

Registering a device token

    Urbanairship.register_device 'DEVICE-TOKEN' # => true

Unregistering a device token

    Urbanairship.unregister_device 'DEVICE-TOKEN' # => true

Sending a push notification (for instant delivery remove schedule_for attribute)

    notification = {
      :schedule_for => 1.hour.from_now,
      :device_tokens => ['DEVICE-TOKEN-ONE', 'DEVICE-TOKEN-TWO'],
      :aps => {:alert => 'You have a new message!', :badge => 1}
    }

    Urbanairship.push notification # => true

Batching push notification sends (for instant delivery remove schedule_for attribute)

    notifications = [
      {
        :schedule_for => 1.hour.from_now,
        :device_tokens => ['DEVICE-TOKEN-ONE', 'DEVICE-TOKEN-TWO'],
        :aps => {:alert => 'You have a new message!', :badge => 1}
      },
      {
        :schedule_for => 3.hours.from_now,
        :device_tokens => ['DEVICE-TOKEN-THREE'],
        :aps => {:alert => 'You have a new message!', :badge => 1}
      }
    ]

        Urbanairship.batch_push notifications # => true

Sending broadcast notifications Urbanairship allows you to send a broadcast notification to all active registered device tokens for your app.(for instant delivery remove schedule_for attribute)

    notification = {
      :schedule_for => 1.hour.from_now,
      :aps => {:alert => 'Important announcement!', :badge => 1}
    }

    Urbanairship.broadcast_push notification # => true
Mohit Jain
  • 43,139
  • 57
  • 169
  • 274
0

In Rails 4.2.x and Ruby 2.3.x, I'm using the 'urbanairship' gem, it is best to reference the most recent documentation found here.

Example of sending to ios:

def deliver_push_notification recipient_uuid, message
  ua             = Urbanairship
  client         = ua::Client.new(key: ENV["URBAN_AIRSHIP_APP_KEY"], secret: ENV["URBAN_AIRSHIP_MASTER_SECRET"])
  p              = client.create_push
  p.audience     = ua.ios_channel(recipient_uuid)
  p.notification = ua.notification(ios: ua.ios(alert: message))
  p.device_types = ua.device_types(["ios"])
  p.send_push
end
wintondeshong
  • 1,257
  • 15
  • 19
0

Check out this example:

https://github.com/bhedana/urbanairship_on_rails

It was linked from the Urban Airship support site

Martijn
  • 1,662
  • 15
  • 21
  • Ok thanks for this but I've tried it and when i start my server (rails s) it throws the following error: "/Users/abramo/.rvm/gems/ruby-1.9.2-p180/gems/urbanairship_on_rails-0.0.1/lib/urbanairship_on_rails/models/apn/broadcast_notification.rb:18:in `': uninitialized constant APN::BroadcastNotification::AASM (NameError) " – Abramodj Jul 08 '11 at 10:11
  • @Abramodj Check out the readme, make sure you have the JSON and Acts As State Machine gems instelled. Also, since this example is from 2008, I'm not sure it runs under Ruby 1.9.2. – Martijn Jul 08 '11 at 11:00
  • Yes i have those gems installed. So what can I do? Nothing, this ?$&)£?!% airship is not working in Rails. Lot of time lost for nothing. – Abramodj Jul 08 '11 at 11:14
  • Urban Airship work perfectly on Rails i've realized one project based entirely on Urban Airship for an customer, you must work much harder instead wait another one work for you.... – Joel AZEMAR Jul 08 '11 at 11:50
  • So please can you help me and tell me where to look instead? I'm working hard, but i just can't find anything to look at! – Abramodj Jul 08 '11 at 13:02
  • Ok, i found out i was encountering a this bug: https://github.com/geemus/fog/issues/293 . Now moved the 'pg' gem at the bottom of my gemfile and everythinkg works fine! – Abramodj Jul 10 '11 at 10:56
  • FYI- at the time of this comment, the last update to urbanairship_on_rails is 3yrs ago. It looks like the groupon on is maintained much more frequently (updated 2 days ago). – Justin Jan 13 '13 at 05:07
0

Ok, this is a simple way of sending a urban airship broadcast from ROR controller:

require 'net/http'
require 'net/https'
require 'open-uri'    

app_key = 'JJqr...'
app_secret = 'lAu7...'
master_secret = 'K81P...'

payload ={
    "aps" => {"badge"  => "0", "alert" => "My own message", "sound" => ""}
}.to_json

full_path = 'https://go.urbanairship.com/api/push/broadcast/'
url = URI.parse(full_path)    
req = Net::HTTP::Post.new(url.path, initheader = {'Content-Type' =>'application/json'})
req.body = payload
req.basic_auth app_key, master_secret

con = Net::HTTP.new(url.host, url.port)
con.use_ssl = true

r = con.start {|http| http.request(req)}

logger.info "\n\n##############\n\n  " + "Resonse body: " + r.body + "  \n\n##############\n\n"  

Cheers!

Abramodj
  • 5,709
  • 9
  • 49
  • 75