-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
141 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
(ns ketchup.notify | ||
(:require [sdk.expo :as expo] | ||
[ketchup.db :as db] | ||
[ketchup.env :as env])) | ||
|
||
(defn status-change! [user-id status] | ||
(let [expo-push-token (env/get-env-var "EXPO_PUSH_TOKEN") | ||
user (db/user-by-id user-id) | ||
_ (assert user) | ||
users (db/get-all-users) | ||
notifications (->> users | ||
;; TODO: remove the user who is changing status | ||
(filter :push_token) | ||
(mapv (fn [{:keys [screen_name push_token]}] | ||
{:to push_token | ||
:title "Status Change" | ||
:body (format "Your friend %s is now %s" | ||
screen_name | ||
status)})))] | ||
(expo/push-many! expo-push-token | ||
notifications))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
(ns sdk.expo | ||
(:require [clj-http.client :as http] | ||
[clojure.data.json :as json])) | ||
|
||
(def base-url "https://exp.host/--") | ||
|
||
;; From https://docs.expo.dev/push-notifications/sending-notifications/ | ||
|
||
;; curl -H "Content-Type: application/json" -X POST "https://exp.host/--/api/v2/push/send" -d '{ | ||
;; "to": "ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]", | ||
;; "title":"hello", | ||
;; "body": "world" | ||
;; }' | ||
|
||
(comment | ||
{:to "ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]" | ||
:title "hello" | ||
:body "world"}) | ||
|
||
(defn valid-notification? [{:keys [to title body]}] | ||
(and (string? to) | ||
(string? title) | ||
(string? body))) | ||
|
||
(comment | ||
;; example response | ||
{:data {:status "ok", :id "e9a83bbe-377a-45d7-a0d6-7b281c809e19"}}) | ||
|
||
(defn push-one! | ||
|
||
[expo-push-token {:keys [device-token title body] :as notification}] | ||
|
||
{:pre [(valid-notification? notification)]} | ||
|
||
(let [data {:to device-token | ||
:title title | ||
:body body} | ||
r (-> (str base-url "/api/v2/push/send") | ||
(http/post | ||
{:as :json | ||
:body (json/write-str data) | ||
:headers {"Content-Type" "application/json" | ||
"Authorization" (str "Bearer " expo-push-token)}}))] | ||
(if (= 200 (:status r)) | ||
(:data (:body r)) | ||
(throw (ex-info "Failed to send push notification" | ||
{:status (:status r) | ||
:body (:body r)}))))) | ||
|
||
(def MAX_EXPO_NOTIFICATIONS 100) | ||
|
||
(defn push-many! | ||
|
||
[expo-push-token notifications] | ||
|
||
{:pre [(string? expo-push-token) | ||
(< (count notifications) MAX_EXPO_NOTIFICATIONS) | ||
(every? valid-notification? notifications)]} | ||
|
||
(let [r (-> (str base-url "/api/v2/push/send") | ||
(http/post | ||
{:as :json | ||
:body (json/write-str notifications) | ||
:headers {"Content-Type" "application/json" | ||
"Authorization" (str "Bearer " expo-push-token)}}))] | ||
(if (= 200 (:status r)) | ||
(:data (:body r)) | ||
(throw (ex-info "Failed to send push notification" | ||
{:status (:status r) | ||
:body (:body r)}))))) |