Sha256: 4c65556b2f20f71053fcb0c35dcc03d70eb3cd7a2c0e40679af5e8fdd3edc7a2

Contents?: true

Size: 1.04 KB

Versions: 4

Compression:

Stored size: 1.04 KB

Contents

module APN
  class Notification
    attr_accessor :badge, :alert, :sound, :content_available, :custom_properties, :device_token

    def initialize(hash)
      [:badge, :alert, :sound, :device_token, :content_available, :custom_properties].each do |k|
        self.instance_variable_set("@#{k}".to_sym, hash[k]) if hash[k]
      end
      raise "Must provide device token: #{hash}" if self.device_token.nil?
      self.device_token = self.device_token.delete(' ')
    end

    def payload
      p = Hash.new
      [:badge, :alert, :sound, :content_available].each do |k|
        p[k.to_s.gsub('_','-').to_sym] = send(k) if send(k)
      end
      aps = {:aps => p}
      aps.merge!(custom_properties) if custom_properties
      aps
    end

    def json_payload
      j = ActiveSupport::JSON.encode(payload)
      raise "The payload #{j} is larger than allowed: #{j.length}" if j.size > 256
      j
    end

    def to_bytes
      j = json_payload
      [0, 0, 32, self.device_token, 0, j.bytesize, j].pack("cccH*cca*").force_encoding('ASCII-8BIT')
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
apn-1.1.0 lib/apn/notification.rb
apn-1.0.2 lib/apn/notification.rb
apn-1.0.1 lib/apn/notification.rb
apn-1.0.0 lib/apn/notification.rb