Pigeon v1.1.1 Pigeon.FCM View Source

Firebase Cloud Messaging (FCM)

Link to this section Summary

Types

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

Link to this section Types

Link to this type on_response() View Source
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)

Link to this type push_opts() View Source
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.

Link to this section Functions

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
Link to this function stop_connection(name) View Source
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