0

I'm developing an application in Expo(React-native) that sends notifications to users each time there is a new article in the database. I use expo-notification for this. But after reading the documentation, I realized that the token created is unique. It therefore belongs to a single device. How can I send notifications to several devices at the same time?

I used the documentation to manage notification sending from the back-end. Here's the code.

defmodule PhoenixApi.NotificationsExpo do

  alias HTTPoison

  @url "https://exp.host/--/api/v2/push/send"

  @token "ExponentPushToken[XXXXXXXXXXXXXXXXXXXXX]"

  def send_to_expo(messages) when is_list(messages) do
    headers = [{"content-type", "application/json"}]
    body = Jason.encode!(messages |> add_token_to_messages())

    case HTTPoison.post(@url, body, headers) do
      {:ok, %HTTPoison.Response{body: body}} ->
        {:ok, Jason.decode!(body)}

      {:error, %HTTPoison.Error{reason: reason}} ->
        {:error, reason}
    end
  end

  defp add_token_to_messages(messages) do
    Enum.map(messages, fn message ->
      Map.put(message, "to", @token)
    end)
  end

end

Thanks for your answer.

ckm_bg
  • 1
  • You would need to iterate over all of the records in your database and send a notification to each one. You can use HTTP/2 to allow concurrent streams if your server side library supports it – Paulw11 Aug 18 '23 at 09:38

0 Answers0