Class: Gravel::APNS::Notification
- Inherits:
-
Object
- Object
- Gravel::APNS::Notification
- 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
-
#action_key ⇒ Object
A localization key to use when populating the content of the ‘View’ button.
-
#badge ⇒ Integer
The badge number to show on the application icon.
-
#body ⇒ String|Gravel::APNS::Notification::Localization
The body of the notification.
-
#category ⇒ Object
A category to identify the notification’s type.
-
#collapse_id ⇒ String
A group identifier for the notification.
-
#content_available ⇒ Boolean
Set to true to trigger a silent notification in your application.
-
#device_token ⇒ String
A token representing the device you want to send the notification to.
-
#expiration ⇒ Time
A time when the notification is no longer valid and APNS should stop attempting to deliver the notification.
-
#launch_image ⇒ Object
The filename of an image to use when launching the app.
-
#mutable_content ⇒ Hash
The mutable content of the notification.
-
#priority ⇒ Integer
The priority of the notification.
-
#sound ⇒ String
The name of the sound file to play when notifying the user.
-
#subtitle ⇒ String|Gravel::APNS::Notification::Localization
The subtitle of the notification.
-
#title ⇒ String|Gravel::APNS::Notification::Localization
The title of the notification.
-
#uuid ⇒ String
A unique identifier for this notification.
Instance Method Summary collapse
-
#for_device_tokens(*tokens) ⇒ Array
Quickly create the same notification for multiple device tokens.
-
#initialize ⇒ Gravel::APNS::Notification
constructor
Create a new APNS notification.
-
#payload ⇒ Hash
Generate the APNS payload.
Constructor Details
#initialize ⇒ Gravel::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_key ⇒ Object
A localization key to use when populating the content of the ‘View’ button.
46 47 48 |
# File 'lib/gravel/apns/notification.rb', line 46 def action_key @action_key end |
#badge ⇒ Integer
The badge number to show on the application icon.
40 41 42 |
# File 'lib/gravel/apns/notification.rb', line 40 def badge @badge end |
#body ⇒ String|Gravel::APNS::Notification::Localization
The body of the notification. You can provide a localization on this value.
28 29 30 |
# File 'lib/gravel/apns/notification.rb', line 28 def body @body end |
#category ⇒ Object
A category to identify the notification’s type. This should match one of the identifier values as defined in your application.
54 55 56 |
# File 'lib/gravel/apns/notification.rb', line 54 def category @category end |
#collapse_id ⇒ String
A group identifier for the notification. This allows APNS to identify similar messages and collapse them into a single notification.
87 88 89 |
# File 'lib/gravel/apns/notification.rb', line 87 def collapse_id @collapse_id end |
#content_available ⇒ Boolean
Set to true to trigger a silent notification in your application. This is useful to trigger a background app refresh.
67 68 69 |
# File 'lib/gravel/apns/notification.rb', line 67 def content_available @content_available end |
#device_token ⇒ String
A token representing the device you want to send the notification to.
106 107 108 |
# File 'lib/gravel/apns/notification.rb', line 106 def device_token @device_token end |
#expiration ⇒ Time
A time when the notification is no longer valid and APNS should stop attempting to deliver the notification.
100 101 102 |
# File 'lib/gravel/apns/notification.rb', line 100 def expiration @expiration end |
#launch_image ⇒ Object
The filename of an image to use when launching the app.
60 61 62 |
# File 'lib/gravel/apns/notification.rb', line 60 def launch_image @launch_image end |
#mutable_content ⇒ Hash
The mutable content of the notification.
73 74 75 |
# File 'lib/gravel/apns/notification.rb', line 73 def mutable_content @mutable_content end |
#priority ⇒ Integer
The priority of the notification.
93 94 95 |
# File 'lib/gravel/apns/notification.rb', line 93 def priority @priority end |
#sound ⇒ String
The name of the sound file to play when notifying the user.
34 35 36 |
# File 'lib/gravel/apns/notification.rb', line 34 def sound @sound end |
#subtitle ⇒ String|Gravel::APNS::Notification::Localization
The subtitle of the notification. You can provide a localization on this value.
21 22 23 |
# File 'lib/gravel/apns/notification.rb', line 21 def subtitle @subtitle end |
#title ⇒ String|Gravel::APNS::Notification::Localization
The title of the notification. You can provide a localization on this value.
14 15 16 |
# File 'lib/gravel/apns/notification.rb', line 14 def title @title end |
#uuid ⇒ String
A unique identifier for this notification.
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.
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 |
#payload ⇒ Hash
Generate 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 |