View Source Pigeon.FCM (Pigeon v2.0.0-rc.2)
Pigeon.Adapter
for Firebase Cloud Messaging (FCM) push notifications.
Getting Started
- Create a
FCM
dispatcher.
# lib/fcm.ex
defmodule YourApp.FCM do
use Pigeon.Dispatcher, otp_app: :your_app
end
- (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")
- 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
- 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)