Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Extend WebPushMessage with the custom settings #20304

Merged
merged 7 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,7 @@ self.addEventListener('message', (event) => {
self.addEventListener('push', (e) => {
const data = e.data?.json();
if (data) {
self.registration.showNotification(data.title, {
body: data.body,
});
self.registration.showNotification(data.title, data.options);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,47 @@

import java.io.Serializable;

import elemental.json.Json;
import elemental.json.JsonObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

/**
* Web Push message object containing an information to be shown in the
* notification.
*
* @since 24.2
*/
public record WebPushMessage(String title, String body) implements Serializable {
public record WebPushMessage(String title, ObjectNode options) implements Serializable {

private static final ObjectMapper objectMapper = new ObjectMapper();

/**
* Creates a new Web Push notification message with title and various options being fetched from a given arbitrary Java Object.
*
* @param title notification title
* @param options any Serializable Java Object representing custom settings that you want to apply to the notification
* @see <a href=https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/showNotification#parameters</a>
*/
public WebPushMessage(String title, Serializable options) {
tepi marked this conversation as resolved.
Show resolved Hide resolved
this(title, objectMapper.convertValue(options, ObjectNode.class));
}

/**
* Creates a new Web Push notification message with title and body.
* Creates a new Web Push notification message with just a title and body.
*
* @param title notification title
* @param body notification body
* @param body notification body
*/
public WebPushMessage {
public WebPushMessage(String title, String body) {
this(title, getBodyOption(body));
}

/**
* Creates a new Web Push notification message with just a title.
*
* @param title notification title
*/
public WebPushMessage(String title) {
this(title, (ObjectNode) null);
}

@Override
Expand All @@ -48,9 +71,21 @@ public String toString() {
* @return JSON representation of this message
*/
public String toJson() {
JsonObject json = Json.createObject();
ObjectNode json = objectMapper.createObjectNode();
json.put("title", title);
json.put("body", body);
return json.toJson();
if (options != null) {
json.set("options", options);
}
return json.toString();
}

private static ObjectNode getBodyOption(String body) {
if (body != null) {
ObjectNode objectNode = objectMapper.createObjectNode();
objectNode.put("body", body);
return objectNode;
} else {
return objectMapper.createObjectNode();
}
caalador marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading