Class: ApnsKit::Notification

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

Constant Summary collapse

MAXIMUM_PAYLOAD_SIZE =
4096

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Notification

Returns a new instance of Notification.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/apns_kit/notification.rb', line 18

def initialize(options)
    @token = options.delete(:token) || options.delete(:device)
    @alert = options.delete(:alert)
    @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)
    @topic = options.delete(:topic)

    @custom_data = options
end

Instance Attribute Details

#alertObject

Returns the value of attribute alert.



12
13
14
# File 'lib/apns_kit/notification.rb', line 12

def alert
  @alert
end

#custom_dataObject

Returns the value of attribute custom_data.



16
17
18
# File 'lib/apns_kit/notification.rb', line 16

def custom_data
  @custom_data
end

#idObject

Returns the value of attribute id.



10
11
12
# File 'lib/apns_kit/notification.rb', line 10

def id
  @id
end

#tokenObject

Returns the value of attribute token.



8
9
10
# File 'lib/apns_kit/notification.rb', line 8

def token
  @token
end

#topicObject

Returns the value of attribute topic.



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

def topic
  @topic
end

Instance Method Details

#headerObject



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

def header
    json = {
        ':scheme' => 'https',
        ':method' => 'POST',
        ':path' => "/3/device/#{token}",
        'apns-id' => id,
        'content-length' => payload.bytesize.to_s,
        'apns-topic' => topic
    }

    json.merge!({ "apns-expiry" => @expiry }) if @expiry
    json.merge!({ "apns-priority" => @priority }) if @priority
    return json
end

#payloadObject



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/apns_kit/notification.rb', line 56

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

    json['aps'] ||= {}
    json['aps']['alert'] = @alert if @alert
    json['aps']['badge'] = @badge.to_i rescue 0 if @badge
    json['aps']['sound'] = @sound if @sound
    json['aps']['category'] = @category if @category
    json['aps']['content-available'] = 1 if @content_available

    JSON.dump(json)
end

#valid?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/apns_kit/notification.rb', line 37

def valid?
    payload.bytesize <= MAXIMUM_PAYLOAD_SIZE
end