./README.md in houston-2.2.4 vs ./README.md in houston-2.3.0

- old
+ new

@@ -24,26 +24,27 @@ 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") +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>" +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!" +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.sound = 'sosumi.aiff' +notification.category = 'INVITE_CATEGORY' notification.content_available = true notification.mutable_content = true -notification.custom_data = { foo: "bar" } +notification.custom_data = { foo: 'bar' } +notification.url_args = %w[boarding A998] # And... sent! That's all it takes. APN.push(notification) ``` @@ -70,14 +71,14 @@ puts "Error: #{notification.error}." if notification.error ``` ### Silent Notifications -To send a silent push notification, set `sound` to an empty string (`""`): +To send a silent push notification, set `sound` to an empty string (`''`): ```ruby -Houston::Notification.new(sound: "", content_available: true) +Houston::Notification.new(sound: '', content_available: true) ``` ### Mutable Notifications To send a mutable push notification (supported by iOS 10+), set `mutable_content` to `true`: @@ -89,17 +90,17 @@ ### 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: ```ruby -certificate = File.read("/path/to/apple_push_notification.pem") -passphrase = "..." +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!" +notification.alert = 'Hello, World!' connection.write(notification.message) connection.close ``` @@ -116,13 +117,13 @@ In `lib/tasks/notifications.rake`: ```ruby namespace :notifications do task device_token_feedback: [:environment] do - APN.devices.each do |device_hash| + APN.unregistered_devices.each do |device_hash| # Format: { token: token, timestamp: timestamp } - device = Device.find_by(token: device_hash['token']) + device = Device.find_by(token: device_hash[:token]) next unless device.present? # Remove device token device.update_attribute(:device_token, nil) end end @@ -139,13 +140,15 @@ $ apn push "<token>" -c /path/to/apple_push_notification.pem -m "Hello from the command line! " ## Enabling Push Notifications on iOS -### AppDelegate.m +### Objective-C ```objective-c +// AppDelegate.m + - (void)applicationDidFinishLaunching:(UIApplication *)application { // ... [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)]; } @@ -160,9 +163,27 @@ - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { NSLog(@"Error: %@", error); +} +``` + +### Swift +```swift +// AppDelegate.swift + +func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool { + application.registerForRemoteNotifications() + return true +} + +func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { + // Register the device token with a webservice +} + +func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { + print("error \(error)") } ``` ## Converting Your Certificate