lib/apns_simple/notification.rb in apns_simple-0.2.0 vs lib/apns_simple/notification.rb in apns_simple-0.3.1
- old
+ new
@@ -1,26 +1,28 @@
require 'json'
module ApnsSimple
class Notification
- attr_reader :token, :alert, :badge, :sound, :content_available
+ attr_reader :token, :alert, :badge, :sound, :content_available, :custom_payload
attr_accessor :error
- def initialize(options)
+ def initialize(options, custom_payload = {})
@token = options.fetch(:token)
@alert = options[:alert]
@badge = options[:badge]
@sound = options[:sound] || 'default'
@content_available = options[:content_available]
+ @custom_payload = custom_payload
end
def payload
payload = { aps: {} }
payload[:aps][:alert] = alert if alert
payload[:aps][:badge] = badge if badge
payload[:aps][:sound] = sound if sound
payload[:aps]['content-available'] = 1 if content_available
+ payload.merge! custom_payload
packed_message = payload.to_json.gsub(/\\u([\da-fA-F]{4})/) {|m| [$1].pack("H*").unpack("n*").pack("U*")}
packed_token = [token.gsub(/[\s|<|>]/,'')].pack('H*')
[0, 0, 32, packed_token, 0, packed_message.bytesize, packed_message].pack("ccca*cca*")
end
\ No newline at end of file