Class NtfyNotifier
java.lang.Object
page.codeberg.friedolyn.notifications.Notifier
page.codeberg.friedolyn.notifications.NtfyNotifier
- All Implemented Interfaces:
Copyable<NtfyNotifier>,Redactable
A
Notifier that sends push notifications via the Ntfy service.
Requires the user to have the Ntfy app installed on their phone (e.g.
Android or have opened the
Progressive Web App. Moreover, it is necessary that the user has
subscribed to a specific topic on a Ntfy server.-
Nested Class Summary
Nested classes/interfaces inherited from class page.codeberg.friedolyn.notifications.Notifier
Notifier.Mood -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final @NonNull StringThehostthat shall be used by default if no other host is specified.static final @NonNull URLTheurlthat shall be used by default if no other URL is specified.private @NonNull StringThe domain name of the Ntfy server that shall relay the push notifications to the user's device.private @NonNull ntfyJava.core.publish.PubClientThe client that will be used to transmit the push notification to the Ntfy server.private @NonNull StringThe (hopefully unique) topic that the user has subscribed to on the Ntfy server and where all push notifications shall be sent to.private @NonNull URLThe URL of the Ntfy server that shall relay the push notifications to the user's device. -
Constructor Summary
ConstructorsConstructorDescriptionNtfyNotifier(@NonNull String topic) Creates a newNtfyNotifierwith the specified topic.NtfyNotifier(@NonNull String host, @NonNull URL url, @NonNull String topic) Creates a newNtfyNotifierwith the specified host, URL and topic. -
Method Summary
Modifier and TypeMethodDescription@NonNull NtfyNotifiercopy()voidsendNotification(@NonNull String subject, @NonNull String summary, @NonNull String message, @NonNull Notifier.Mood mood) Sends a push notification to the user's device via the Ntfy service.voidSets thehostof the Ntfy server to use.voidsetPubClient(@NonNull ntfyJava.core.publish.PubClient pubClient) Updates thepubClientthat shall be used to send the push notifications.voidUpdates thetopic.voidUpdates theurlof the Ntfy server to use.@NonNull StringtoString()@NonNull StringSame astoString(), but redacts all fields if the debug level is set toDebugLevel.WARNorDebugLevel.ERROR.Methods inherited from class page.codeberg.friedolyn.notifications.Notifier
sendNotification
-
Field Details
-
pubClient
@NonNull private @NonNull ntfyJava.core.publish.PubClient pubClientThe client that will be used to transmit the push notification to the Ntfy server. -
host
The domain name of the Ntfy server that shall relay the push notifications to the user's device. -
url
The URL of the Ntfy server that shall relay the push notifications to the user's device. -
topic
The (hopefully unique) topic that the user has subscribed to on the Ntfy server and where all push notifications shall be sent to. -
DEFAULT_HOST
-
DEFAULT_URL
-
-
Constructor Details
-
NtfyNotifier
Creates a newNtfyNotifierwith the specified topic.- Parameters:
topic- Where to send the push notifications to.
-
NtfyNotifier
public NtfyNotifier(@NonNull @NonNull String host, @NonNull @NonNull URL url, @NonNull @NonNull String topic) Creates a newNtfyNotifierwith the specified host, URL and topic.
-
-
Method Details
-
sendNotification
public void sendNotification(@NonNull @NonNull String subject, @NonNull @NonNull String summary, @NonNull @NonNull String message, @NonNull @NonNull Notifier.Mood mood) throws RuntimeException Sends a push notification to the user's device via the Ntfy service.- Specified by:
sendNotificationin classNotifier- Parameters:
subject- What the notification is about.summary- A short version of the message. Should be a one-liner that summarises the full message.message- The full message that should be sent to the user. Depending on the implementation, full messages might be too long to be displayed (in full) and might therefore be truncated or omitted entirely.mood- Whether the content of the message contains news that the user will likely perceive as positive, or negative (or neither – in which case the mood is neutral).- Throws:
RuntimeException- If the push notification could not be sent or if the subject, summary or message are blank.- See Also:
-
setHost
Sets thehostof the Ntfy server to use.- Parameters:
host- The new domain name of the Ntfy server to use.- Throws:
IllegalArgumentException- If the host is blank.
-
setTopic
Updates thetopic.- Parameters:
topic- The new topic where the push notifications shall be sent to. Should be unique for each user, so that no one receives notifications that are meant for someone else. Must not be blank.- Throws:
IllegalArgumentException- If the topic is blank.
-
setUrl
-
setPubClient
public void setPubClient(@NonNull @NonNull ntfyJava.core.publish.PubClient pubClient) Updates thepubClientthat shall be used to send the push notifications.- Parameters:
pubClient- The newPubClientto use.
-
copy
- Specified by:
copyin interfaceCopyable<NtfyNotifier>- Returns:
- A deep copy of the object, i.e. a clone that has the exact same values as the original object, but is a different instance (new reference).
-
toString
- Specified by:
toStringin interfaceRedactable- Specified by:
toStringin classNotifier- Returns:
- A string representation of this object including all fields, even sensitive ones.
- Implementation Note:
- We don't really care about the
pubClient, so we just callObject.toString()on it.
-
toStringRedacted
Same astoString(), but redacts all fields if the debug level is set toDebugLevel.WARNorDebugLevel.ERROR.- Specified by:
toStringRedactedin interfaceRedactable- Specified by:
toStringRedactedin classNotifier- Returns:
- A string representation of this object including all fields. However, sensitive fields will be removed or replaced with a placeholder. The exact behaviour is implementation-specific and may depend on the debug level currently in effect.
-