Sha256: 8a543785711ce71161f84549d0d1f7eebb6dd6d224751f21926c4d01d9cf44ef

Contents?: true

Size: 1.17 KB

Versions: 9

Compression:

Stored size: 1.17 KB

Contents

module LeadZeppelin
  module APNS
    class Notification
      attr_reader :device_token, :identifier, :expiry

      def initialize(device_token, message, opts={})
        @device_token = device_token
        @opts = opts
        
        @identifier = @opts[:identifier] || SecureRandom.random_bytes(4)
        @identifier = @identifier.to_s

        @expiry = @opts[:expiry].nil? ? 1 : @opts[:expiry].to_i

        if opts[:raw] == true
          @message = message
        elsif message.is_a?(Hash)
          other = message.delete(:other)
          @message = {aps: message}
          @message.merge!(other) if other
        elsif message.is_a?(String)
          @message = {aps: {alert: message}}
        else
          raise ArgumentError, "notification message must be hash or string"
        end
      end

      def payload
        [1, @identifier, @expiry, 0, 32, packaged_token, 0, message_json.bytesize, message_json].pack("cA4Ncca*cca*")
      end

      def packaged_token
        [device_token.gsub(/[\s|<|>]/,'')].pack('H*')
      end

      def message_json
        return @message if @opts[:raw] == true
        @message_json ||= MultiJson.encode @message
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
lead_zeppelin-0.1.10 lib/lead_zeppelin/apns/notification.rb
lead_zeppelin-0.1.9 lib/lead_zeppelin/apns/notification.rb
lead_zeppelin-0.1.8 lib/lead_zeppelin/apns/notification.rb
lead_zeppelin-0.1.7 lib/lead_zeppelin/apns/notification.rb
lead_zeppelin-0.1.6 lib/lead_zeppelin/apns/notification.rb
lead_zeppelin-0.1.5 lib/lead_zeppelin/apns/notification.rb
lead_zeppelin-0.1.4 lib/lead_zeppelin/apns/notification.rb
lead_zeppelin-0.1.3 lib/lead_zeppelin/apns/notification.rb
lead_zeppelin-0.1.2 lib/lead_zeppelin/apns/notification.rb