From 405cef2bec6fef60d716bafb676a02a6218c4eda Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Sat, 30 Dec 2023 16:06:06 +0100 Subject: [PATCH] new: [ntfy] add Firebase Cloud Messaging (FCM) support --- ntfy/README.org | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ ntfy/lib/common | 37 +++++++++++++++++++++++++++++-- 2 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 ntfy/README.org diff --git a/ntfy/README.org b/ntfy/README.org new file mode 100644 index 0000000..94dcaf2 --- /dev/null +++ b/ntfy/README.org @@ -0,0 +1,58 @@ +# -*- ispell-local-dictionary: "english" -*- + +* Usage + +** Basic usage + +#+begin_src yaml +ntfy: + options: + ## These are default of the charm (different from default of `ntfy`) + #auth-default-access: deny-all + #enable-signup: false + #enable-login: true + #enable-reservations: true +#+end_src + +Any option from the help of [[https://docs.ntfy.sh/config/][ntfy]] can be used, to the exception of ~firebase-key-file~. + +To setup ~firebase~, there are some slight differences you can find here. + + +** Connection with =Firebase= + +I you build your own Android/IOS ntfy app, you can use Firebase Cloud +Messaging (FCM) to send notification through official channel. To +configure your server to send notification with Firebase you can: + +#+begin_src yaml +ntfy: + options: + firebase: ## content of your firebase service key file in yaml + type: service_account + project_id: myfirebase-project + private_key_id: 0ebce...41480 + private_key: | + -----BEGIN PRIVATE KEY----- + MzynkLGbScqN4XAWCU84Q6LLi6MFsnDyrA883Cdkttg1zI62q/BohgIck+897oyb + .. + ifQT/X7DVZl42p0M1mwwdAS8Ig== + -----END PRIVATE KEY----- + client_email: firebase-adminsdk-fodhh@ntfy-0k.iam.gserviceaccount.com + client_id: "101777552980915316312" + auth_uri: https://accounts.google.com/o/oauth2/auth + token_uri: https://oauth2.googleapis.com/token + auth_provider_x509_cert_url: https://www.googleapis.com/oauth2/v1/certs + client_x509_cert_url: https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-fxdhj%40myfirebase-project.iam.gserviceaccount.com +#+end_src + +To produce the output from the private service key file you created in +=Firebase= you could: + +#+begin_src sh +cat myfirebase-project-firebase-adminsdk-fxdhj-6eaewk4a4.json | yq -p json -o yaml +#+end_src + +To get more information about how to deploy your own app [[https://docs.ntfy.sh/config/#firebase-fcm][ntfy's doc +about FCM]] might be helpful. + diff --git a/ntfy/lib/common b/ntfy/lib/common index 497212a..3b54886 100644 --- a/ntfy/lib/common +++ b/ntfy/lib/common @@ -1,11 +1,44 @@ # -*- mode: shell-script -*- NTFY_CONFIGFILE="$SERVICE_CONFIGSTORE/etc/ntfy/server.yml" +NTFY_FIREBASE_KEYFILE="$SERVICE_CONFIGSTORE/etc/ntfy/firebase-key.json" ntfy:init() { service_def=$(get_compose_service_def "$SERVICE_NAME") - mkdir -p "${NTFY_CONFIGFILE%/*}" && - e "$service_def" | yq e '.options' - > "$NTFY_CONFIGFILE" + options=$(e "$service_def" | yq e '.options' -) + + mkdir -p "${NTFY_CONFIGFILE%/*}" || { + err "failed to create directory ${NTFY_CONFIGFILE%/*}" + exit 1 + } + + ## Ensure 'firebase-key-file' was not set in '$options' + if yq e -e '.firebase-key-file' <<< "$options" >/dev/null; then + err "${WHITE}firebase-key-file${NORMAL} should not be" \ + "specified in ${DARKYELLOW}$SERVICE_NAME${NORMAL}'s options" + exit 1 + fi + + ## if 'firebase' is set + if firebase_key=$(yq e -e '.firebase' 2>/dev/null <<< "$options"); then + ## set 'firebase-key-file' in '$options' + options=$(yq e '.firebase-key-file = "/etc/ntfy/firebase-key.json"' <<< "$options") || { + err "failed to set firebase-key-file in options" + exit 1 + } + umask 077 + ## store content of $firebase_key in json format in '$NTFY_FIREBASE_KEYFILE' + e "$firebase_key" | yq -p yaml -o json . > "$NTFY_FIREBASE_KEYFILE" || { + err "failed to store firebase-key-file" + exit 1 + } + fi + + e "$options" > "$NTFY_CONFIGFILE" || { + err "failed to store ntfy config" + exit 1 + } + } ntfy:config() {