Skip to content

Instantly share code, notes, and snippets.

@naquad
Created October 16, 2011 12:58
Show Gist options
  • Save naquad/1290847 to your computer and use it in GitHub Desktop.
Save naquad/1290847 to your computer and use it in GitHub Desktop.
# Converts the supplied options into the JSON needed for Apple's push notification servers.
# Extracts :alert, :badge, and :sound keys into the 'aps' hash, merges any other hash data
# into the root of the hash to encode and send to apple.
def packaged_message
opts = @options.clone # Don't destroy our pristine copy
hsh = {'aps' => {}}
hsh['aps']['alert'] = opts.delete(:alert).to_s if opts[:alert]
hsh['aps']['badge'] = opts.delete(:badge).to_i if opts[:badge]
if sound = opts.delete(:sound)
hsh['aps']['sound'] = sound.is_a?(TrueClass) ? 'default' : sound.to_s
end
hsh.merge!(opts)
ActiveSupport::JSON::encode(hsh)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment