A Firebase Cloud Message client for Elixir
MIT License
A Firebase Cloud Message client for Elixir
Add to dependencies
def deps do
[{:fcmex, "~> 0.6.0"}]
end
mix deps.get
Fcmex by default reading FCM server key from your environment variable FCM_SERVER_KEY
on runtime.
If FCM_SERVER_KEY
is not found in your environment, it fallbacks to search following line.
config :fcmex,
server_key: "a_example_key"
{:ok, body} = Fcmex.push("user_token",
notification: %{
title: "foo",
body: "bar",
click_action: "open_foo",
icon: "new",
}
)
{:ok, body} = Fcmex.push("/topics/topic_name",
notification: %{
title: "foo",
body: "bar",
click_action: "open_foo",
icon: "new",
}
)
{:ok, body} = Fcmex.push("user_token",
data: %{
nick: "Mario",
body: "great match!",
room: "PortugalVSDenmark",
}
)
{:ok, body} = Fcmex.push("user_token",
notification: %{
title: "foo",
body: "bar",
click_action: "open_foo",
icon: "new",
},
data: %{
nick: "Mario",
body: "great match!",
room: "PortugalVSDenmark",
}
)
[ok: body] = Fcmex.push(["user_token", "user_token_2"],
notification: %{
title: "foo",
body: "bar",
click_action: "open_foo",
icon: "new",
}
)
As the FCM limitation of multiple send at once is up to 1000, Fcmex chunks tokens to list of 1000 tokens.
If specified tokens is over than 1000 tokens, then response is returned by keyword list chunked by every 1000 requests. (order is not guaranteed)
[ok: result, ok: result2, ...]
If one of request goes something wrong (e.g. timeout, server error), then fcmex returns results with :error
keyword.
[ok: result, error: result2, ...]
# create a subscription
{:ok, result} = Fcmex.Subscription.subscribe("topic_name", "fcm_token")
# get subscription information related with specified token
{:ok, result} = Fcmex.Subscription.get("fcm_token")
iex> result
%{
"application" => "application_name",
"applicationVersion" => "3.6.1",
"authorizedEntity" => "1234567890",
"platform" => "IOS",
"rel" => %{"topics" => %{"test_topic" => %{"addDate" => "2018-05-03"}}},
"scope" => "*"
}}
# create multiple subscriptions
{:ok, result} = Fcmex.Subscription.subscribe("topic_name", ["fcm_token", "fcm_token2"])
# unsubscribe a topic
{:ok, result} = Fcmex.Subscription.unsubscribe("topic_name", "fcm_token")
# batch unsubscribe from a topic
{:ok, result} = Fcmex.Subscription.unsubscribe("topic_name", ["fcm_token", "fcm_token2"])
iex> Fcmex.unregistered?(token)
true
iex> tokens = ["token1", "token2", ...]
iex> Fcmex.filter_unregistered_tokens(tokens)
["token1"]
You can use these options as well.
priority
: default: "high"
collapse_key
: default: nil
time_to_live
: default: nil
content_available
: default: nil
Fcmex.push(["user_token", "user_token_2"],
notification: %{
title: "foo",
body: "bar",
click_action: "open_foo",
icon: "new",
},
priority: "normal",
collapse_key: "data",
time_to_live: 1000,
content_available: true
)
A more detail of parameters are available on Firebase doc page.
You can set httpoison option as below.
config :fcmex,
fcm_server_key: {:system, "FCM_SERVER_KEY"} || System.get_env("FCM_SERVER_KEY"),
httpoison_options: [ssl: [{:versions, [:'tlsv1.2']}], recv_timeout: 500]
fcmex
uses Poison
to encode/decode JSON by default. If you want to use alternative library like Jason
, add the package to mix.exs
and set the module to config.
# mix.exs
def deps do
[
...,
{:jason, "~> 1.3"}
]
end
# config/config.exs
config :fcmex, :json_library, Jason
If you start contributing and you want to run mix test, first you need to export FCM_SERVER_KEY environment variable in the same shell as the one you will be running mix test in.
export FCM_SERVER_KEY="yourkey"
mix test
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)MIT