GithubHelp home page GithubHelp logo

ozzie00 / houston Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nomad-cli/houston

0.0 3.0 0.0 841 KB

Apple Push Notifications; No Dirigible Required

Home Page: http://nomad-cli.com

License: MIT License

houston's Introduction

Houston

Push Notifications don't have to be difficult.

Houston is a simple gem for sending Apple Push Notifications. Pass your credentials, construct your message, and send it.

In a production application, you will probably want to schedule or queue notifications into a background job. Whether you're using queue_classic, resque, or rolling you own infrastructure, integrating Houston couldn't be simpler.

Another caveat is that Houston doesn't manage device tokens for you. For that, you should check out Helios

Houston is named for Houston, TX, the metonymical home of NASA's Johnson Space Center, as in Houston, We Have Liftoff!.

It's part of a series of world-class command-line utilities for iOS development, which includes Cupertino (Apple Dev Center management), Shenzhen (Building & Distribution), Venice (In-App Purchase Receipt Verification), and Dubai (Passbook pass generation).

This project is also part of a series of open source libraries covering the mission-critical aspects of an iOS app's infrastructure. Be sure to check out its sister projects: GroundControl, SkyLab, houston, and Orbiter.

Installation

$ gem install houston

Usage

require 'houston'

# Environment variables are automatically read, or can be overridden by any specified options. You can also
# conveniently use `Houston::Client.development` or `Houston::Client.production`.
APN = Houston::Client.development
APN.certificate = File.read("/path/to/apple_push_notification.pem")

# An example of the token sent back when a device registers for notifications
token = "<ce8be627 2e43e855 16033e24 b4c28922 0eeda487 9c477160 b2545e95 b68b5969>"

# Create a notification that alerts a message to the user, plays a sound, and sets the badge on the app
notification = Houston::Notification.new(device: token)
notification.alert = "Hello, World!"

# Notifications can also change the badge count, have a custom sound, have a category identifier, indicate available Newsstand content, or pass along arbitrary data.
notification.badge = 57
notification.sound = "sosumi.aiff"
notification.category = "INVITE_CATEGORY"
notification.content_available = true
notification.custom_data = {foo: "bar"}

# And... sent! That's all it takes.
APN.push(notification)

Error Handling

If an error occurs when sending a particular notification, its error attribute will be populated:

puts "Error: #{notification.error}." if notification.error

Silent Notifications

To send a silent push notification, set sound to an empty string (''):

Houston::Notification.new(:sound => '',
                          :content_available => true)

Persistent Connections

If you want to manage your own persistent connection to Apple push services, such as for background workers, here's how to do it:

certificate = File.read("/path/to/apple_push_notification.pem")
passphrase = "..."
connection = Houston::Connection.new(Houston::APPLE_DEVELOPMENT_GATEWAY_URI, certificate, passphrase)
connection.open

notification = Houston::Notification.new(device: token)
notification.alert = "Hello, World!"
connection.write(notification.message)

connection.close

Feedback Service

Apple provides a feedback service to query for unregistered device tokens, these are devices that have failed to receive a push notification and should be removed from your application. You should periodically query for and remove these devices, Apple audits providers to ensure they are removing unregistered devices. To obtain the list of unregistered device tokens:

Houston::Client.development.devices

Versioning

Houston 2.0 supports the new enhanced notification format. Support for the legacy notification format is available in 1.x releases.

Command Line Tool

Houston also comes with the apn binary, which provides a convenient way to test notifications from the command line.

$ apn push "<token>" -c /path/to/apple_push_notification.pem -m "Hello from the command line!"

Enabling Push Notifications on iOS

AppDelegate.m

- (void)applicationDidFinishLaunching:(UIApplication *)application {
  // ...

  [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
}

- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    NSLog(@"application:didRegisterForRemoteNotificationsWithDeviceToken: %@", deviceToken);

    // Register the device token with a webservice
}

- (void)application:(UIApplication *)application
didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
    NSLog(@"Error: %@", error);
}

Converting Your Certificate

These instructions come from the APN on Rails project, which is another great option for sending push notifications.

Once you have the certificate from Apple for your application, export your key and the apple certificate as p12 files. Here is a quick walkthrough on how to do this:

  1. Click the disclosure arrow next to your certificate in Keychain Access and select the certificate and the key.
  2. Right click and choose Export 2 items….
  3. Choose the p12 format from the drop down and name it cert.p12.

Now covert the p12 file to a pem file:

$ openssl pkcs12 -in cert.p12 -out apple_push_notification.pem -nodes -clcerts

Contact

Mattt Thompson

License

Houston is available under the MIT license. See the LICENSE file for more info.

houston's People

Contributors

aj0strow avatar chelsea avatar chipairon avatar dcaunt avatar dmathieu avatar drodriguez avatar jcxplorer avatar jerryhjones avatar loganhasson avatar mattt avatar mpvosseller avatar nragaz avatar octover avatar roland9 avatar rroblak avatar sideshow avatar soffes avatar sshrpe avatar tomdalling avatar wspruijt avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.