-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
93 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
defmodule MongoosePush.Service do | ||
@moduledoc """ | ||
Generic interface for push notifications services. | ||
""" | ||
@type notification :: term | ||
|
||
@callback push(notification, string, atom) :: :ok | {:error, term} | ||
@callback prepare_notification(string, MongoosePush.request) :: notification | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
defmodule MongoosePush.Service.APNS do | ||
@moduledoc """ | ||
APNS (apple Push Notification Service) service provider implementation. | ||
""" | ||
|
||
@behaviour MongoosePush.Service | ||
alias Pigeon.APNS | ||
alias MongoosePush.Service | ||
|
||
@spec prepare_notification(string, MongoosePush.request) :: | ||
Service.notification | ||
def prepare_notification(device_id, request) do | ||
%{ | ||
"alert" => %{ | ||
"title" => request.title, | ||
"body" => request.body | ||
}, | ||
"badge" => request[:badge], | ||
"category" => request[:click_action] | ||
} | ||
|> | ||
APNS.Notification.new(device_id, request[:topic]) | ||
end | ||
|
||
@spec push(Service.notification, string, atom) :: :ok | {:error, term} | ||
def push(notification, _device_id, worker) do | ||
case APNS.push(notification, [name: worker]) do | ||
{:ok, _state} -> | ||
:ok | ||
{:error, reason, _state} -> | ||
{:error, reason} | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
defmodule MongoosePush.Service.FCM do | ||
@moduledoc """ | ||
FCM (Firebase Cloud Messaging) service provider implementation. | ||
""" | ||
|
||
@behaviour MongoosePush.Service | ||
alias Pigeon.GCM | ||
|
||
@spec prepare_notification(string, MongoosePush.request) :: | ||
Service.notification | ||
def prepare_notification(device_id, request) do | ||
msg = [:body, :title, :click_action, :tag] | ||
|> Enum.reduce(%{}, fn(field, map) -> | ||
Map.put(map, field, request[field]) | ||
end) | ||
Pigeon.GCM.Notification.new(device_id, msg) | ||
end | ||
|
||
@spec push(Service.notification, string, atom) :: :ok | {:error, term} | ||
def push(notification, device_id, worker) do | ||
case GCM.push(notification, [name: worker]) do | ||
{:ok, state} -> | ||
%Pigeon.GCM.NotificationResponse{ok: ok, update: update} = state | ||
case Enum.member?(ok ++ update, device_id) do | ||
true -> :ok | ||
false -> | ||
{:error, :invalid_device_token} | ||
end | ||
{:error, reason, _state} -> | ||
{:error, reason} | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters