Clojure:兩步發送iOS推送通知(apns)

首先在project.clj中,添加對notnoop 類庫的引用:[com.notnoop.apns/apns "0.2.3"]oop

而後使用以下方法就能夠發送推送消息了:ui

 1 (ns demo.apns
 2   (:import (com.notnoop.apns APNS)))
 3 
 4 (defn send-push-notification
 5   [device-tokens message]
 6   (loop [rest-device-tokens device-tokens
 7          sent-count 0]
 8     (if (empty? rest-device-tokens)
 9       sent-count
10       (do
11         (let [service (.build (.withSandboxDestination
12                                 (.withCert (APNS/newService)
13                                            "resources/demo.p12"
14                                            "password")))
15               payload (.build (.alertBody (APNS/newPayload) message))]
16           (.push service (first rest-device-tokens) payload))
17         (recur (rest rest-device-tokens) (inc sent-count))))))

調用方法以下:spa

(send-push-notification [「token1」 「token2」 …] 「test message」)rest

 

須要注意證書必須爲p12格式(能夠經過KeyChain Access軟件轉換)code

相關文章
相關標籤/搜索