This project is a wrapper for using the awesome Onesignal APIs. Currently supports the following Onesignal APIs Create Notification
The package is available in Hex, and can be installed
by adding onesignal_elixir
to your list of dependencies in mix.exs
:
def deps do
[
{:onesignal_elixir, "~> 0.1.5"}
]
end
Config
config :onesignal_elixir,
rest_api_key: "YOUR-ONESIGNAL-REST-API-KEY",
app_id: "YOUR-ONESIGNAL-APP-ID"
Usage Examples
iex(1)> alias OnesignalElixir.{Notification,Builder,Filter}
[OnesignalElixir.Notification, OnesignalElixir.Builder, OnesignalElixir.Filter]
iex(2)> filters = Filter.new() |> Filter.last_session(">","1.2") |> Filter.add_operator("AND") |> Filter.tag("exists", "email")
[ %{field: "last_session", hours_ago: "1.2", relation: ">"},
%{operator: "AND"},
%{field: "tag", key: "email", relation: "exists"}
]
iex(3)> body = OnesignalElixir.new() |> Builder.add_content(:en, "Welcome to One Signal") |> Builder.add_content(:es, "Bienvenido a One Signal")|> Builder.add_heading(:en, "Hello") |> Builder.add_heading(:es, "Hola") |> Builder.add_subtitle(:en, "Welcome") |> Builder.add_subtitle(:es, "Bienvenido") |> Builder.add_filters(filters)
%{
__struct__: OnesignalElixir.Notification,
app_id: "YOUR-ONESIGNAL-APP-ID",
contents: %{en: "Welcome to One Signal", es: "Bienvenido a One Signal"},
filters: [
%{field: "last_session", hours_ago: "1.2", relation: ">"},
%{operator: "AND"},
%{field: "tag", key: "email", relation: "exists"}
],
headings: %{en: "Hello", es: "Hola"},
subtitles: %{en: "Welcome", es: "Bienvenido"}
}
iex(4)> OnesignalElixir.send_notification(body)
{:ok, %{
"external_id" => nil,
"id" => "GENERATED_NOTIFICATION_ID",
"recipients" => 6
}}
iex(5)> body = OnesignalElixir.new() |> Builder.add_content(:en, "Welcome to One Signal") |> Builder.add_content(:es, "Bienvenido a One Signal")|> Builder.add_heading(:en, "Hello") |> Builder.add_heading(:es, "Hola") |> Builder.add_subtitle(:en, "Welcome") |> Builder.add_subtitle(:es, "Bienvenido") |> Builder.include_segment("Active Users")
%{
__struct__: OnesignalElixir.Notification,
app_id: "YOUR-ONESIGNAL-APP-ID",
contents: %{en: "Welcome to One Signal", es: "Bienvenido a One Signal"},
headings: %{en: "Hello", es: "Hola"},
included_segments: ["Active Users"],
subtitles: %{en: "Welcome", es: "Bienvenido"}
}
iex(6)> OnesignalElixir.send_notification(body)
{:ok,
%{
"external_id" => nil,
"id" => "GENERATED_NOTIFICATION_ID",
"recipients" => 5
}}
iex(2)> body = OnesignalElixir.new() |> Builder.include_player_ids(["6e1a1b95-47e9-4978-b2f2-e2e42207e7b5"]) |> Builder.add_heading(:en, "Hello") |> Builder.add_content(:en, "This is the text content")
%{
__struct__: OnesignalElixir.Notification,
app_id: "a9ec9a20-d595-47b6-8efa-be94ecd4abaa",
contents: %{en: "This is the text content"},
headings: %{en: "Hello"},
include_player_ids: ["6e1a1b95-47e9-4978-b2f2-e2e42207e7b5"]
}
iex(3)> OnesignalElixir.send_notification(body)
{:ok,
%{
"external_id" => nil,
"id" => "f796da55-2d8b-4abf-afa5-86c98c9f185b",
"recipients" => 1
}}
iex(4)> body = OnesignalElixir.new() |> Builder.include_external_user_ids(["[email protected]"]) |> Builder.add_heading(:en, "Hello") |> Builder.add_content(:en, "This is the text content")
%{
__struct__: OnesignalElixir.Notification,
app_id: "a9ec9a20-d595-47b6-8efa-be94ecd4abaa",
contents: %{en: "This is the text content"},
headings: %{en: "Hello"},
include_external_user_ids: ["[email protected]"]
}
iex(5)> OnesignalElixir.send_notification(body)
{:ok,
%{
"external_id" => nil,
"id" => "0c49f257-d955-428a-849c-7d9768b210cf",
"recipients" => 1
}}
Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/onesignal_elixir.