class Grocer::Notification

Public: An object used to send notifications to APNS.

Constants

CONTENT_AVAILABLE_INDICATOR
MAX_PAYLOAD_SIZE
MUTABLE_CONTENT_INDICATOR

Attributes

alert[R]
badge[R]
category[R]
content_available[R]
custom[R]
device_token[RW]
expiry[RW]
identifier[RW]
mutable_content[R]
sound[R]

Public Class Methods

new(payload = {}) click to toggle source

Public: Initialize a new Grocer::Notification. You must specify at least an `alert` or `badge`.

payload - The Hash of notification parameters and payload to be sent to APNS.:

:device_token      - The String representing to device token sent to APNS.
:alert             - The String or Hash to be sent as the alert portion of the payload. (optional)
:badge             - The Integer to be sent as the badge portion of the payload. (optional)
:sound             - The String representing the sound portion of the payload. (optional)
:expiry            - The Integer representing UNIX epoch date sent to APNS as the notification expiry. (default: 0)
:identifier        - The arbitrary Integer sent to APNS to uniquely this notification. (default: 0)
:content_available - The truthy or falsy value indicating the availability of new content for background fetch. (optional)
:mutable_content   - The truthy or falsy value indicating whether to have this notification be processed by a Notification Service Extension (since iOS 10) (optional)
:category          - The String to be sent as the category portion of the payload. (optional)
# File lib/grocer/notification.rb, line 25
def initialize(payload = {})
  @identifier = 0

  payload.each do |key, val|
    send("#{key}=", val)
  end
end

Public Instance Methods

alert=(alert) click to toggle source
# File lib/grocer/notification.rb, line 47
def alert=(alert)
  @alert = alert
  @encoded_payload = nil
end
badge=(badge) click to toggle source
# File lib/grocer/notification.rb, line 52
def badge=(badge)
  @badge = badge
  @encoded_payload = nil
end
category=(category) click to toggle source
# File lib/grocer/notification.rb, line 67
def category=(category)
  @category = category
  @encoded_payload = nil
end
content_available=(content_available) click to toggle source
# File lib/grocer/notification.rb, line 72
def content_available=(content_available)
  @content_available = CONTENT_AVAILABLE_INDICATOR if content_available
  @encoded_payload = nil
end
content_available?() click to toggle source
# File lib/grocer/notification.rb, line 77
def content_available?
  !!content_available
end
custom=(custom) click to toggle source
# File lib/grocer/notification.rb, line 57
def custom=(custom)
  @custom = custom
  @encoded_payload = nil
end
mutable_content=(mutable_content) click to toggle source
# File lib/grocer/notification.rb, line 81
def mutable_content=(mutable_content)
  @mutable_content = MUTABLE_CONTENT_INDICATOR if mutable_content
  @encoded_payload = nil
end
mutable_content?() click to toggle source
# File lib/grocer/notification.rb, line 86
def mutable_content?
  !!mutable_content
end
sound=(sound) click to toggle source
# File lib/grocer/notification.rb, line 62
def sound=(sound)
  @sound = sound
  @encoded_payload = nil
end
to_bytes() click to toggle source
# File lib/grocer/notification.rb, line 33
def to_bytes
  validate_payload

  [
    1,
    identifier,
    expiry_epoch_time,
    device_token_length,
    sanitized_device_token,
    encoded_payload.bytesize,
    encoded_payload
  ].pack('CNNnH64nA*')
end
valid?() click to toggle source
# File lib/grocer/notification.rb, line 96
def valid?
  validate_payload rescue false
end
validate_payload() click to toggle source
# File lib/grocer/notification.rb, line 90
def validate_payload
  fail NoPayloadError unless alert || badge || custom
  fail PayloadTooLargeError if payload_too_large?
  true
end

Private Instance Methods

device_token_length() click to toggle source
# File lib/grocer/notification.rb, line 130
def device_token_length
  32
end
encoded_payload() click to toggle source
# File lib/grocer/notification.rb, line 102
def encoded_payload
  @encoded_payload ||= JSON.dump(payload_hash)
end
expiry_epoch_time() click to toggle source
# File lib/grocer/notification.rb, line 122
def expiry_epoch_time
  expiry.to_i
end
payload_hash() click to toggle source
# File lib/grocer/notification.rb, line 106
def payload_hash
  aps_hash = { }
  aps_hash[:alert] = alert if alert
  aps_hash[:badge] = badge if badge
  aps_hash[:sound] = sound if sound
  aps_hash[:'content-available'] = content_available if content_available?
  aps_hash[:'mutable-content'] = mutable_content if mutable_content?
  aps_hash[:category] = category if category

  { aps: aps_hash }.merge(custom || { })
end
payload_too_large?() click to toggle source
# File lib/grocer/notification.rb, line 118
def payload_too_large?
  encoded_payload.bytesize > MAX_PAYLOAD_SIZE
end
sanitized_device_token() click to toggle source
# File lib/grocer/notification.rb, line 126
def sanitized_device_token
  device_token.tr(' ', '') if device_token
end