Web Push Elixir
Simple web push library for Elixir.
Prerequisities
- Elixir 1.15, OTP 24 / 25 / 26
Installation
- Add
web_push_elixirto your list of dependencies inmix.exs:
def deps do
[
{:web_push_elixir, "~> 0.1.0"}
]
end
- Run mix command to generate your Vapid public and private keys:
mix generate.vapid.keys
- Set your environment variables for your keys:
environment:
- VAPID_PUBLIC_KEY=someVapidPublicKey
- VAPID_PRIVATE_KEY=someVapidPrivateKey
- VAPID_SUBJECT=mailto:admin@email.com
Usage
WebPushElixir provides a simple public API send_notification/2 that accepts 2 arguments:
subscription: is the subscription information received from the client.message: is a message string.
subscription = '{"endpoint":"https://some-push-service","keys":{"p256dh":"BNcRdreALRFXTkOOUHK1EtK2wtaz5Ry4YfYCA_0QTpQtUbVlUls0VJXg7A8u-Ts1XbjhazAkj7I99e8QcYP7DkM=","auth":"tBHItJI5svbpez7KI4CCXg=="}}'
message = "Some message"
WebPushElixir.send_notification(subscription, message)
For more information on how to subscribe a client, permission UX and more - have a look at https://web.dev/notifications/
Credits
Heavily inspired by elixir-web-push-encryption