Class: RubyPushNotifications::APNS::APNSNotification

Inherits:
Object
  • Object
show all
Includes:
NotificationResultsManager
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

Attributes included from NotificationResultsManager

#results

Instance Method Summary collapse

Constructor Details

#initialize(tokens, data) ⇒ APNSNotification

Initializes the APNS Notification

Parameters:

  • . (Array)

    Array containing all destinations for the notification

  • . (Hash)

    Hash with the data to use as payload.



21
22
23
24
# File 'lib/ruby-push-notifications/apns/apns_notification.rb', line 21

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

Instance Method Details

#countInteger

Returns . The number of binaries this notification will send. One for each receiver.

Returns:

  • (Integer)

    . The number of binaries this notification will send. One for each receiver.



51
52
53
# File 'lib/ruby-push-notifications/apns/apns_notification.rb', line 51

def count
  @tokens.count
end

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

Parameters:

  • starting_id (Integer)

    . Every notification encodes a unique ID for further reference. This parameter represents the first id the first notification of this group should use.

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
    


42
43
44
45
46
47
# File 'lib/ruby-push-notifications/apns/apns_notification.rb', line 42

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