Class: RubyPushNotifications::APNS::APNSNotification

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-push-notifications/apns/apns_notification.rb

Overview

Represents a APNS Notification. Manages the conversion of the notification to APNS binary format for each of the destinations. By default sets maximum expiration date (4 weeks).

Author:

  • Carlos Alonso

Constant Summary collapse

WEEKS_4 =
2419200

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tokens, data) ⇒ APNSNotification

Initializes the APNS Notification


24
25
26
27
# File 'lib/ruby-push-notifications/apns/apns_notification.rb', line 24

def initialize(tokens, data)
  @tokens = tokens
  @data = data
end

Instance Attribute Details

#resultsArray


18
19
20
# File 'lib/ruby-push-notifications/apns/apns_notification.rb', line 18

def results
  @results
end

Instance Method Details

#countInteger


54
55
56
# File 'lib/ruby-push-notifications/apns/apns_notification.rb', line 54

def count
  @tokens.count
end

#each_message(starting_id) {|.| ... } ⇒ Object

Yield Parameters:

  • . (String)

    APNS binary's representation of this notification. Consisting of:

    Notification = 2(1), FrameLength(4), items(FrameLength)
    Item = ItemID(1), ItemLength(2), data(ItemLength)
    Items:
      Device Token => Id: 1, length: 32, data: binary device token
      Payload => Id: 2, length: ??, data: json formatted payload
      Notification ID => Id: 3, length: 4, data: notif id as int
      Expiration Date => Id: 4, length: 4, data: Unix timestamp as int
      Priority => Id: 5, length: 1, data: 10 as 1 byte int
    

45
46
47
48
49
50
# File 'lib/ruby-push-notifications/apns/apns_notification.rb', line 45

def each_message(starting_id)
  @tokens.each_with_index do |token, i|
    bytes = device_token(token) + payload + notification_id(starting_id + i) + expiration_date + priority
    yield [2, bytes.bytesize, bytes].pack 'cNa*'
  end
end