The Urbanairship Gem: Sending Push Notifications with Ruby

If you’re building an app that sends push notifications, Urban Airship is a service that can save you a lot of headache. They provide a common API for sending notifications to iOS, Android and Blackberry devices, as well as some useful features like notification batching, scheduling, and the ability to tag devices to make sending a notification to a large group of users more manageable.

At Groupon we wrote the urbanairship gem to wrap these API interactions, which you can install with gem install urbanairship.

Registering a device token

Before sending notifications to a device, you have to register it with UA. The simplest way to do this is:

Urbanairship.register_device('DEVICE-TOKEN')

You can also give it an alias and a set of tags.

Urbanairship.register_device('DEVICE-TOKEN',
  :alias => 'user-1234',
  :tags => ['chicago-users']
)

The registration call is idempotent. If you want to change a device’s tags or aliases later, you can resend the registration request. Just note that any attributes that are missing from the request will be removed, so be sure to include every tag and alias that you want to associate with that device token each time you make the registration call.

You can also set a ‘quiet time’ and timezone for each device. Check out the Urban Airship API docs for more options.

Sending a notification

Once your device tokens have been registered, sending simple notifications is easy.

Urbanairship.push({
  :device_tokens => ['DEVICE-TOKEN'],
  :aps => {:alert => 'You have a new message!', :badge => 1}
})

You can also specify tags, aliases, and even scheduled delivery times for your notifications. This code sends a push notification, delayed by one hour, to all the devices you’ve tagged with ‘chicago-users’.

Urbanairship.push({
  :tags => ['chicago-users'],
  :schedule_for => [1.hour.from_now],
  :aps => {:alert => 'Hello Chicago!', :badge => 1}
})

The Urban Airship API docs detail even more options that you can specify.

Batched and broadcast notifications

If your back-end system uses some sort of batch process for generating and sending push notifications to multiple users, you can use Urban Airship’s batch push method to cut down on the number of API requests you need to make.

Urbanairship.batch_push(
  {
    :device_tokens => ['DEVICE-TOKEN'],
    :aps => {:alert => 'Message one', :badge => 1}
  },
  {
    :device_tokens => ['DEVICE-TOKEN-TWO'],
    :aps => {:alert => 'Message two', :badge => 1}
  }
)

You can also send a message to ALL of your app’s registered device tokens with the broadcast push method.

Urbanairship.broadcast_push({
  :aps => {:alert => 'Hello EVERYBODY!', :badge => 1}
})

The feedback API

Sometimes a user will opt-in for push notifications on their device and then later disable them or uninstall your app. In these cases, if you try to send a push notification to that device, it will fail and Apple will register the failure in their feedback API. If you send too many repeat notifications to devices that don’t want to receive them, Apple will send you a warning or even revoke your ability to send push notifications.

Fortunately, Urban Airship steps in to help once again. If they notice you trying to send a notification to a device that can’t receive it, UA will refrain from forwarding that notification along to Apple, sparing you from their wrath. But this is really just a safety measure. They also offer a feedback API which you can use to find tokens that have opted-out. You should poll this API periodically and, for each token that comes back, delete or disable it on your end.

Urbanairship.feedback(24.hours.ago) # =>
# [
#   {
#     "marked_inactive_on"=>"2011-06-03 22:53:23",
#     "alias"=>nil,
#     "device_token"=>"DEVICE-TOKEN-ONE"
#   },
#   {
#     "marked_inactive_on"=>"2011-06-03 22:53:23",
#     "alias"=>nil,
#     "device_token"=>"DEVICE-TOKEN-TWO"
#   }
# ]

This retrieves all devices which rejected a notification in the last 24 hours. We’d then go through our database and mark those tokens as inactive or just get rid of them.

That’s it! With Urban Airship and a few lines of Ruby you can send cross-platform push notifications.

posted on January 4, 2012

The triviality of copyright tells you that when other sectors of the economy start to evince concerns about the internet and the PC, that copyright will be revealed for a minor skirmish, and not a war. Why would other sectors nurse grudges against computers? Well, because the world we live in today is /made/ of computers. We don’t have cars anymore, we have computers we ride in; we don’t have airplanes anymore, we have flying Solaris boxes with a big bucketful of SCADA controllers [laughter]; a 3D printer is not a device, it’s a peripheral, and it only works connected to a computer; a radio is no longer a crystal, it’s a general-purpose computer with a fast ADC and a fast DAC and some software.

The grievances that arose from unauthorized copying are trivial, when compared to the calls for action that our new computer-embroidered reality will create

Regardless of whether you think these are real problems or merely hysterical fears, they are nevertheless the province of lobbies and interest groups that are far more influential than Hollywood and big content are on their best days, and every one of them will arrive at the same place — “can’t you just make us a general purpose computer that runs all the programs, except the ones that scare and anger us? Can’t you just make us an Internet that transmits any message over any protocol between any two points, unless it upsets us?”

As a member of the Walkman generation, I have made peace with the fact that I will require a hearing aid long before I die, and of course, it won’t be a hearing aid, it will be a computer I put in my body. So when I get into a car — a computer I put my body into — with my hearing aid — a computer I put inside my body — I want to know that these technologies are not designed to keep secrets from me, and to prevent me from terminating processes on them that work against my interests.

Freedom in the future will require us to have the capacity to monitor our devices and set meaningful policy on them, to examine and terminate the processes that run on them, to maintain them as honest servants to our will, and not as traitors and spies working for criminals, thugs, and control freaks.

posted on December 31, 2011

Seen in Warsaw.

Seen in Warsaw.

posted on March 30, 2011

posted on November 6, 2010

Liu Xiaobo, 54, a literary critic and philosopher, was awarded the Nobel Peace Prize last Friday. He had no idea that he had even been nominated — news of a political nature is taboo in the Jinzhou prison.

posted on October 12, 2010

Notice that MIT’s idea-centric process (e.g., finding the right ideas is key) contrasts with the progress-centric process (e.g., getting started is key) that dominates popular discussion on getting things done.

posted on September 14, 2010

What your fiercest rival does badly, do incredibly well.

posted on April 28, 2010

That’s why Jordan Mechner’s advice — and it’s so beautiful — is to proceed from 1 to 3 to 2. Go right from the inspiration — the vision — to actually making it. Don’t think it through. Don’t talk about it. Don’t plan it. Dive in and start making it happen. If you do that — if you can start rocking — you’ll get some momentum, and when you have some momentum then the project has a chance, because now you’re into it. It’s going somewhere, it’s tangible. Sure, you’ll still run up against problems to solve and decisions to make, but you’ll approach these in the moment and solve them in the moment. You’ll solve them so you can keep moving.

posted on March 25, 2010

Being intelligent is like having a knife. If you train every day in using the knife, you will be invincible. If you think that just having a knife will make you win any battle you fight, then you will fail.

posted on March 5, 2010

The city walls of Xi’an, one of the four ancient capitals of China.

The city walls of Xi’an, one of the four ancient capitals of China.

posted on January 19, 2010