README.md 2.47 KB
Newer Older
Miguel Basticioto's avatar
Miguel Basticioto committed
1
2
# NotificationDispatcher

Miguel Basticioto's avatar
Miguel Basticioto committed
3
4
5
This lib is used to send notification events to a rabbitmq queue server. It contains methods 
to create the necessary database structures, for example the notification_messages table or
the devices table to store the tokens necessary for push notification.
Miguel Basticioto's avatar
Miguel Basticioto committed
6
7
8

## Installation

Miguel Basticioto's avatar
Miguel Basticioto committed
9
[Available in Hex](https://hex.pm/packages/notification_dispatcher), this package can be installed
Miguel Basticioto's avatar
Miguel Basticioto committed
10
11
12
13
14
by adding `notification_dispatcher` to your list of dependencies in `mix.exs`:

```elixir
def deps do
  [
Miguel Basticioto's avatar
Miguel Basticioto committed
15
    {:notification_dispatcher, "~> 0.1.3"}
Miguel Basticioto's avatar
Miguel Basticioto committed
16
17
18
19
  ]
end
```

Miguel Basticioto's avatar
Miguel Basticioto committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
Create the following config on your project:
```elixir
config :notification_dispatcher,
       rabbitmq_host: "localhost",
       rabbitmq_username: "guest",
       rabbitmq_password: "guest",
       repo: MyApp.Repo    
```

To store your notification_messages templates you need a migration that calls the following method:
```elixir
  import NotificationDispatcher.Migration

  def change do
    create_notification_messages()
  end
```

If your project requires push notifications by using firebase you need to crate a migration
that calls the following method:
```elixir
  import NotificationDispatcher.Migration

  def change do
    create_notification_messages()
  end
```

You can use the following method to register notification templates on your database:
```elixir
alias NotificationDispatcher.Controller.NotificationMessageController

NotificationMessageController.create(
  %{
    "title" => "title",
    "message" => "message",
    "notification_type" => 0,
    "channel" => 0,
    "locale" => "en",
    "dispatch_offsets" => "[0]"
  }
)
```
Miguel Basticioto's avatar
Miguel Basticioto committed
63

Miguel Basticioto's avatar
Miguel Basticioto committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
This method can return:
```elixir
{:ok, notification_message, json}
``` 
or
```elixir
{:error, changeset}
```         

You can use the following method to create user_devices if your project requires it:
```elixir
alias NotificationDispatcher.Context.NotificationContext

NotificationContext.create_device(
  %{
    "device_token" => "firebase_token_of_user",
    "os" => "android"
  },
  user_id
)
```

If you have the necessity you can also get all devices by user by using the following method:
```elixir
alias NotificationDispatcher.Context.NotificationContext

NotificationContext.get_devices(user_ids)
```

To send a notification event to the rabbitmq queue you can call the following method:
```elixir
alias NotificationDispatcher.Service.NotificationService

NotificationService.send_notification(
  user_id, 
  device_token_or_email, 
  notification_type, 
  params, 
  language, 
  device_id
)
```