View Source Pigeon.FCM (Pigeon v1.6.3)

Firebase Cloud Messaging (FCM)

Summary

Types

Can be either a single notification or a list.

Async callback for push notification response.

Options for sending push notifications.

Functions

Sends a push over FCM.

Starts FCM worker connection with given config or name.

Stops existing FCM worker connection.

Types

@type notification() ::
  Pigeon.FCM.Notification.t() | [Pigeon.FCM.Notification.t(), ...]

Can be either a single notification or a list.

@type on_response() :: (Pigeon.FCM.Notification.t() -> no_return())

Async callback for push notification response.

Examples

handler = fn(n) ->
  case n.status do
    :success ->
      bad_regids = FCM.Notification.remove?(n)
      to_retry = FCM.Notification.retry?(n)
      # Handle updated regids, remove bad ones, etc
    :unauthorized ->
      # Bad FCM key
    error ->
      # Some other error
  end
end

n = Pigeon.FCM.Notification.new("device token", %{}, %{"message" => "test"})
Pigeon.FCM.push(n, on_response: handler)
@type push_opts() :: [
  to: atom() | pid() | nil,
  timeout: pos_integer() | nil,
  on_response: on_response() | nil
]

Options for sending push notifications.

  • :to - Defines worker to process push. Defaults to :fcm_default
  • :on_response - Optional async callback triggered on receipt of push. See on_response/0
  • :timeout - Specifies timeout for push responses. Useful if sending large batches synchronously.

Functions

Link to this function

push(notification, opts \\ [])

View Source
@spec push(notification(), Keyword.t()) :: notification() | :ok

Sends a push over FCM.

Examples

iex> n = Pigeon.FCM.Notification.new("regId", %{}, %{"message" => "123"})
iex> Pigeon.FCM.push(n)
%Pigeon.FCM.Notification{message_id: nil,
 payload: %{"data" => %{"message" => "123"}}, priority: :normal,
 registration_id: "regId", status: :success, response:
 [invalid_registration: "regId"]}

iex> n = Pigeon.FCM.Notification.new("regId", %{}, %{"message" => "123"})
iex> Pigeon.FCM.push(n, on_response: nil)
:ok

iex> n = Pigeon.FCM.Notification.new(["regId", "regId"], %{},
...> %{"message" => "123"})
iex> Pigeon.FCM.push(n)
%Pigeon.FCM.Notification{message_id: nil,
 payload: %{"data" => %{"message" => "123"}}, priority: :normal,
 registration_id: ["regId", "regId"], status: :success,
 response: [invalid_registration: "regId",
 invalid_registration: "regId"]}

iex> n = Pigeon.FCM.Notification.new(["regId", "regId"], %{},
...> %{"message" => "test"})
iex> notifs = Pigeon.FCM.push([n, n])
iex> Enum.map(notifs, & &1.response)
[[invalid_registration: "regId", invalid_registration: "regId"],
 [invalid_registration: "regId", invalid_registration: "regId"]]
Link to this function

start_connection(opts \\ [])

View Source

Starts FCM worker connection with given config or name.

Examples

iex> config = Pigeon.FCM.Config.new(:fcm_default)
iex> {:ok, pid} = Pigeon.FCM.start_connection(%{config | name: nil})
iex> Process.alive?(pid)
true
@spec stop_connection(atom() | pid()) :: :ok

Stops existing FCM worker connection.

Examples

iex> config = Pigeon.FCM.Config.new(:fcm_default)
iex> {:ok, pid} = Pigeon.FCM.start_connection(%{config | name: nil})
iex> Pigeon.FCM.stop_connection(pid)
:ok
iex> :timer.sleep(500)
iex> Process.alive?(pid)
false