Class: Lorkhan::Notification

Inherits:
Object
  • Object
show all
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.
- title: Title of the notification 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

Instance Method Summary collapse

Constructor Details

#initialize(id, topic, token) ⇒ Notification

Create a new notification

token: The Device token that the notification will be delivered to



62
63
64
65
66
67
68
69
70
# File 'lib/lorkhan/notification.rb', line 62

def initialize(id, topic, token)
  @id = id
  @token = token
  @topic = topic
  @apns_id = id || SecureRandom.uuid
  @expiration = 0
  @content_available = false
  @priority = PRIORITY_DELIVER_IMMEDIATELY
end

Instance Attribute Details

#alertObject

Returns the value of attribute alert.



53
54
55
# File 'lib/lorkhan/notification.rb', line 53

def alert
  @alert
end

#apns_idObject (readonly)

Returns the value of attribute apns_id.



53
54
55
# File 'lib/lorkhan/notification.rb', line 53

def apns_id
  @apns_id
end

#badgeObject

Returns the value of attribute badge.



54
55
56
# File 'lib/lorkhan/notification.rb', line 54

def badge
  @badge
end

#categoryObject

Returns the value of attribute category.



54
55
56
# File 'lib/lorkhan/notification.rb', line 54

def category
  @category
end

#collapse_idObject

Returns the value of attribute collapse_id.



55
56
57
# File 'lib/lorkhan/notification.rb', line 55

def collapse_id
  @collapse_id
end

#content_availableObject

Returns the value of attribute content_available.



53
54
55
# File 'lib/lorkhan/notification.rb', line 53

def content_available
  @content_available
end

#custom_payloadObject

Returns the value of attribute custom_payload.



54
55
56
# File 'lib/lorkhan/notification.rb', line 54

def custom_payload
  @custom_payload
end

#expirationObject

Returns the value of attribute expiration.



55
56
57
# File 'lib/lorkhan/notification.rb', line 55

def expiration
  @expiration
end

#mutable_contentObject

Returns the value of attribute mutable_content.



54
55
56
# File 'lib/lorkhan/notification.rb', line 54

def mutable_content
  @mutable_content
end

#priorityObject (readonly)

Returns the value of attribute priority.



53
54
55
# File 'lib/lorkhan/notification.rb', line 53

def priority
  @priority
end

#soundObject

Returns the value of attribute sound.



54
55
56
# File 'lib/lorkhan/notification.rb', line 54

def sound
  @sound
end

#titleObject

Returns the value of attribute title.



54
55
56
# File 'lib/lorkhan/notification.rb', line 54

def title
  @title
end

#tokenObject (readonly)

Returns the value of attribute token.



53
54
55
# File 'lib/lorkhan/notification.rb', line 53

def token
  @token
end

#topicObject (readonly)

Returns the value of attribute topic.



53
54
55
# File 'lib/lorkhan/notification.rb', line 53

def topic
  @topic
end

#url_argsObject

Returns the value of attribute url_args.



54
55
56
# File 'lib/lorkhan/notification.rb', line 54

def url_args
  @url_args
end

Instance Method Details

#bodyObject



72
73
74
# File 'lib/lorkhan/notification.rb', line 72

def body
  JSON.dump(to_h).force_encoding(Encoding::BINARY)
end

#push_typeObject



76
77
78
79
80
# File 'lib/lorkhan/notification.rb', line 76

def push_type
  return 'background' if content_available == true && alert.nil?

  'alert'
end

#to_hObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/lorkhan/notification.rb', line 92

def to_h
  {}.tap do |root|
    root[:aps] = {}.tap do |aps|
      aps[:alert] = alert if alert
      aps[:title] = title if title
      aps[:badge] = badge if badge
      aps[:sound] = sound if sound
      aps[:category] = category if category
      aps['url-args'] = url_args if url_args
      aps['mutable-content'] = 1 if mutable_content
      aps['content-available'] = 1 if content_available
    end
    root.merge!(custom_payload) if custom_payload
  end
end