1
0

Initial commit for push service

This commit is contained in:
2020-07-21 22:55:29 +02:00
commit 604cdd04b0
14 changed files with 583 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>push-service-parent</artifactId>
<groupId>de.77zzcx7</groupId>
<version>1-SNAPSHOT</version>
</parent>
<artifactId>push-service-client-lib</artifactId>
<packaging>jar</packaging>
<description>Client libraries for push-service</description>
<name>push-service-client-lib</name>
<dependencies>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,33 @@
package de.pushservice.client;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
public enum ResponseReason {
OK(HttpStatus.OK),
UNKNOWN_ERROR(HttpStatus.INTERNAL_SERVER_ERROR);
private final HttpStatus httpStatus;
ResponseReason(HttpStatus httpStatus) {
this.httpStatus = httpStatus;
}
public ResponseEntity toResponseEntity() {
return new ResponseEntity<>(this.name(), this.httpStatus);
}
public HttpStatus getHttpStatus() {
return this.httpStatus;
}
public static ResponseReason fromResponseEntity(ResponseEntity<String> entity) {
for (ResponseReason reason : values()) {
if (reason.name().equals(entity.getBody())) {
return reason;
}
}
return UNKNOWN_ERROR;
}
}

View File

@@ -0,0 +1,33 @@
package de.pushservice.client.dto;
import de.pushservice.client.model.Urgency;
public class MessageDto {
private String payload;
private String topic;
private Urgency urgency;
public String getPayload() {
return payload;
}
public void setPayload(String payload) {
this.payload = payload;
}
public String getTopic() {
return topic;
}
public void setTopic(String topic) {
this.topic = topic;
}
public Urgency getUrgency() {
return urgency;
}
public void setUrgency(Urgency urgency) {
this.urgency = urgency;
}
}

View File

@@ -0,0 +1,22 @@
package de.pushservice.client.dto;
public class NotificationRequestDto {
private SubscriptionDto subscriptionDto;
private MessageDto messageDto;
public SubscriptionDto getSubscriptionDto() {
return subscriptionDto;
}
public void setSubscriptionDto(SubscriptionDto subscriptionDto) {
this.subscriptionDto = subscriptionDto;
}
public MessageDto getMessageDto() {
return messageDto;
}
public void setMessageDto(MessageDto messageDto) {
this.messageDto = messageDto;
}
}

View File

@@ -0,0 +1,31 @@
package de.pushservice.client.dto;
public class SubscriptionDto {
private String auth;
private String endpoint;
private String key;
public void setAuth(String auth) {
this.auth = auth;
}
public String getAuth() {
return auth;
}
public void setKey(String key) {
this.key = key;
}
public String getKey() {
return key;
}
public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}
public String getEndpoint() {
return endpoint;
}
}

View File

@@ -0,0 +1,8 @@
package de.pushservice.client.model;
public enum Urgency {
VERY_LOW,
LOW,
NORMAL,
HIGH
}

View File

@@ -0,0 +1,28 @@
'use strict';
self.addEventListener('push', function(event) {
console.log('Received push');
let notificationTitle = 'Hello';
const notificationOptions = {
body: 'Thanks for sending this push msg.',
icon: './images/logo-192x192.png',
badge: './images/badge-72x72.png',
tag: 'simple-push-demo-notification',
data: {
url: 'https://developers.google.com/web/fundamentals/getting-started/push-notifications/',
},
};
if (event.data) {
const dataText = event.data.text();
notificationTitle = 'Received Payload';
notificationOptions.body = `Push data: '${dataText}'`;
}
event.waitUntil(
Promise.all([
self.registration.showNotification(
notificationTitle, notificationOptions),
])
);
});

View File

@@ -0,0 +1,121 @@
/**
* Step one: run a function on load (or whenever is appropriate for you)
* Function run on load sets up the service worker if it is supported in the
* browser. Requires a serviceworker in a `sw.js`. This file contains what will
* happen when we receive a push notification.
* If you are using webpack, see the section below.
*/
$(function () {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/service-worker.js').then(initialiseState);
} else {
console.warn('Service workers are not supported in this browser.');
}
});
/**
* Step two: The serviceworker is registered (started) in the browser. Now we
* need to check if push messages and notifications are supported in the browser
*/
function initialiseState() {
// Check if desktop notifications are supported
if (!('showNotification' in ServiceWorkerRegistration.prototype)) {
console.warn('Notifications aren\'t supported.');
return;
}
// Check if user has disabled notifications
// If a user has manually disabled notifications in his/her browser for
// your page previously, they will need to MANUALLY go in and turn the
// permission back on. In this statement you could show some UI element
// telling the user how to do so.
if (Notification.permission === 'denied') {
console.warn('The user has blocked notifications.');
return;
}
// Check is push API is supported
if (!('PushManager' in window)) {
console.warn('Push messaging isn\'t supported.');
return;
}
navigator.serviceWorker.ready.then(function (serviceWorkerRegistration) {
// Get the push notification subscription object
serviceWorkerRegistration.pushManager.getSubscription().then(function (subscription) {
// If this is the user's first visit we need to set up
// a subscription to push notifications
if (!subscription) {
subscribe();
return;
}
// Update the server state with the new subscription
sendSubscriptionToServer(subscription);
})
.catch(function(err) {
// Handle the error - show a notification in the GUI
console.warn('Error during getSubscription()', err);
});
});
}
/**
* Step three: Create a subscription. Contact the third party push server (for
* example mozilla's push server) and generate a unique subscription for the
* current browser.
*/
function subscribe() {
navigator.serviceWorker.ready.then(function (serviceWorkerRegistration) {
// Contact the third party push server. Which one is contacted by
// pushManager is configured internally in the browser, so we don't
// need to worry about browser differences here.
//
// When .subscribe() is invoked, a notification will be shown in the
// user's browser, asking the user to accept push notifications from
// <yoursite.com>. This is why it is async and requires a catch.
serviceWorkerRegistration.pushManager.subscribe({userVisibleOnly: true}).then(function (subscription) {
// Update the server state with the new subscription
return sendSubscriptionToServer(subscription);
})
.catch(function (e) {
if (Notification.permission === 'denied') {
console.warn('Permission for Notifications was denied');
} else {
console.error('Unable to subscribe to push.', e);
}
});
});
}
/**
* Step four: Send the generated subscription object to our server.
*/
function sendSubscriptionToServer(subscription) {
// Get public key and user auth from the subscription object
var key = subscription.getKey ? subscription.getKey('p256dh') : '';
var auth = subscription.getKey ? subscription.getKey('auth') : '';
// This example uses the new fetch API. This is not supported in all
// browsers yet.
return fetch('/profile/subscription', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
endpoint: subscription.endpoint,
// Take byte[] and turn it into a base64 encoded string suitable for
// POSTing to a server over HTTP
key: key ? btoa(String.fromCharCode.apply(null, new Uint8Array(key))) : '',
auth: auth ? btoa(String.fromCharCode.apply(null, new Uint8Array(auth))) : ''
})
});
}