Enable VAPID
This commit is contained in:
@@ -6,11 +6,12 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@Controller
|
||||
@RestController
|
||||
public class SubscriptionController {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(SubscriptionController.class);
|
||||
|
||||
@@ -23,4 +24,9 @@ public class SubscriptionController {
|
||||
|
||||
return this.subscriptionService.subscribe(subscriptionDto).toResponseEntity();
|
||||
}
|
||||
|
||||
@GetMapping("subscription/vapidPublicKey")
|
||||
public String getVapidPublicKey() {
|
||||
return this.subscriptionService.getVapidPublicKey();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package de.pushservice.client.dto;
|
||||
|
||||
import de.pushservice.client.model.Urgency;
|
||||
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
|
||||
|
||||
public class MessageDto {
|
||||
private String payload;
|
||||
@@ -36,4 +37,9 @@ public class MessageDto {
|
||||
public void setUrgency(Urgency urgency) {
|
||||
this.urgency = urgency;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return ReflectionToStringBuilder.toString(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package de.pushservice.client.dto;
|
||||
|
||||
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
|
||||
|
||||
public class NotificationRequestDto {
|
||||
private String scope;
|
||||
private MessageDto messageDto;
|
||||
@@ -24,4 +26,9 @@ public class NotificationRequestDto {
|
||||
public void setMessageDto(MessageDto messageDto) {
|
||||
this.messageDto = messageDto;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return ReflectionToStringBuilder.toString(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package de.pushservice.client.dto;
|
||||
|
||||
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Payload for a notification. Gets sent to the third party push service which forwards it to the user agent.
|
||||
@@ -60,4 +62,9 @@ public class PayloadDto {
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return ReflectionToStringBuilder.toString(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package de.pushservice.client.dto;
|
||||
|
||||
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
|
||||
|
||||
public class SubscriptionDto {
|
||||
private String auth;
|
||||
private String endpoint;
|
||||
@@ -37,4 +39,9 @@ public class SubscriptionDto {
|
||||
public void setScope(String scope) {
|
||||
this.scope = scope;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return ReflectionToStringBuilder.toString(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,4 +68,11 @@ public class SubscriptionService {
|
||||
LOGGER.error(String.format("Could not send notification! %s", responseReason));
|
||||
}
|
||||
}
|
||||
|
||||
public String getVapidPublicKey() {
|
||||
final ResponseEntity<String> responseEntity = new RestTemplate()
|
||||
.exchange(this.config.getServerUrl() + "vapidPublicKey", HttpMethod.GET, HttpEntity.EMPTY, String.class);
|
||||
|
||||
return responseEntity.getBody();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,9 +45,20 @@ function initializeState(registrationScope) {
|
||||
});
|
||||
}
|
||||
|
||||
function subscribe(registrationScope) {
|
||||
async function subscribe(registrationScope) {
|
||||
let response = await fetch('subscription/vapidPublicKey');
|
||||
let key = await response.text();
|
||||
options = {};
|
||||
|
||||
if (!key) {
|
||||
options = {userVisibleOnly: true};
|
||||
}
|
||||
else {
|
||||
options = {userVisibleOnly: true, applicationServerKey: base64UrlToUint8Array(key)};
|
||||
}
|
||||
|
||||
navigator.serviceWorker.ready.then(function (serviceWorkerRegistration) {
|
||||
serviceWorkerRegistration.pushManager.subscribe({userVisibleOnly: true}).then(function (subscription) {
|
||||
serviceWorkerRegistration.pushManager.subscribe(options).then(function (subscription) {
|
||||
return sendSubscriptionToServer(subscription, registrationScope);
|
||||
})
|
||||
.catch(function (e) {
|
||||
@@ -80,4 +91,20 @@ function sendSubscriptionToServer(subscription, registrationScope) {
|
||||
scope: registrationScope
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
function base64UrlToUint8Array(base64UrlData) {
|
||||
const padding = '='.repeat((4 - base64UrlData.length % 4) % 4);
|
||||
const base64 = (base64UrlData + padding)
|
||||
.replace(/\-/g, '+')
|
||||
.replace(/_/g, '/');
|
||||
|
||||
const rawData = atob(base64);
|
||||
const buffer = new Uint8Array(rawData.length);
|
||||
|
||||
for (let i = 0; i < rawData.length; ++i) {
|
||||
buffer[i] = rawData.charCodeAt(i);
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
Reference in New Issue
Block a user