Push Notifications

Episode #19 | 18 minutes | published on June 7, 2012
Subscribers Only
In this episode, I set up a push notification server using Rails and Urban Airship, and show the steps required to handle push notifications in an application.

Links

Obtaining a Device Token

  • (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    /
    app setup */

    [application registerForRemoteNotificationTypes:(
    UIRemoteNotificationTypeAlert |
    UIRemoteNotificationTypeBadge
    )];

    [self.window makeKeyAndVisible];
    return YES;
    }

  • (void)application:(UIApplication *)application
    didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSLog(@"Device token: %@", deviceToken);
    NSString *token = [NSString stringWithFormat:@"%@", [deviceToken description]];
    token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
    token = [token stringByReplacingOccurrencesOfString:@"<" withString:@""];
    token = [token stringByReplacingOccurrencesOfString:@">" withString:@""];

    NSLog(@"token string: %@", token); // Send this to your server
    }

  • (void)application:(UIApplication *)application
    didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"Failed: %@", error);
    }
    ```

You'll request this on every app launch. Once the user has agreed, they will not be prompted anymore.

Setting up Rails app

I used Rails 3.2.3 and Ruby 1.9.3 for this episode, but any recent version of Rails/Ruby should work.

rails new push_server

Edit the Gemfile and add a reference to the urbanairship gem:

gem 'urbanairship'

Install it with bundler:

bundle install

Now create an initializer in config/initializers called urban_airship.rb:

Urbanairship.application_key = YOUR_APP_KEY_HERE
Urbanairship.application_secret = ENV["UA_APP_SECRET"]
Urbanairship.master_secret = ENV["UA_APP_MASTER_SECRET"]
Urbanairship.logger = Rails.logger

You'll have to obtain your app key, app secret, and master secret from Urban Airship. You should not check in these secrets to your repository. Instead, leverage environment variables on your dev machines.

To set one up, simply type export UA_APP_SECRET=MY_APP_SECRET_HERE in your shell.

Registering a Device

In order to send push notifications you must first register the device:

Urbanairship.register_device("some_device_token_here")

It's also important to run the feedback rake task from this gem so that your app can be notified when device tokens become inactive. This can happen if a user disables push notifications later on. You should avoid sending notifications to inactive tokens.

Sending a notification

The notification payload is pretty simple:

notification = {
  :schedule_for => [Time.now],
  :device_tokens => [device_token1, device_token2],
  :aps => {
    :alert => "Zombies, run!",
    :badge => 99
  }
}

You can send the notification like this:

Urbanairship.push(notification)
blog comments powered by Disqus