Skip to content

Commit

Permalink
Merge pull request #47 from renanmedina/hotfix/deprecated-api-warning
Browse files Browse the repository at this point in the history
  • Loading branch information
miyataka authored Oct 21, 2024
2 parents 650c31c + 17762ca commit 4158443
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ json = response.json
json[:name] # => "projects/[your_project_id]/messages/0:1571037134532751%31bd1c9631bd1c96"
```

### push messages in batch
### push messages in batch [DEPRECATED]
```ruby
require 'fcmpush'

Expand Down
13 changes: 8 additions & 5 deletions lib/fcmpush/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,15 @@ def unsubscribe(topic, *instance_ids, query: {}, headers: {})
raise NetworkError, "A network error occurred: #{e.class} (#{e.message})"
end

#-------------------------------------------------------------------------------
# BATCH REQUESTS DEPRECATED BY GOOGLE ON June 20,2023 AND SHUTDOWN ON June 21, 2024
# https://firebase.google.com/support/faq/#fcm-depr-features
#
# Until upgrading to use HTTP/2, warning and throwing error
#-------------------------------------------------------------------------------
def batch_push(messages, query: {}, headers: {})
uri, request = make_batch_request(messages, query, headers)
response = exception_handler(connection.request(uri, request))
BatchResponse.new(response)
rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
raise NetworkError, "A network error occurred: #{e.class} (#{e.message})"
warn '[DEPRECATION] `batch_push` is deprecated. Please use `push` for each message instead.'
raise DeprecatedApiError, 'BATCH REQUESTS DEPRECATED BY GOOGLE ON June 20,2023 AND SHUTDOWN ON June 21, 2024 (https://firebase.google.com/support/faq/#fcm-depr-features)'
end

private
Expand Down
1 change: 1 addition & 0 deletions lib/fcmpush/exceptions.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Fcmpush
class APIError < StandardError; end
class DeprecatedApiError < StandardError; end
class NetworkError < APIError; end

class HttpError < APIError
Expand Down
33 changes: 16 additions & 17 deletions spec/fcmpush_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,22 @@
end

context '#batch_push' do
it 'batch test' do
client = Fcmpush.new(project_id)
message_jsons = 5.times.map do |i|
{ message: { token: device_token,
notification: { title: "test title#{i}",
body: "test body#{i}" } } }
end
response = client.batch_push(message_jsons)

json = response.json

expect(response.code).to eq('200')
expect(response.success_count).to eq(5)
expect(response.failure_count).to eq(0)
expect(json.length).to eq(5)
result = json.all? { |i| i[:name]&.start_with?("projects/#{project_id}/messages/") }
expect(result).to be true
it 'raises DeprecatedApiError' do
expect do
client = Fcmpush.new(project_id)
message_jsons = 5.times.map do |i|
{
message: {
token: device_token,
notification: {
title: "test title#{i}",
body: "test body#{i}"
}
}
}
end
client.batch_push(message_jsons)
end.to raise_error(Fcmpush::DeprecatedApiError)
end
end
end
Expand Down

0 comments on commit 4158443

Please sign in to comment.