Class: Grocer::Notification
- Inherits:
-
Object
- Object
- Grocer::Notification
- Defined in:
- lib/grocer/notification.rb
Overview
Public: An object used to send notifications to APNS.
Direct Known Subclasses
MobileDeviceManagementNotification, NewsstandNotification, PassbookNotification, SafariNotification
Constant Summary collapse
- MAX_PAYLOAD_SIZE =
2048
- CONTENT_AVAILABLE_INDICATOR =
1
- MUTABLE_CONTENT_INDICATOR =
1
Instance Attribute Summary collapse
-
#alert ⇒ Object
Returns the value of attribute alert.
-
#badge ⇒ Object
Returns the value of attribute badge.
-
#category ⇒ Object
Returns the value of attribute category.
-
#content_available ⇒ Object
Returns the value of attribute content_available.
-
#custom ⇒ Object
Returns the value of attribute custom.
-
#device_token ⇒ Object
Returns the value of attribute device_token.
-
#expiry ⇒ Object
Returns the value of attribute expiry.
-
#identifier ⇒ Object
Returns the value of attribute identifier.
-
#mutable_content ⇒ Object
Returns the value of attribute mutable_content.
-
#sound ⇒ Object
Returns the value of attribute sound.
Instance Method Summary collapse
- #content_available? ⇒ Boolean
-
#initialize(payload = {}) ⇒ Notification
constructor
Public: Initialize a new Grocer::Notification.
- #mutable_content? ⇒ Boolean
- #to_bytes ⇒ Object
- #valid? ⇒ Boolean
- #validate_payload ⇒ Object
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 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)
25 26 27 28 29 30 31 |
# File 'lib/grocer/notification.rb', line 25 def initialize(payload = {}) @identifier = 0 payload.each do |key, val| send("#{key}=", val) end end |
Instance Attribute Details
#alert ⇒ Object
Returns the value of attribute alert.
11 12 13 |
# File 'lib/grocer/notification.rb', line 11 def alert @alert end |
#badge ⇒ Object
Returns the value of attribute badge.
11 12 13 |
# File 'lib/grocer/notification.rb', line 11 def badge @badge end |
#category ⇒ Object
Returns the value of attribute category.
11 12 13 |
# File 'lib/grocer/notification.rb', line 11 def category @category end |
#content_available ⇒ Object
Returns the value of attribute content_available.
11 12 13 |
# File 'lib/grocer/notification.rb', line 11 def content_available @content_available end |
#custom ⇒ Object
Returns the value of attribute custom.
11 12 13 |
# File 'lib/grocer/notification.rb', line 11 def custom @custom end |
#device_token ⇒ Object
Returns the value of attribute device_token.
10 11 12 |
# File 'lib/grocer/notification.rb', line 10 def device_token @device_token end |
#expiry ⇒ Object
Returns the value of attribute expiry.
10 11 12 |
# File 'lib/grocer/notification.rb', line 10 def expiry @expiry end |
#identifier ⇒ Object
Returns the value of attribute identifier.
10 11 12 |
# File 'lib/grocer/notification.rb', line 10 def identifier @identifier end |
#mutable_content ⇒ Object
Returns the value of attribute mutable_content.
11 12 13 |
# File 'lib/grocer/notification.rb', line 11 def mutable_content @mutable_content end |
#sound ⇒ Object
Returns the value of attribute sound.
11 12 13 |
# File 'lib/grocer/notification.rb', line 11 def sound @sound end |
Instance Method Details
#content_available? ⇒ Boolean
77 78 79 |
# File 'lib/grocer/notification.rb', line 77 def content_available? !!content_available end |
#mutable_content? ⇒ Boolean
86 87 88 |
# File 'lib/grocer/notification.rb', line 86 def mutable_content? !!mutable_content end |
#to_bytes ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# 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? ⇒ Boolean
96 97 98 |
# File 'lib/grocer/notification.rb', line 96 def valid? validate_payload rescue false end |
#validate_payload ⇒ Object
90 91 92 93 94 |
# File 'lib/grocer/notification.rb', line 90 def validate_payload fail NoPayloadError unless alert || badge || custom fail PayloadTooLargeError if payload_too_large? true end |