class Gravel::APNS::Notification

A notification for an Apple device (using APNS).

Constants

PRIORITY_ECO
PRIORITY_IMMEDIATE

Attributes

action_key[RW]

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

@param [String] The action button's localization key.

badge[RW]

The badge number to show on the application icon.

@return [Integer] The badge number.

body[RW]

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

@return [String|Gravel::APNS::Notification::Localization] The body.

category[RW]

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

@param [String] The notification's category.

collapse_id[RW]

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

@return [String] The collapse identifier.

content_available[RW]

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

@return [Boolean] Whether or not new content is available.

device_token[RW]

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

@return [String] The device token.

expiration[RW]

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

@return [Time] The expiration time.

launch_image[RW]

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

@param [String] The launch image filename.

mutable_content[RW]

The mutable content of the notification.

@return [Hash] The mutable content.

priority[RW]

The priority of the notification.

@return [Integer] The priority.

sound[RW]

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

@return [String] The sound file name.

subtitle[RW]

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

@return [String|Gravel::APNS::Notification::Localization] The subtitle.

title[RW]

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

@return [String|Gravel::APNS::Notification::Localization] The title.

uuid[RW]

A unique identifier for this notification.

@return [String] The unique identifier.

Public Class Methods

new() click to toggle source

Create a new APNS notification.

@return [Gravel::APNS::Notification] The notification object.

# File lib/gravel/apns/notification.rb, line 112
def initialize
  self.content_available = false
  self.uuid = SecureRandom.uuid
end

Public Instance Methods

for_device_tokens(*tokens) click to toggle source

Quickly create the same notification for multiple device tokens.

@param tokens [Splat] An array of device tokens. @return [Array] An array of notifications.

# 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() click to toggle source

Generate the APNS payload.

@return [Hash] The APNS payload.

# 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