Sha256: 0bb58bfdd91fbf3bf38795a1f17e4fb8382e2a094ff51d0ea5fa2b4cc472beec
Contents?: true
Size: 1.43 KB
Versions: 1
Compression:
Stored size: 1.43 KB
Contents
require 'json' module Houston class Notification MAXIMUM_PAYLOAD_SIZE = 256 attr_accessor :token, :alert, :badge, :sound, :content_available, :custom_data, :id, :expiry attr_reader :sent_at alias :device :token alias :device= :token= def initialize(options = {}) @token = options.delete(:token) || options.delete(:device) @alert = options.delete(:alert) @badge = options.delete(:badge) @sound = options.delete(:sound) @expiry = options.delete(:expiry) @id = options.delete(:id) @content_available = options.delete(:content_available) @custom_data = options end def payload json = {}.merge(@custom_data || {}) json['aps'] ||= {} json['aps']['alert'] = @alert if @alert json['aps']['badge'] = @badge.to_i rescue 0 if @badge json['aps']['sound'] = @sound if @sound json['aps']['content-available'] = 1 if @content_available json end def message json = payload.to_json device_token = [@token.gsub(/[<\s>]/, '')].pack('H*') @expiry ||= Time.now + 86400 @id ||= 0 [1, @id, @expiry.to_i, 0, 32, device_token, 0, json.bytes.count, json].pack('ciicca*cca*') end def mark_as_sent! @sent_at = Time.now end def mark_as_unsent! @sent_at = nil end def sent? !!@sent_at end def valid? payload.to_json.bytesize <= MAXIMUM_PAYLOAD_SIZE end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
houston-1.0.0 | ./lib/houston/notification.rb |