Class: P8Pusher::Notification

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

Defined Under Namespace

Classes: APNSError

Constant Summary collapse

MAXIMUM_PAYLOAD_SIZE =
2048

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Notification

Returns a new instance of Notification.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/p8_pusher/notification.rb', line 40

def initialize(options = {})
  @token = options.delete(:token) || options.delete(:device)
  @title = options.delete(:title)
  @subtitle = options.delete(:subtitle)
  @body = options.delete(:body)
  @topic = options.delete(:topic) || ENV['APN_BUNDLE_ID']
  @badge = options.delete(:badge)
  @sound = options.delete(:sound)
  @category = options.delete(:category)
  @expiry = options.delete(:expiry)
  @id = options.delete(:id)
  @priority = options.delete(:priority)
  @content_available = options.delete(:content_available)
  @mutable_content = options.delete(:mutable_content)

  @custom_data = options
end

Instance Attribute Details

#apns_error_code=(value) ⇒ Object (writeonly)

Sets the attribute apns_error_code

Parameters:

  • value

    the value to set the attribute apns_error_code to.



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

def apns_error_code=(value)
  @apns_error_code = value
end

#badgeObject

Returns the value of attribute badge.



32
33
34
# File 'lib/p8_pusher/notification.rb', line 32

def badge
  @badge
end

#bodyObject

Returns the value of attribute body.



32
33
34
# File 'lib/p8_pusher/notification.rb', line 32

def body
  @body
end

#categoryObject

Returns the value of attribute category.



32
33
34
# File 'lib/p8_pusher/notification.rb', line 32

def category
  @category
end

#content_availableObject

Returns the value of attribute content_available.



32
33
34
# File 'lib/p8_pusher/notification.rb', line 32

def content_available
  @content_available
end

#custom_dataObject

Returns the value of attribute custom_data.



32
33
34
# File 'lib/p8_pusher/notification.rb', line 32

def custom_data
  @custom_data
end

#expiryObject

Returns the value of attribute expiry.



32
33
34
# File 'lib/p8_pusher/notification.rb', line 32

def expiry
  @expiry
end

#idObject

Returns the value of attribute id.



32
33
34
# File 'lib/p8_pusher/notification.rb', line 32

def id
  @id
end

#mutable_contentObject

Returns the value of attribute mutable_content.



32
33
34
# File 'lib/p8_pusher/notification.rb', line 32

def mutable_content
  @mutable_content
end

#priorityObject

Returns the value of attribute priority.



32
33
34
# File 'lib/p8_pusher/notification.rb', line 32

def priority
  @priority
end

#sent_atObject (readonly)

Returns the value of attribute sent_at.



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

def sent_at
  @sent_at
end

#soundObject

Returns the value of attribute sound.



32
33
34
# File 'lib/p8_pusher/notification.rb', line 32

def sound
  @sound
end

#subtitleObject

Returns the value of attribute subtitle.



32
33
34
# File 'lib/p8_pusher/notification.rb', line 32

def subtitle
  @subtitle
end

#titleObject

Returns the value of attribute title.



32
33
34
# File 'lib/p8_pusher/notification.rb', line 32

def title
  @title
end

#tokenObject Also known as: device

Returns the value of attribute token.



32
33
34
# File 'lib/p8_pusher/notification.rb', line 32

def token
  @token
end

#topicObject

Returns the value of attribute topic.



32
33
34
# File 'lib/p8_pusher/notification.rb', line 32

def topic
  @topic
end

Instance Method Details

#errorObject



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

def error
  APNSError.new(@apns_error_code) if @apns_error_code && @apns_error_code.nonzero?
end

#mark_as_sent!Object



84
85
86
# File 'lib/p8_pusher/notification.rb', line 84

def mark_as_sent!
  @sent_at = Time.now
end

#mark_as_unsent!Object



88
89
90
# File 'lib/p8_pusher/notification.rb', line 88

def mark_as_unsent!
  @sent_at = nil
end

#messageObject



75
76
77
78
79
80
81
82
# File 'lib/p8_pusher/notification.rb', line 75

def message
  data = [device_token_item,
          payload_item,
          identifier_item,
          expiration_item,
          priority_item].compact.join
  [2, data.bytes.count, data].pack('cNa*')
end

#payloadObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/p8_pusher/notification.rb', line 58

def payload
  json = {}.merge(@custom_data || {}).inject({}) { |h, (k, v)| h[k.to_s] = v; h }

  json['aps'] ||= {}
  json['aps']['alert'] ||= {}
  json['aps']['alert']['title'] = @title if @title
  json['aps']['alert']['subtitle'] = @subtitle if @subtitle
  json['aps']['alert']['body'] = @body if @body
  json['aps']['badge'] = @badge&.to_i if @badge
  json['aps']['sound'] = @sound if @sound
  json['aps']['category'] = @category if @category
  json['aps']['content-available'] = 1 if @content_available
  json['aps']['mutable-content'] = 1 if @mutable_content

  json
end

#sent?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/p8_pusher/notification.rb', line 92

def sent?
  !!@sent_at
end

#valid?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/p8_pusher/notification.rb', line 96

def valid?
  payload.to_json.bytesize <= MAXIMUM_PAYLOAD_SIZE
end