Class: Lowdown::Notification

Inherits:
Object
  • Object
show all
Defined in:
lib/lowdown/notification.rb

Overview

A Notification holds the data and metadata about a Remote Notification.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Notification

Returns a new instance of Notification.

Parameters:

  • params (Hash)

    a dictionary of keys described in the Instance Attribute Summary.



45
46
47
# File 'lib/lowdown/notification.rb', line 45

def initialize(params)
  params.each { |key, value| send("#{key}=", value) }
end

Instance Attribute Details

#expirationTime?

Returns the time until which to retry delivery of a notification. By default it is only tried once.

Returns:

  • (Time, nil)

    the time until which to retry delivery of a notification. By default it is only tried once.



24
25
26
# File 'lib/lowdown/notification.rb', line 24

def expiration
  @expiration
end

#idObject?

Returns a object that uniquely identifies this notification and is coercable to a String.

Returns:

  • (Object, nil)

    a object that uniquely identifies this notification and is coercable to a String.



19
20
21
# File 'lib/lowdown/notification.rb', line 19

def id
  @id
end

#payloadHash

Returns the data payload for this notification.

Returns:

  • (Hash)

    the data payload for this notification.



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

def payload
  @payload
end

#priorityInteger?

Returns the priority at which to deliver this notification, which may be 10 or 5 if power consumption should be taken into consideration. Defaults to `10.

Returns:

  • (Integer, nil)

    the priority at which to deliver this notification, which may be 10 or 5 if power consumption should be taken into consideration. Defaults to `10.



30
31
32
# File 'lib/lowdown/notification.rb', line 30

def priority
  @priority
end

#tokenString

Returns a device token.

Returns:

  • (String)

    a device token.



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

def token
  @token
end

#topicString?

Returns the ‘topic’ for this notification.

Returns:

  • (String, nil)

    the ‘topic’ for this notification.



35
36
37
# File 'lib/lowdown/notification.rb', line 35

def topic
  @topic
end

Instance Method Details

#formatted_idString

Formats the #id in the format required by the APN service, which is in groups of 8-4-4-12. It is padded with leading zeroes.

Returns:

  • (String)

    the formatted ID.



62
63
64
65
66
67
# File 'lib/lowdown/notification.rb', line 62

def formatted_id
  if @id
    padded = @id.to_s.rjust(32, "0")
    [padded[0,8], padded[8,4], padded[12,4], padded[16,4], padded[20,12]].join("-")
  end
end

#formatted_payloadHash

Unless the payload contains an aps entry, the payload is assumed to be a mix of APN defined attributes and custom attributes and re-organized according to the specifications.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/lowdown/notification.rb', line 77

def formatted_payload
  if @payload.has_key?("aps")
    @payload
  else
    payload = {}
    payload["aps"] = aps = {}
    @payload.each do |key, value|
      next if value.nil?
      key = key.to_s
      if APS_KEYS.include?(key)
        aps[key] = value
      else
        payload[key] = value
      end
    end
    payload
  end
end

#valid?Boolean

Returns whether this notification holds enough data and metadata to be sent to the APN service.

Returns:

  • (Boolean)

    whether this notification holds enough data and metadata to be sent to the APN service.



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

def valid?
  !!(@token && @payload)
end