simple iOS push notifications



simple iOS push notifications

0 0


simple-push-notifications


On Github pivotalsquid / simple-push-notifications

simple iOS push notifications

motivation

i wanted to be able to send a notififcation to my phone from a curl command, perhaps at the end of a long running script,

i wanted to learn a little about swift

what was the minimum needed to make that happen?

  • curl command
  • server process
  • worker/job
  • iOS app

curl command

						curl -X POST -H "Content-Type: application/json" -d '{"msg": "Ping!"}' "http://localhost:3000/ping"
					

server process

simple endpoint to accept the curl request and create a job

						class PingController < ApplicationController
  def create
    PingJob.perform_later(params['msg'])
    respond_to do |format|
      format.json { head :ok }
    end
  end
end
					

worker

						require 'houston'
class PingJob < ActiveJob::Base
  queue_as :pingme

  def perform(*args)
    apn = Houston::Client.development
    apn.certificate = File.read('config/certs/cert_and_key.pem')
    apn.passphrase = 'password!'
    device_token = '<6d67435d de8dbaea 10f6d693 ..... e7183199>'
    notification = Houston::Notification.new(device: device_token)
    notification.alert = args[0]
    notification.sound = 'sosumi.aiff'
    apn.push(notification)
  end
end
					

ios app

create a single view app that does almost nothing

didFinishLaunchingWithOptions

            func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
  let settings =
      UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound],
      categories: nil)
  UIApplication.sharedApplication().registerUserNotificationSettings(settings)
  UIApplication.sharedApplication().registerForRemoteNotifications()
  return true
}
          

implement method to get device token

            func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
        print("Got token data! \(deviceToken)")
    }
          

...and print errors

            func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
        print("Couldn't register: \(error)")
    }
          

ready to send a ping!

so, what did i end up learning?

not swift

code examples

objective-c examples are everywhere

swift, nowhere to be found

learning xcode

creating app id

apn ssl certificate

convoluted mess converting .p12 to pem, etc

debugging push notifications is hard

can only be done on the sending side

nothing available on the apple side

weird ssl errors

            verify error:num=20:unable to get local issuer certificate
            
          

certificate authority files are your friends

frustration

as simple as this seemed, there are a lot of pieces

configuring the certs properly is a pain

simple iOS push notifications