/pushy-clj

Send APNs push notifications using the new HTTP/2 protocol from Clojure

Primary LanguageClojureEclipse Public License 1.0EPL-1.0

pushy-clj

A Clojure library for sending push notifications via APNS to Apple devices using the new HTTP/2 protocol.

It's a thin wrapper around Pushy.

Make sure you have one of:

Leiningen

The current stable version is:

[pushy-clj "0.4.2"]

Usage

🔧 If you are upgrading from versions prior to 0.4.0, note that the API has changed, because of deep changes to the API of the Pushy Java library itself. You will need to change your existing code to conform to the examples below. There is no longer a no-argument version of make-client, and you need to provide the :dev or :prod keyword as a new, first argument. There is also no longer a connect function; Pushy takes responsibility for the connection as soon as you create the client, until you call disconnect.

First, we create a client and connect to the APNs development server:

(require '[pushy-clj.core :refer :all]
         '[clojure.java.io :as io])

(import 'java.io.File)

;; Old-school certificate-based auth
(with-open [cert (io/input-stream (File. "/path/to/cert.p12"))]
  (def client (make-client :dev cert "password")))
                    ;; use :prod in production env

;; New token-based auth
(with-open [key (io/input-stream (File. "/path/to/key.p8"))]
  (def client (make-client :dev key "team-id" "key-id")))

Pushy is responsible for opening the connection to the specified server, and reopening it if anything causes it to close.

Then we build a notification following Apple's guidelines:

(def payload {:aps {:alert "Hello!"}})

(def notification (build-push-notification "device-token" 
                                           "topic" ;; this can be nil
                                           payload))

Now, we can send the notification:

(def resp-future (send-push-notification client notification)) ;; async operation!

The notification is sent asynchronously, and resp-future is returned immediately. This future can be derefed to get the response as a hashmap.

@resp-future ;; blocking operation

;; => {:accepted? true
;;     :rejection-reason nil
;;     :token-expiration-ts nil}

Finally, we can close the connection using disconnect:

(disconnect client) ;; blocking operation

License

Copyright © 2016 Divyansh Prakash

Distributed under the Eclipse Public License version 1.0.