#!/usr/bin/env ruby $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) require 'logger' require 'getoptlong' require 'rubygems' require 'apnserver' require 'base64' require 'socket' def usage puts "Usage: apnsend [switches] (--b64-token | --hex-token) " puts " --server the apn server defaults to a locally running apnserverd" puts " --port <2195> the port of the apn server" puts " --pem the path to the pem file, if a pem is supplied the server defaults to gateway.push.apple.com:2195" puts " --pem-passphrase the pem passphrase" puts " --alert the message to send" puts " --sound the sound to play, defaults to 'default'" puts " --badge the badge number" puts " --custom a custom json string to be added to the main object" puts " --b64-token a base 64 encoded device token" puts " --hex-token a hex encoded device token" puts " --help this message" end opts = GetoptLong.new( ["--server", "-s", GetoptLong::REQUIRED_ARGUMENT], ["--port", "-p", GetoptLong::REQUIRED_ARGUMENT], ["--pem", "-c", GetoptLong::REQUIRED_ARGUMENT], ["--pem-passphrase", "-C", GetoptLong::REQUIRED_ARGUMENT], ["--alert", "-a", GetoptLong::REQUIRED_ARGUMENT], ["--sound", "-S", GetoptLong::REQUIRED_ARGUMENT], ["--badge", "-b", GetoptLong::REQUIRED_ARGUMENT], ["--custom", "-j", GetoptLong::REQUIRED_ARGUMENT], ["--b64-token", "-B", GetoptLong::REQUIRED_ARGUMENT], ["--hex-token", "-H", GetoptLong::REQUIRED_ARGUMENT], ["--help", "-h", GetoptLong::NO_ARGUMENT] ) notification = ApnServer::Notification.new opts.each do |opt, arg| case opt when '--help' usage exit when '--server' ApnServer::Config.host = arg when '--port' ApnServer::Config.port = arg.to_i when '--pem' ApnServer::Config.pem = arg when '--pem-passphrase' ApnServer::Config.password = arg when '--alert' notification.alert = arg when '--sound' notification.sound = arg when '--badge' notification.badge = arg.to_i when '--custom' notification.custom = ActiveSupport::JSON.decode(arg) when '--b64-token' notification.device_token = Base64::decode64(arg) when '--hex-token' notification.device_token = arg.scan(/[0-9a-f][0-9a-f]/).map {|s| s.hex.chr}.join end end if notification.device_token.nil? usage exit else notification.push end