diff --git a/lib/push/chunk.rb b/lib/push/chunk.rb index 8fa5f22..617e135 100644 --- a/lib/push/chunk.rb +++ b/lib/push/chunk.rb @@ -49,6 +49,10 @@ def as_json notifications.map(&:as_json) end + def all_recipients + notifications.flat_map(&:recipients) + end + private attr_accessor :notifications diff --git a/lib/push/client.rb b/lib/push/client.rb index 9eba722..cd4f03f 100644 --- a/lib/push/client.rb +++ b/lib/push/client.rb @@ -183,7 +183,7 @@ def send(notifications) threads = Chunk.for(notifications).map do |chunk| expected_count = chunk.count - + tokens = chunk.all_recipients Thread.new do pool.with do |http| response = http.post(PUSH_API_URL, json: chunk.as_json) @@ -197,7 +197,10 @@ def send(notifications) elsif !data.is_a?(Array) || data.length != expected_count TicketsExpectationFailed.new(expected_count: expected_count, data: data) else - data.map { |ticket| Ticket.new(ticket) } + data.map do |ticket| + current_ticket_token = tokens.shift(1)[0] + Ticket.new(ticket, current_ticket_token) + end end end end diff --git a/lib/push/receipts.rb b/lib/push/receipts.rb index 63334a9..bd28db6 100644 --- a/lib/push/receipts.rb +++ b/lib/push/receipts.rb @@ -33,6 +33,10 @@ def message data.fetch('message') end + def error_message + data.fetch('details').fetch('error') + end + def explain Expo::Push::Error.explain((data['details'] || {})['error']) end diff --git a/lib/push/tickets.rb b/lib/push/tickets.rb index 02394d1..ea8db5b 100644 --- a/lib/push/tickets.rb +++ b/lib/push/tickets.rb @@ -12,10 +12,11 @@ module Push # valid. This is exposed via #original_push_token. # class Ticket - attr_reader :data + attr_reader :data, :token - def initialize(data) + def initialize(data, token) self.data = data + self.token = token end def id @@ -23,13 +24,7 @@ def id end def original_push_token - return nil if ok? - - if message.include?('PushToken[') - return /Expo(?:nent)?PushToken\[(?:[^\]]+?)\]/.match(message) { |match| match[0] } - end - - /\A[a-z\d]{8}-[a-z\d]{4}-[a-z\d]{4}-[a-z\d]{4}-[a-z\d]{12}\z/i.match(message) { |match| match[0] } + token end def message @@ -50,7 +45,7 @@ def error? private - attr_writer :data + attr_writer :data, :token end ## @@ -80,6 +75,13 @@ def ids end end + def token_by_receipt_id + tokens_by_receipt_id = {} + tokens_by_receipt_id.tap do |hash| + each { |ticket| hash[ticket.id] = ticket.original_push_token } + end + end + def batch_ids ids.each_slice(PUSH_NOTIFICATION_RECEIPT_CHUNK_LIMIT).to_a end