Class: Lorkhan::Notification
- Inherits:
-
Object
- Object
- Lorkhan::Notification
- Defined in:
- lib/lorkhan/notification.rb
Overview
Describes a push notification
Required attributes
- token: The device token to deliver the notification to
- apns_id: The APNS id for the notification.
This *must* be a UUID and is created when the instance is initialized.
- priority: The priority of the notification. Apple will use this to determine how to deliver the notification.
See `PRIORITY_DELIVER_IMMEDIATELY` & `PRIORITY_DELIVER_BACKGROUND` for more info.
- expiration: A UNIX UTC epoch expressed in seconds for how long Apple will retain the notification,
and attempt redelivery. A 0 time will attempt a single delivery then disguard the message.
- topic: The topic of the remote notification, which is typically the bundle ID for your app.
The certificate you create in your developer account must include the capability for this topic.
Optional attributes
- alert: The message displayed to the user.
- badge: The badge number displayed on the app icon.
- category: The category for the notification. Used to provide direct notification actions.
- collapse_id: Used to group multiple notifications on the screen.
- content_available: A boolean if this should be a content available push.
This will deliver a silent notification to the device.
Your app's entitlements must be enabled.
If the value for this is truthy, the alert, badge, sound will be ignored.
- custom_payload: A custom hash of data to send. Must be JSON encodable.
- mutable_content: A `1` if the notificaiton is mutable.
See `UNNotificationServiceExtension` (https://developer.apple.com/reference/usernotifications/unnotificationserviceextension)
- sound: The name of a bundled sound file to play.
- url_args: Used in conjunction with the `urlFormatString` in
the `website.json` file for sending web notifications.
Constant Summary collapse
- PRIORITY_DELIVER_IMMEDIATELY =
Send the push message immediately. Notifications with this priority must trigger an alert, sound, or badge on the target device.
10
- PRIORITY_DELIVER_BACKGROUND =
Send the push message at a time that takes into account power considerations for the device. Notifications with this priority might be grouped and delivered in bursts.
5
- DEFAULT_SOUND_NAME =
'default'.freeze
Instance Attribute Summary collapse
-
#alert ⇒ Object
Returns the value of attribute alert.
-
#apns_id ⇒ Object
readonly
Returns the value of attribute apns_id.
-
#badge ⇒ Object
Returns the value of attribute badge.
-
#category ⇒ Object
Returns the value of attribute category.
-
#collapse_id ⇒ Object
Returns the value of attribute collapse_id.
-
#content_available ⇒ Object
Returns the value of attribute content_available.
-
#custom_payload ⇒ Object
Returns the value of attribute custom_payload.
-
#expiration ⇒ Object
Returns the value of attribute expiration.
-
#mutable_content ⇒ Object
Returns the value of attribute mutable_content.
-
#priority ⇒ Object
readonly
Returns the value of attribute priority.
-
#sound ⇒ Object
Returns the value of attribute sound.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
-
#topic ⇒ Object
Returns the value of attribute topic.
-
#url_args ⇒ Object
Returns the value of attribute url_args.
Instance Method Summary collapse
- #body ⇒ Object
-
#initialize(token) ⇒ Notification
constructor
Create a new notification.
- #push_type ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(token) ⇒ Notification
Create a new notification
token: The Device token that the notification will be delivered to
61 62 63 64 65 66 67 |
# File 'lib/lorkhan/notification.rb', line 61 def initialize(token) @token = token @apns_id = SecureRandom.uuid @expiration = 0 @content_available = false @priority = PRIORITY_DELIVER_IMMEDIATELY end |
Instance Attribute Details
#alert ⇒ Object
Returns the value of attribute alert.
52 53 54 |
# File 'lib/lorkhan/notification.rb', line 52 def alert @alert end |
#apns_id ⇒ Object (readonly)
Returns the value of attribute apns_id.
52 53 54 |
# File 'lib/lorkhan/notification.rb', line 52 def apns_id @apns_id end |
#badge ⇒ Object
Returns the value of attribute badge.
53 54 55 |
# File 'lib/lorkhan/notification.rb', line 53 def badge @badge end |
#category ⇒ Object
Returns the value of attribute category.
53 54 55 |
# File 'lib/lorkhan/notification.rb', line 53 def category @category end |
#collapse_id ⇒ Object
Returns the value of attribute collapse_id.
54 55 56 |
# File 'lib/lorkhan/notification.rb', line 54 def collapse_id @collapse_id end |
#content_available ⇒ Object
Returns the value of attribute content_available.
52 53 54 |
# File 'lib/lorkhan/notification.rb', line 52 def content_available @content_available end |
#custom_payload ⇒ Object
Returns the value of attribute custom_payload.
53 54 55 |
# File 'lib/lorkhan/notification.rb', line 53 def custom_payload @custom_payload end |
#expiration ⇒ Object
Returns the value of attribute expiration.
54 55 56 |
# File 'lib/lorkhan/notification.rb', line 54 def expiration @expiration end |
#mutable_content ⇒ Object
Returns the value of attribute mutable_content.
53 54 55 |
# File 'lib/lorkhan/notification.rb', line 53 def mutable_content @mutable_content end |
#priority ⇒ Object (readonly)
Returns the value of attribute priority.
52 53 54 |
# File 'lib/lorkhan/notification.rb', line 52 def priority @priority end |
#sound ⇒ Object
Returns the value of attribute sound.
53 54 55 |
# File 'lib/lorkhan/notification.rb', line 53 def sound @sound end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
52 53 54 |
# File 'lib/lorkhan/notification.rb', line 52 def token @token end |
#topic ⇒ Object
Returns the value of attribute topic.
54 55 56 |
# File 'lib/lorkhan/notification.rb', line 54 def topic @topic end |
#url_args ⇒ Object
Returns the value of attribute url_args.
53 54 55 |
# File 'lib/lorkhan/notification.rb', line 53 def url_args @url_args end |
Instance Method Details
#body ⇒ Object
69 70 71 |
# File 'lib/lorkhan/notification.rb', line 69 def body JSON.dump(to_h).force_encoding(Encoding::BINARY) end |
#push_type ⇒ Object
73 74 75 76 77 |
# File 'lib/lorkhan/notification.rb', line 73 def push_type return 'background' if content_available == true 'alert' end |
#to_h ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/lorkhan/notification.rb', line 95 def to_h {}.tap do |root| root[:aps] = {}.tap do |aps| if content_available aps['content-available'] = 1 else aps[:alert] = alert if alert aps[:badge] = badge if badge aps[:sound] = sound if sound end aps[:category] = category if category aps['url-args'] = url_args if url_args aps['mutable-content'] = 1 if mutable_content end root.merge!(custom_payload) if custom_payload end end |