Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Silent notifications #10

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,19 @@ config :one_signal, OneSignal,
|> put_segment("New Players")
|> notify
```


# Background notifications

You can also add background notifications. Useful for when you want to trigger background tasks in your apps that are not currently in the foreground

```elixir
import OneSignal.Param
OneSignal.new
|> put_segment("Free Players")
|> put_segment("New Players")
|> put_data("payload", %{ hash: "tag"}) # send payload data as data
|> background_notify


```
23 changes: 19 additions & 4 deletions lib/one_signal/notification.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
defmodule OneSignal.Notification do
defstruct id: nil, recipients: 0

def post_notification_url() do
OneSignal.endpoint <> "/notifications"
end


@doc """
Send push notification
Expand All @@ -22,14 +20,31 @@ defmodule OneSignal.Notification do
end

def send(body) do
case OneSignal.API.post(post_notification_url, body) do
case OneSignal.API.post(post_notification_url(), body) do
{:ok, response} ->
response = Enum.map(response, &to_key_atom/1)
struct(__MODULE__, response)
err -> err
end
end

def send_background(body) do
body = body
|> Map.put("content_available", true) #needed to setup backgorund
|> Map.delete("contents") # need to remove contents for it to work

case OneSignal.API.post(post_notification_url(), body) do
{:ok, response} ->
response = Enum.map(response, &to_key_atom/1)
struct(__MODULE__, response)
err -> err
end
end

def post_notification_url() do
OneSignal.endpoint <> "/notifications"
end

def to_key_atom({k, v}) do
{String.to_atom(k), v}
end
Expand Down
13 changes: 12 additions & 1 deletion lib/one_signal/param.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ defmodule OneSignal.Param do
|> OneSignal.Notification.send
end

def background_notify(%Param{} = param) do
param
|> build
|> OneSignal.Notification.send_background
end

@doc """
Build notifications parameter of request
"""
Expand Down Expand Up @@ -102,7 +108,12 @@ defmodule OneSignal.Param do

iex> OneSignal.new
|> put_message("Hello")
|> put_filter("{userId: asdf}")
|> put_filter(%{
field: "tag",
key: "userId",
relation: "is",
value: "asdf"
})
"""
def put_filter(%Param{filters: filters} = param, filter) do
%{param | filters: [filter | filters]}
Expand Down