All Projects → danhper → pushex

danhper / pushex

Licence: MIT license
Push notifications for Elixir

Programming Languages

elixir
2628 projects

Projects that are alternatives of or similar to pushex

PUSHTestFCM
[FireMonkey] Push test project
Stars: ✭ 17 (-82.29%)
Mutual labels:  notifications, apns, gcm
Notificationpusher
Standalone PHP library for easy devices notifications push.
Stars: ✭ 1,143 (+1090.63%)
Mutual labels:  notifications, apns, gcm
Onesignal Unity Sdk
OneSignal is a free push notification service for mobile apps. This plugin makes it easy to integrate your Unity app with OneSignal. https://onesignal.com
Stars: ✭ 161 (+67.71%)
Mutual labels:  notifications, apns, gcm
MongoosePush
MongoosePush is a simple Elixir RESTful service allowing to send push notification via FCM and/or APNS.
Stars: ✭ 101 (+5.21%)
Mutual labels:  notifications, apns, gcm
Codehub Push
Push notification server built in Node.js for the iOS application CodeHub
Stars: ✭ 86 (-10.42%)
Mutual labels:  notifications, apns
Onesignal Gradle Plugin
Use with OneSignal-Android-SDK to help integrate it into your Android Studio or Gradle project. https://onesignal.com
Stars: ✭ 49 (-48.96%)
Mutual labels:  notifications, gcm
Pushy
A Java library for sending APNs (iOS/macOS/Safari) push notifications
Stars: ✭ 1,353 (+1309.38%)
Mutual labels:  notifications, apns
epns
📱 Erlang Push Notifications. APNS(Apple Push Notifications) and FCM(Firebase Cloud Messaging) Push Notifications
Stars: ✭ 13 (-86.46%)
Mutual labels:  apns, gcm
Socket.io Push
整合了小米,华为,友盟,谷歌,苹果推送的统一解决方案
Stars: ✭ 605 (+530.21%)
Mutual labels:  notifications, apns
Applozic Ios Sdk
iOS Real Time Chat & Messaging SDK
Stars: ✭ 104 (+8.33%)
Mutual labels:  notifications, apns
Appleapnpush
Send push notification to Apple Devices (iPhone, iPad)
Stars: ✭ 134 (+39.58%)
Mutual labels:  notifications, apns
Toot Relay
Relay that forwards web push notifications to APNs, built for Toot!.app but usable for anyone.
Stars: ✭ 18 (-81.25%)
Mutual labels:  notifications, apns
Apns Http2
A Java library for sending notifications via APNS using Apple's HTTP/2 API.
Stars: ✭ 194 (+102.08%)
Mutual labels:  notifications, apns
Apns2
⚡ HTTP/2 Apple Push Notification Service (APNs) push provider for Go — Send push notifications to iOS, tvOS, Safari and OSX apps, using the APNs HTTP/2 protocol.
Stars: ✭ 2,569 (+2576.04%)
Mutual labels:  notifications, apns
Node Gcm
A NodeJS wrapper library port to send data to Android devices via Google Cloud Messaging
Stars: ✭ 1,286 (+1239.58%)
Mutual labels:  notifications, gcm
Applozic Android Sdk
Android Real Time Chat & Messaging SDK
Stars: ✭ 611 (+536.46%)
Mutual labels:  notifications, gcm
Net Core Push Notifications
Lightweight .NET Core Push Notifications for Android and iOS
Stars: ✭ 105 (+9.38%)
Mutual labels:  notifications, apns
mobile-messaging-sdk-ios
Mobile Messaging SDK for iOS
Stars: ✭ 45 (-53.12%)
Mutual labels:  notifications, apns
Onesignal Ios Sdk
OneSignal is a free push notification service for mobile apps. This plugin makes it easy to integrate your native iOS app with OneSignal. https://onesignal.com
Stars: ✭ 370 (+285.42%)
Mutual labels:  notifications, apns
Airnotifier
Push Notifications Server for Human Beings.
Stars: ✭ 522 (+443.75%)
Mutual labels:  notifications, apns

Pushex

Build Status Coverage Status

Pushex is a library to easily send push notifications with Elixir.

About

Goals

The main goals are the following:

  • Easy to use async API
  • Common API for iOS and Android
  • Multiple applications handling
  • Proper error and response handling
  • Testable using a sanbox mode

Status

Both GCM and APNS are working. APNS delegates to apns4ex for now, I will probably use the HTTP2 API in a later version.

The API is still subject to change, with a minor version bump for each change.

Installation

Add the following to your dependencies mix.ex.

[{:pushex, "~> 0.2"}]

Then, add :pushex to your applications.

Usage

The most basic usage, with no configuration looks like this:

app = %Pushex.GCM.App{name: "a_unique_name_you_like", auth_key: "a GCM API auth key"}
Pushex.push(%{title: "my_title", body: "my_body"}, to: "registration_id", with_app: app)

To avoid having to create or retreive your app each time, you can configure as many apps as you want in your config.exs:

config :pushex,
  gcm: [
    default_app: "first_app",
    apps: [
      [name: "first_app", auth_key: "a key"],
      [name: "other_app", auth_key: "another key"]
    ]
  ],
  apns: [
    default_app: "first_app",
    apps: [
      [name: "first_app", env: :dev, certfile: "/path/to/certfile", pool_size: 5]
    ]
  ]

You can then do the following:

# this will use the default app, "first_app" with the above configuration
Pushex.push(%{title: "my_title", body: "my_body"}, to: "registration_id", using: :gcm)

# this will use the other_app
Pushex.push(%{title: "my_title", body: "my_body"}, to: "registration_id", using: :gcm, with_app: "other_app")

Note that the function is async and only returns a reference, see the response and error handling documentation for more information.

Sending to multiple platforms

If you want to use the same message for both platforms, you can define messages as follow:

message = %{
  common: "this will be in both payloads",
  other: "this will also be in both payloads",
  apns: %{
    alert: "My alert",
    badge: 1
  },
  gcm: %{
    title: "GCM title",
    body: "My body"
  }
}

Pushex.push(message, to: ["apns_token1", "apns_token2"], using: :apns)
Pushex.push(message, to: ["gcm_registration_id1", "gcm_registration_id2"], using: :gcm)

Only :gcm and :apns are currently available.

Passing more options

If you need to pass options, priority for example, you can just pass it in the keyword list and it will be sent.

See

https://developers.google.com/cloud-messaging/http-server-ref#downstream-http-messages-json

for more information.

The parameters from Table 1 should be passed in the keyword list, while the parameters from Table 2 should be passed in the first argument.

For more information about APNS options, see apns4ex docs.

NOTE: if you pass an array to the to parameter, if will automatically be converted to registration_ids when sending the request, to keep a consistent API.

Loading app from somewhere else

If you are saving your auth_keys in your database, you can override the default way to retreive the apps:

# config.exs
config :pushex,
  app_manager_impl: MyAppManager

# my_app_manager.ex
defmodule MyAppManager do
  @behaviour Pushex.AppManager

  def find_app(platform, name) do
    if app = Repo.get_by(App, platform: platform, name: name) do
      make_app(platform, app)
    end
  end

  # transform to a `Pushex._.App`
  defp make_app(:gcm, app) do
    struct(Pushex.GCM.App, Map.from_struct(app))
  end
  defp make_app(:apns, app) do
    struct(Pushex.APNS.App, Map.from_struct(app))
  end
end

Handling responses

To handle responses, you can define a module using Pushex.EventHandler which uses :gen_event to process events.

# config.exs
config :pushex,
  event_handlers: [MyEventHandler]

# my_event_handler.ex
defmodule MyEventHandler do
  use Pushex.EventHandler

  def handle_event({:request, request, {pid, ref}}, state) do
    # do whatever you want with the request
    # for example, logging or saving in a DB
    {:ok, state}
  end

  def handle_event({:response, response, request, {pid, ref}}, state) do
    # do whatever you want with the response and request
    {:ok, state}
  end
end

The ref passed here is the one returned when calling push.

Testing

Pushex offers a sandbox mode to make testing easier.

To enable it, you should add the following to your configuration:

config :pushex,
  sandbox: true

Once you are using the sandbox, the messages will not be sent to GCM or APNS anymore, but stored in Pushex.Sandbox. Furthermore, all the messages will be returned to the process that sent them. Here is a sample test.

test "send notification to users" do
  ref = Pushex.push(%{body: "my message"}, to: "my-user", using: :gcm)
  pid = self()
  assert_receive {{:ok, response}, request, ^ref}
  assert [{{:ok, ^response}, ^request, {^pid, ^ref}}] = Pushex.Sandbox.list_notifications
end

Note that list_notifications depends on the running process, so if you call it from another process, you need to explicitly pass the pid with the :pid option.

Also note that Pushex.push is asynchronous, so if you remove the assert_receive, you will have a race condition. To avoid this, you can use Pushex.Sandbox.wait_notifications/1 instead of Pushex.Sandbox.list_notifications. It will wait (by default for 100ms) until at least :count notifications arrive

test "send notification to users and wait" do
  Enum.each (1..10), fn _ ->
    Pushex.push(%{body: "foo"}, to: "whoever", using: :gcm)
  end
  notifications = Pushex.Sandbox.wait_notifications(count: 10, timeout: 50)
  assert length(notifications) == 10
end

However, the requests are asynchronous, so there is no guaranty that the notifications in the sandbox will in the same order they have been sent.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].