-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.clj
85 lines (69 loc) · 3.32 KB
/
core.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
(ns clj-push-notifications.core
(:import
java.io.FileInputStream
java.io.ByteArrayInputStream
(com.google.firebase FirebaseOptions FirebaseOptions$Builder FirebaseApp)
com.google.auth.oauth2.GoogleCredentials
(com.google.firebase.messaging FirebaseMessaging Message MulticastMessage
Notification AndroidConfig AndroidNotification
ApnsConfig Aps)))
(defn- string-to-stream [string]
(-> string
(.getBytes)
(ByteArrayInputStream.)))
(defn init-firebase
([credentials] (init-firebase credentials nil))
([credentials db-url]
(let [refreshToken (string-to-stream credentials)
builder (FirebaseOptions$Builder.)
builder-with-cred (.setCredentials builder (GoogleCredentials/fromStream refreshToken))
builder-with-db (if db-url
(.setDatabaseUrl builder-with-cred db-url)
builder-with-cred)
options (.build builder-with-db)]
(FirebaseApp/initializeApp options))))
(defn delete-firebase []
(.delete (FirebaseApp/getInstance)))
(defn- get-notification [builder title message badge custom-field custom-data type]
(assert (string? custom-field))
(assert (int? badge))
(assert (string? type))
(let [notification (Notification. title message)
android-notification (doto (AndroidNotification/builder)
(.setIcon "stock_ticker_update")
(.setColor "#f45342")
(.setTitle title)
(.setClickAction type))
android-notification (.build android-notification)
android-config (doto (AndroidConfig/builder)
(.setTtl (* 3600 1000))
(.setNotification android-notification)
(.putData custom-field custom-data))
android-config (.build android-config)
aps (doto (Aps/builder)
(.setBadge badge)
(.putCustomData custom-field custom-data)
(.setCategory type))
aps (.build aps)
apns-config (doto (ApnsConfig/builder)
(.setAps aps))
apns-config (.build apns-config)
message (doto builder
(.setNotification notification)
(.setAndroidConfig android-config)
(.setApnsConfig apns-config))]
message))
(defn send-notification [{:keys [token title message badge custom-field custom-data type]}]
(let [custom-field (or custom-field "")
custom-data (or custom-data "")
message (get-notification (Message/builder) title message badge custom-field custom-data type)
message-with-token (.setToken message token)
builded (.build message-with-token)]
(future (.send (FirebaseMessaging/getInstance) builded))))
(defn send-multicast-notifications [{:keys [tokens title message badge custom-field custom-data type]}]
(let [custom-field (or custom-field "")
custom-data (or custom-data "")
message (get-notification (MulticastMessage/builder) title message badge custom-field custom-data type)
message-with-token (.addAllTokens message tokens)
built (.build message-with-token)]
(future (.sendMulticast (FirebaseMessaging/getInstance) built))))