Class: APN::Notification
- Inherits:
-
Object
- Object
- APN::Notification
- Defined in:
- lib/apn/notification.rb
Overview
Encapsulates the logic necessary to convert an iPhone token and an array of options into a string of the format required by Apple’s servers to send the notification. Much of the processing code here copied with many thanks from github.com/samsoffes/apple_push_notification/blob/master/lib/apple_push_notification.rb
APN::Notification.new’s first argument is the token of the iPhone which should receive the notification. The second argument is a hash with any of :alert, :badge, and :sound keys. All three accept string arguments, while :sound can also be set to true
to play the default sound installed with the application. At least one of these keys must exist. Any other keys are merged into the root of the hash payload ultimately sent to the iPhone:
APN::Notification.new(token, {:alert => 'Stuff', :custom => {:code => 23}})
# Writes this JSON to servers: {"aps" => {"alert" => "Stuff"}, "custom" => {"code" => 23}}
As a shortcut, APN::Notification.new also accepts a string as the second argument, which it converts into the alert to send. The following two lines are equivalent:
APN::Notification.new(token, 'Some Alert')
APN::Notification.new(token, {:alert => 'Some Alert'})
Constant Summary collapse
- MAX_ALERT_LENGTH =
Available to help clients determine before they create the notification if their message will be too large. Each iPhone Notification payload must be 256 or fewer characters. Encoding a null message has a 57 character overhead, so there are 199 characters available for the alert string.
199
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
-
#token ⇒ Object
Returns the value of attribute token.
Instance Method Summary collapse
-
#initialize(token, opts) ⇒ Notification
constructor
A new instance of Notification.
- #to_s ⇒ Object
-
#valid? ⇒ Boolean
Ensures at least one of
%w(alert badge sound)
is present.
Constructor Details
#initialize(token, opts) ⇒ Notification
Returns a new instance of Notification.
27 28 29 30 31 32 |
# File 'lib/apn/notification.rb', line 27 def initialize(token, opts) @options = hash_as_symbols(opts.is_a?(Hash) ? opts : {:alert => opts}) @token = token raise "The maximum size allowed for a notification payload is 256 bytes." if packaged_notification.size.to_i > 256 end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
26 27 28 |
# File 'lib/apn/notification.rb', line 26 def @options end |
#token ⇒ Object
Returns the value of attribute token.
26 27 28 |
# File 'lib/apn/notification.rb', line 26 def token @token end |
Instance Method Details
#to_s ⇒ Object
34 35 36 |
# File 'lib/apn/notification.rb', line 34 def to_s packaged_notification end |
#valid? ⇒ Boolean
Ensures at least one of %w(alert badge sound)
is present
39 40 41 42 |
# File 'lib/apn/notification.rb', line 39 def valid? return true if %w(alert badge sound).any?{|key| .keys.include?(key.to_sym) } false end |