Class: Push0r::ApnsPushMessage

Inherits:
PushMessage show all
Defined in:
lib/push0r/APNS/ApnsPushMessage.rb

Overview

ApnsPushMessage is a PushMessage implementation that encapsulates a single push notification to be sent to a single user.

Instance Attribute Summary collapse

Attributes inherited from PushMessage

#identifier, #payload, #receiver_token, #time_to_live

Instance Method Summary collapse

Methods inherited from PushMessage

#attach

Constructor Details

#initialize(receiver_token, environment = ApnsEnvironment::PRODUCTION, identifier = nil, time_to_live = nil) ⇒ ApnsPushMessage

Returns a new ApnsPushMessage instance that encapsulates a single push notification to be sent to a single user.

Parameters:

  • receiver_token (String)

    the apns push token (aka device token) to push the notification to

  • environment (Fixnum) (defaults to: ApnsEnvironment::PRODUCTION)

    the environment to use when sending this push message. Defaults to ApnsEnvironment::PRODUCTION.

  • identifier (Fixnum) (defaults to: nil)

    a unique identifier to identify this push message during error handling. If nil, a random identifier is automatically generated.

  • time_to_live (Fixnum) (defaults to: nil)

    The time to live in seconds for this push messages. If nil, the time to live is set to zero seconds.



11
12
13
14
15
16
17
# File 'lib/push0r/APNS/ApnsPushMessage.rb', line 11

def initialize(receiver_token, environment = ApnsEnvironment::PRODUCTION, identifier = nil, time_to_live = nil)
  if identifier.nil? ## make sure the message has an identifier (required for apns error handling)
    identifier = Random.rand(2**32)
  end
  super(receiver_token, identifier, time_to_live)
  @environment = environment
end

Instance Attribute Details

#environmentObject (readonly)

Returns the value of attribute environment.



4
5
6
# File 'lib/push0r/APNS/ApnsPushMessage.rb', line 4

def environment
  @environment
end

Instance Method Details

#simple(alert_text = nil, sound = nil, badge = nil, category = nil) ⇒ Object

Convenience method to attach common data (that is an alert, a sound or a badge value) to this message’s payload.

Parameters:

  • alert_text (String) (defaults to: nil)

    the alert text to be displayed

  • sound (String) (defaults to: nil)

    the sound to be played

  • badge (Fixnum) (defaults to: nil)

    the badge value to be displayed

  • category (String) (defaults to: nil)

    the category this message belongs to (see UIUserNotificationCategory in apple’s documentation)



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/push0r/APNS/ApnsPushMessage.rb', line 24

def simple(alert_text = nil, sound = nil, badge = nil, category = nil)
  new_payload = {aps: {}}
  if alert_text
    new_payload[:aps][:alert] = alert_text
  end
  if sound
    new_payload[:aps][:sound] = sound
  end
  if badge
    new_payload[:aps][:badge] = badge
  end
  if category
    new_payload[:aps][:category] = category
  end

  @payload.merge!(new_payload)

  return self
end