Class: Gravel::APNS::Notification

Inherits:
Object
  • Object
show all
Defined in:
lib/gravel/apns/notification.rb,
lib/gravel/apns/notification/localization.rb

Overview

A notification for an Apple device (using APNS).

Defined Under Namespace

Classes: Localization

Constant Summary collapse

PRIORITY_IMMEDIATE =
10
PRIORITY_ECO =
5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGravel::APNS::Notification

Create a new APNS notification.



112
113
114
115
# File 'lib/gravel/apns/notification.rb', line 112

def initialize
  self.content_available = false
  self.uuid = SecureRandom.uuid
end

Instance Attribute Details

#action_keyObject

A localization key to use when populating the content of the ‘View’ button.

Parameters:

  • The (String)

    action button’s localization key.



46
47
48
# File 'lib/gravel/apns/notification.rb', line 46

def action_key
  @action_key
end

#badgeInteger

The badge number to show on the application icon.

Returns:

  • (Integer)

    The badge number.



40
41
42
# File 'lib/gravel/apns/notification.rb', line 40

def badge
  @badge
end

#bodyString|Gravel::APNS::Notification::Localization

The body of the notification. You can provide a localization on this value.

Returns:



28
29
30
# File 'lib/gravel/apns/notification.rb', line 28

def body
  @body
end

#categoryObject

A category to identify the notification’s type. This should match one of the identifier values as defined in your application.

Parameters:

  • The (String)

    notification’s category.



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

def category
  @category
end

#collapse_idString

A group identifier for the notification. This allows APNS to identify similar messages and collapse them into a single notification.

Returns:

  • (String)

    The collapse identifier.



87
88
89
# File 'lib/gravel/apns/notification.rb', line 87

def collapse_id
  @collapse_id
end

#content_availableBoolean

Set to true to trigger a silent notification in your application. This is useful to trigger a background app refresh.

Returns:

  • (Boolean)

    Whether or not new content is available.



67
68
69
# File 'lib/gravel/apns/notification.rb', line 67

def content_available
  @content_available
end

#device_tokenString

A token representing the device you want to send the notification to.

Returns:

  • (String)

    The device token.



106
107
108
# File 'lib/gravel/apns/notification.rb', line 106

def device_token
  @device_token
end

#expirationTime

A time when the notification is no longer valid and APNS should stop attempting to deliver the notification.

Returns:

  • (Time)

    The expiration time.



100
101
102
# File 'lib/gravel/apns/notification.rb', line 100

def expiration
  @expiration
end

#launch_imageObject

The filename of an image to use when launching the app.

Parameters:

  • The (String)

    launch image filename.



60
61
62
# File 'lib/gravel/apns/notification.rb', line 60

def launch_image
  @launch_image
end

#mutable_contentHash

The mutable content of the notification.

Returns:

  • (Hash)

    The mutable content.



73
74
75
# File 'lib/gravel/apns/notification.rb', line 73

def mutable_content
  @mutable_content
end

#priorityInteger

The priority of the notification.

Returns:

  • (Integer)

    The priority.



93
94
95
# File 'lib/gravel/apns/notification.rb', line 93

def priority
  @priority
end

#soundString

The name of the sound file to play when notifying the user.

Returns:

  • (String)

    The sound file name.



34
35
36
# File 'lib/gravel/apns/notification.rb', line 34

def sound
  @sound
end

#subtitleString|Gravel::APNS::Notification::Localization

The subtitle of the notification. You can provide a localization on this value.

Returns:



21
22
23
# File 'lib/gravel/apns/notification.rb', line 21

def subtitle
  @subtitle
end

#titleString|Gravel::APNS::Notification::Localization

The title of the notification. You can provide a localization on this value.

Returns:



14
15
16
# File 'lib/gravel/apns/notification.rb', line 14

def title
  @title
end

#uuidString

A unique identifier for this notification.

Returns:

  • (String)

    The unique identifier.



79
80
81
# File 'lib/gravel/apns/notification.rb', line 79

def uuid
  @uuid
end

Instance Method Details

#for_device_tokens(*tokens) ⇒ Array

Quickly create the same notification for multiple device tokens.

Parameters:

  • tokens (Splat)

    An array of device tokens.

Returns:

  • (Array)

    An array of notifications.



122
123
124
125
126
127
128
129
# File 'lib/gravel/apns/notification.rb', line 122

def for_device_tokens(*tokens)
  tokens.map do |token|
    notification = self.dup
    notification.uuid = SecureRandom.uuid
    notification.device_token = token
    notification
  end
end

#payloadHash

Generate the APNS payload.

Returns:

  • (Hash)

    The APNS payload.



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/gravel/apns/notification.rb', line 135

def payload
  aps = Hash.new

  if self.title.is_a?(String)
    aps['alert'] ||= Hash.new
    aps['alert']['title'] = self.title
  elsif self.title.is_a?(Gravel::APNS::Notification::Localization)
    aps['alert'] ||= Hash.new
    aps['alert'].merge!(self.title.payload(:title))
  end

  if self.subtitle.is_a?(String)
    aps['alert'] ||= Hash.new
    aps['alert']['subtitle'] = self.subtitle
  elsif self.subtitle.is_a?(Gravel::APNS::Notification::Localization)
    aps['alert'] ||= Hash.new
    aps['alert'].merge!(self.subtitle.payload(:subtitle))
  end

  if self.body.is_a?(String)
    aps['alert'] ||= Hash.new
    aps['alert']['body'] = self.body
  elsif self.body.is_a?(Gravel::APNS::Notification::Localization)
    aps['alert'] ||= Hash.new
    aps['alert'].merge!(self.body.payload(:body))
  end

  if self.sound == :default
    aps['sound'] = 'default'
  elsif self.sound.is_a?(String)
    aps['sound'] = self.sound.to_s
  end

  if self.badge.is_a?(Integer)
    aps['badge'] = self.badge
  end

  if self.action_key.is_a?(String)
    aps['alert']['action-loc-key'] = self.action_key
  end

  if self.category.is_a?(String)
    aps['category'] = self.category
  end

  if self.launch_image.is_a?(String)
    aps['alert']['launch-image'] = self.launch_image
  end

  if self.content_available
    aps['content-available'] = '1'
  end

  payload = Hash.new

  if self.mutable_content.is_a?(Hash)
    aps['mutable-content'] = '1'
    payload.merge!(self.mutable_content)
  end

  unless aps.empty?
    payload['aps'] = aps
  end

  payload
end