use ntfy JSON API so notification titles support non-ASCII characters (#2398)

This commit is contained in:
Athou
2026-11-19 21:27:51 +01:00
parent fe5ff7f721
commit abf9e2cd85
3 changed files with 22 additions and 10 deletions

View File

@@ -95,14 +95,17 @@ public class PushNotificationService {
return; return;
} }
SimpleRequestBuilder request = SimpleRequestBuilder.post(serverUrl + "/" + topic); JsonObject json = new JsonObject();
request.addHeader("Title", feedTitle); json.put("topic", topic);
request.setBody(entryTitle, ContentType.TEXT_PLAIN.withCharset(StandardCharsets.UTF_8)); json.put("title", feedTitle);
json.put("message", entryTitle);
if (StringUtils.isNotBlank(entryUrl)) { if (StringUtils.isNotBlank(entryUrl)) {
request.addHeader("Click", entryUrl); json.put("click", entryUrl);
} }
SimpleRequestBuilder request = SimpleRequestBuilder.post(serverUrl);
request.setBody(json.toString(), ContentType.APPLICATION_JSON);
if (StringUtils.isNotBlank(settings.getUserSecret())) { if (StringUtils.isNotBlank(settings.getUserSecret())) {
request.addHeader("Authorization", "Bearer " + settings.getUserSecret()); request.addHeader("Authorization", "Bearer " + settings.getUserSecret());
} }

View File

@@ -64,16 +64,25 @@ class PushNotificationServiceTest {
userSettings.setServerUrl("http://localhost:" + mockServerClient.getPort()); userSettings.setServerUrl("http://localhost:" + mockServerClient.getPort());
userSettings.setTopic("test-topic"); userSettings.setTopic("test-topic");
userSettings.setUserSecret("test-secret"); userSettings.setUserSecret("test-secret");
subscription.setTitle("ąćęłńóśźż 📰 Test Feed");
mockServerClient mockServerClient
.when( .when(
HttpRequest.request() HttpRequest.request()
.withMethod("POST") .withMethod("POST")
.withPath("/test-topic") .withPath("/")
.withHeader("Title", "Test Feed")
.withHeader("Click", "http://example.com/entry")
.withHeader("Authorization", "Bearer test-secret") .withHeader("Authorization", "Bearer test-secret")
.withBody("Test Entry")) .withContentType(MediaType.APPLICATION_JSON_UTF_8)
.withBody(
JsonBody.json(
"""
{
"topic": "test-topic",
"title": "ąćęłńóśźż 📰 Test Feed",
"message": "Test Entry",
"click": "http://example.com/entry"
}
""")))
.respond(HttpResponse.response().withStatusCode(200)); .respond(HttpResponse.response().withStatusCode(200));
Assertions.assertDoesNotThrow(() -> pushNotificationService.notify(userSettings, subscription, entry)); Assertions.assertDoesNotThrow(() -> pushNotificationService.notify(userSettings, subscription, entry));

View File

@@ -40,7 +40,7 @@ class PushNotificationIT extends BaseIT {
@Test @Test
void receivedPushNotifications() throws IOException { void receivedPushNotifications() throws IOException {
// mock ntfy server // mock ntfy server
HttpRequest ntfyPost = HttpRequest.request().withMethod("POST").withPath("/ntfy/integration-test"); HttpRequest ntfyPost = HttpRequest.request().withMethod("POST").withPath("/ntfy");
getMockServerClient().when(ntfyPost).respond(HttpResponse.response().withStatusCode(200)); getMockServerClient().when(ntfyPost).respond(HttpResponse.response().withStatusCode(200));
// enable push notifications // enable push notifications