首页 > 代码库 > Clojure:两步发送iOS推送通知(apns)

Clojure:两步发送iOS推送通知(apns)

首先在project.clj中,添加对notnoop 类库的引用:[com.notnoop.apns/apns "0.2.3"]

然后使用如下方法就可以发送推送消息了:

 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-count10       (do11         (let [service (.build (.withSandboxDestination12                                 (.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))))))

调用方法如下:

(send-push-notification [“token1” “token2” …] “test message”)

 

需要注意证书必须为p12格式(可以通过KeyChain Access软件转换)