Class: Expo::Push::Middleware::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/expo-push-middleware.rb

Defined Under Namespace

Classes: Error, TooManyMessagesError

Instance Method Summary collapse

Constructor Details

#initialize(**args) ⇒ Client

Returns a new instance of Client.



28
29
30
# File 'lib/expo-push-middleware.rb', line 28

def initialize(**args)
  @http_client = args[:http_client] || Typhoeus
end

Instance Method Details

#send_messages(messages, **_) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/expo-push-middleware.rb', line 32

def send_messages(messages, **_)
  # https://docs.expo.io/versions/latest/guides/push-notifications/#message-format
  expo_tokens = messages.map { |message| message.with_indifferent_access[:to] }.flatten
  raise TooManyMessagesError, "Only 100 message objects at a time allowed." if expo_tokens.length > 100


  @http_client.post(
    "#{Expo::Push::Middleware.config.api_url}/api/v1/push_requests",
    body: { push_request: { messages: messages } }.to_json,
    headers: headers
  )
end