Pigeon.FCM (Pigeon v2.0.0-rc.0) View Source

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

Getting Started

  1. Create a FCM dispatcher.
# lib/fcm.ex
defmodule YourApp.FCM do
  use Pigeon.Dispatcher, otp_app: :your_app
end
  1. (Optional) Add configuration to your config.exs.
# config.exs

config :your_app, YourApp.FCM,
  adapter: Pigeon.FCM,
  project_id: "example-project-123",
  service_account_json: File.read!("service-account.json")
  1. Start your dispatcher on application boot.
defmodule YourApp.Application do
  @moduledoc false

  use Application

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

If you skipped step two, include your configuration.

defmodule YourApp.Application do
  @moduledoc false

  use Application

  @doc false
  def start(_type, _args) do
    children = [
      {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,
      project_id: "example-project-123",
      service_account_json: File.read!("service-account.json")
    ]
  end
end
  1. Create a notification.
n = Pigeon.FCM.Notification.new({:token, "reg ID"}, %{"body" => "test message"})
  1. 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)