Class: ApnsProviderApi::Notification
- Inherits:
-
Object
- Object
- ApnsProviderApi::Notification
- Defined in:
- lib/apns_provider_api/notification.rb
Defined Under Namespace
Classes: APNSError
Constant Summary collapse
- MAXIMUM_PAYLOAD_SIZE =
MAXIMUM_PAYLOAD_SIZE = 4096 already accepted by provider API but not for iOS apps
2048
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_data ⇒ Object
Returns the value of attribute custom_data.
-
#error_message ⇒ Object
Returns the value of attribute error_message.
-
#expiry ⇒ Object
Returns the value of attribute expiry.
-
#id ⇒ Object
Returns the value of attribute id.
-
#priority ⇒ Object
Returns the value of attribute priority.
-
#sent_at ⇒ Object
readonly
Returns the value of attribute sent_at.
-
#sound ⇒ Object
Returns the value of attribute sound.
-
#token ⇒ Object
(also: #device)
Returns the value of attribute token.
-
#uuid ⇒ Object
Returns the value of attribute uuid.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Notification
constructor
A new instance of Notification.
- #mark_as_sent! ⇒ Object
- #mark_as_unsent! ⇒ Object
- #payload ⇒ Object
- #sent? ⇒ Boolean
- #trim_alert(alert, available_bytes, dottize) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Notification
Returns a new instance of Notification.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/apns_provider_api/notification.rb', line 39 def initialize( = {}) @token = .delete(:token) || .delete(:device) raise 'invalid token' if @token && @token.empty? @badge = .delete(:badge) @sound = .delete(:sound) @category = .delete(:category) @expiry = .delete(:expiry) @id = .delete(:id) @priority = .delete(:priority) @content_available = .delete(:content_available) dottize = .delete(:dottize) @custom_data = alert = .delete(:alert) available_bytes = MAXIMUM_PAYLOAD_SIZE - payload.to_s.bytesize @alert = trim_alert(alert, available_bytes, dottize) generate_uuid(.delete(:uuid)) end |
Instance Attribute Details
#alert ⇒ Object
Returns the value of attribute alert.
32 33 34 |
# File 'lib/apns_provider_api/notification.rb', line 32 def alert @alert end |
#badge ⇒ Object
Returns the value of attribute badge.
32 33 34 |
# File 'lib/apns_provider_api/notification.rb', line 32 def badge @badge end |
#category ⇒ Object
Returns the value of attribute category.
32 33 34 |
# File 'lib/apns_provider_api/notification.rb', line 32 def category @category end |
#content_available ⇒ Object
Returns the value of attribute content_available.
32 33 34 |
# File 'lib/apns_provider_api/notification.rb', line 32 def content_available @content_available end |
#custom_data ⇒ Object
Returns the value of attribute custom_data.
32 33 34 |
# File 'lib/apns_provider_api/notification.rb', line 32 def custom_data @custom_data end |
#error_message ⇒ Object
Returns the value of attribute error_message.
32 33 34 |
# File 'lib/apns_provider_api/notification.rb', line 32 def @error_message end |
#expiry ⇒ Object
Returns the value of attribute expiry.
32 33 34 |
# File 'lib/apns_provider_api/notification.rb', line 32 def expiry @expiry end |
#id ⇒ Object
Returns the value of attribute id.
32 33 34 |
# File 'lib/apns_provider_api/notification.rb', line 32 def id @id end |
#priority ⇒ Object
Returns the value of attribute priority.
32 33 34 |
# File 'lib/apns_provider_api/notification.rb', line 32 def priority @priority end |
#sent_at ⇒ Object (readonly)
Returns the value of attribute sent_at.
33 34 35 |
# File 'lib/apns_provider_api/notification.rb', line 33 def sent_at @sent_at end |
#sound ⇒ Object
Returns the value of attribute sound.
32 33 34 |
# File 'lib/apns_provider_api/notification.rb', line 32 def sound @sound end |
#token ⇒ Object Also known as: device
Returns the value of attribute token.
32 33 34 |
# File 'lib/apns_provider_api/notification.rb', line 32 def token @token end |
#uuid ⇒ Object
Returns the value of attribute uuid.
32 33 34 |
# File 'lib/apns_provider_api/notification.rb', line 32 def uuid @uuid end |
Instance Method Details
#mark_as_sent! ⇒ Object
70 71 72 |
# File 'lib/apns_provider_api/notification.rb', line 70 def mark_as_sent! @sent_at = Time.now end |
#mark_as_unsent! ⇒ Object
74 75 76 |
# File 'lib/apns_provider_api/notification.rb', line 74 def mark_as_unsent! @sent_at = nil end |
#payload ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/apns_provider_api/notification.rb', line 57 def payload json = {}.merge(@custom_data || {}).inject({}){|h,(k,v)| h[k.to_s] = v; h} 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']['category'] = @category if @category json['aps']['content-available'] = 1 if @content_available json end |
#sent? ⇒ Boolean
78 79 80 |
# File 'lib/apns_provider_api/notification.rb', line 78 def sent? !!@sent_at end |
#trim_alert(alert, available_bytes, dottize) ⇒ Object
82 83 84 85 86 87 88 89 |
# File 'lib/apns_provider_api/notification.rb', line 82 def trim_alert(alert, available_bytes, dottize) if alert.size > available_bytes raise "the notification payload was too large \nTip: use dottize option to auto trim the notification alert" unless dottize chars_to_delete = alert.size - available_bytes alert = dottize_no_broken_words(alert, alert.size - chars_to_delete) end alert end |