Sha256: e4716d9d22189310d22786f925590a4a049de03c1481927a08a9bd6521a0c8dd

Contents?: true

Size: 1.03 KB

Versions: 4

Compression:

Stored size: 1.03 KB

Contents

require 'json'

module ApnsSimple
  class Notification

    attr_reader :token, :alert, :badge, :sound, :content_available, :custom_payload
    attr_accessor :error, :error_message, :error_code

    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

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
apns_simple-1.1.1 lib/apns_simple/notification.rb
apns_simple-1.1.0 lib/apns_simple/notification.rb
apns_simple-1.0.1 lib/apns_simple/notification.rb
apns_simple-1.0.0 lib/apns_simple/notification.rb