View Source Pigeon.FCM (Pigeon v2.0.0)

Pigeon.Adapter for Firebase Cloud Messaging (FCM) push notifications.

Getting Started

Create a dispatcher.

  # lib/your_app/fcm.ex

  defmodule YourApp.FCM do
    use Pigeon.Dispatcher, otp_app: :your_app
  end

Install and configure Goth.

Install and configure goth if you haven't already. Pigeon.FCM requires it for token authentication.

Configure your dispatcher.

Configure your FCM dispatcher and start it on application boot.

# config.exs

config :your_app, YourApp.FCM,
  adapter: Pigeon.FCM,
  auth: YourApp.Goth, # Your Goth worker configured in the previous step.
  project_id: "example-project-123"

Add it to your supervision tree.

defmodule YourApp.Application do
  @moduledoc false

  use Application

  @doc false
  def start(_type, _args) do
    children = [
      {Goth, name: YourApp.Goth},
      YourApp.FCM
    ]
    opts = [strategy: :one_for_one, name: YourApp.Supervisor]
    Supervisor.start_link(children, opts)
  end
end

If preferred, you can include your configuration directly.

defmodule YourApp.Application do
  @moduledoc false

  use Application

  @doc false
  def start(_type, _args) do
    children = [
      {Goth, name: YourApp.Goth},
      {YourApp.FCM, fcm_opts()}
    ]
    opts = [strategy: :one_for_one, name: YourApp.Supervisor]
    Supervisor.start_link(children, opts)
  end

  defp fcm_opts do
    [
      adapter: Pigeon.FCM,
      auth: YourApp.Goth,
      project_id: "example-project-123"
    ]
  end
end

Create a notification.

n = Pigeon.FCM.Notification.new({:token, "reg ID"}, %{"body" => "test message"})

Send the notification.

On successful response, :name will be set to the name returned from the FCM API and :response will be :success. If there was an error, :error will contain a JSON map of the response and :response will be an atomized version of the error type.

YourApp.FCM.push(n)

Customizing Goth

You can use any of the configuration options (e.g. :source) for Goth. Check out the documentation of Goth.start_link/1 for more details.