Class: Grocer::Notification

Inherits:
Object
  • Object
show all
Defined in:
lib/grocer/notification.rb

Overview

Public: An object used to send notifications to APNS.

Constant Summary collapse

MAX_PAYLOAD_SIZE =
256

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payload = {}) ⇒ Notification

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 value sent to APNS to uniquely this notification. (default: 0)


20
21
22
23
24
25
26
# File 'lib/grocer/notification.rb', line 20

def initialize(payload = {})
  @identifier = 0

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

Instance Attribute Details

#alertObject

Returns the value of attribute alert.



8
9
10
# File 'lib/grocer/notification.rb', line 8

def alert
  @alert
end

#badgeObject

Returns the value of attribute badge.



8
9
10
# File 'lib/grocer/notification.rb', line 8

def badge
  @badge
end

#customObject

Returns the value of attribute custom.



8
9
10
# File 'lib/grocer/notification.rb', line 8

def custom
  @custom
end

#device_tokenObject

Returns the value of attribute device_token.



8
9
10
# File 'lib/grocer/notification.rb', line 8

def device_token
  @device_token
end

#expiryObject

Returns the value of attribute expiry.



8
9
10
# File 'lib/grocer/notification.rb', line 8

def expiry
  @expiry
end

#identifierObject

Returns the value of attribute identifier.



8
9
10
# File 'lib/grocer/notification.rb', line 8

def identifier
  @identifier
end

#soundObject

Returns the value of attribute sound.



8
9
10
# File 'lib/grocer/notification.rb', line 8

def sound
  @sound
end

Instance Method Details

#to_bytesObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/grocer/notification.rb', line 28

def to_bytes
  validate_payload
  payload = encoded_payload

  [
    1,
    identifier,
    expiry_epoch_time,
    device_token_length,
    sanitized_device_token,
    payload.bytesize,
    payload
  ].pack('CNNnH64nA*')
end