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.
- 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(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

#alertObject

Returns the value of attribute alert.



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

def alert
  @alert
end

#apns_idObject (readonly)

Returns the value of attribute apns_id.



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

def apns_id
  @apns_id
end

#badgeObject

Returns the value of attribute badge.



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

def badge
  @badge
end

#categoryObject

Returns the value of attribute category.



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

def category
  @category
end

#collapse_idObject

Returns the value of attribute collapse_id.



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

def collapse_id
  @collapse_id
end

#content_availableObject

Returns the value of attribute content_available.



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

def content_available
  @content_available
end

#custom_payloadObject

Returns the value of attribute custom_payload.



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

def custom_payload
  @custom_payload
end

#expirationObject

Returns the value of attribute expiration.



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

def expiration
  @expiration
end

#mutable_contentObject

Returns the value of attribute mutable_content.



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

def mutable_content
  @mutable_content
end

#priorityObject (readonly)

Returns the value of attribute priority.



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

def priority
  @priority
end

#soundObject

Returns the value of attribute sound.



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

def sound
  @sound
end

#tokenObject (readonly)

Returns the value of attribute token.



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

def token
  @token
end

#topicObject

Returns the value of attribute topic.



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

def topic
  @topic
end

#url_argsObject

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

#bodyObject



69
70
71
# File 'lib/lorkhan/notification.rb', line 69

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

#push_typeObject



73
74
75
76
77
# File 'lib/lorkhan/notification.rb', line 73

def push_type
  return 'background' if content_available == true

  'alert'
end

#to_hObject



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