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 String
Thehost
that shall be used by default if no other host is specified.static final @NonNull URL
Theurl
that shall be used by default if no other URL is specified.private @NonNull String
The domain name of the Ntfy server that shall relay the push notifications to the user's device.private @NonNull ntfyJava.core.publish.PubClient
The client that will be used to transmit the push notification to the Ntfy server.private @NonNull String
The (hopefully unique) topic that the user has subscribed to on the Ntfy server and where all push notifications shall be sent to.private @NonNull URL
The URL of the Ntfy server that shall relay the push notifications to the user's device. -
Constructor Summary
ConstructorsConstructorDescriptionNtfyNotifier
(@NonNull String topic) Creates a newNtfyNotifier
with the specified topic.NtfyNotifier
(@NonNull String host, @NonNull URL url, @NonNull String topic) Creates a newNtfyNotifier
with the specified host, URL and topic. -
Method Summary
Modifier and TypeMethodDescription@NonNull NtfyNotifier
copy()
void
sendNotification
(@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.void
Sets thehost
of the Ntfy server to use.void
setPubClient
(@NonNull ntfyJava.core.publish.PubClient pubClient) Updates thepubClient
that shall be used to send the push notifications.void
Updates thetopic
.void
Updates theurl
of the Ntfy server to use.@NonNull String
toString()
@NonNull String
Same astoString()
, but redacts all fields if the debug level is set toDebugLevel.WARN
orDebugLevel.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 newNtfyNotifier
with 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 newNtfyNotifier
with 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:
sendNotification
in 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 thehost
of 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 thepubClient
that shall be used to send the push notifications.- Parameters:
pubClient
- The newPubClient
to use.
-
copy
- Specified by:
copy
in 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:
toString
in interfaceRedactable
- Specified by:
toString
in 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.WARN
orDebugLevel.ERROR
.- Specified by:
toStringRedacted
in interfaceRedactable
- Specified by:
toStringRedacted
in 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.
-