Java push notification

java push notification

This time we are going to use the native Push API and Notification API to send web push notification without depending on any third-party libraries like one-signal, firebase etc.

Practically your back end can’t directly send push notifications to a browser, it always has to messages to a push service, and this service relays them to the browser, basically the address of this service is hardcoded into the browsers, you can’t change it and even you can’t self-host such services. Each browser seller runs its push service. Mozilla has Mozilla Push Service, and Chrome call it Firebase Cloud Messaging.

How to send push notification to android using java

To send message, your back end has to send push notifications always to the correct push server. For Firefox users use Mozilla’s push service and for Chrome users send messages to Google’s service. You don’t have to create an account for each push service, and they can be used for free without registration. Because all push services follow and implement the same API, you only need to implement the sending API key once in your application. The payload of push notifications must be encrypted. Back end encrypts the payload, and only the browser can decrypt it.

Steps to implement Java Push Notification

1. Create a spring boot project and following dependency in pom.xml file

<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.10.1</version>
</dependency>

2. The client side application or frontend consist index.html, main application and the Service Worker.

3. Next, we need to create a key pair; also called the VAPID key (Voluntary Application Server Identification). This key pair is used by the push service to uniquely identify your back end from our JavaScript application.The client needs to send the public key with the subscription request to the push service. The push service stores the public key in its database. When our back end creates a push message, it signs it with the private key and then sends the message with the signature to the push service. The push service validates the signature with the stored public key, and if valid, relays the message to the recipient.

4. Before we can subscribe to the push service, we need to install a Service Worker.

awaitnavigator.serviceWorker.register("/sw.js", {
scope: "/"
  });

5. Once we get the subscription object in hand, we now need to send this information to the server. In our case we send the subscription object in a POST request to our Spring Boot server.

await fetch("/subscribe", {
method: 'POST',
body: JSON.stringify(subscription),
headers: {
      "content-type": "application/json"
    }
  });

6. On the server, we store this information in a map with the endpoint URL as key. The endpoint URL is unique for each browser.

@PostMapping("/subscribe")
@ResponseStatus(HttpStatus.CREATED)
public void subscribe(@RequestBody Subscription subscription) {
this.subscriptions.put(subscription.getEndpoint(), subscription);
  }

How to send push notification to android using java?

How to implement push notification in java?

How to implement push notification in java web application?

Leave a Reply

Your email address will not be published. Required fields are marked *

*

code