2

I have a database in which I insert and send data using java.So I want to send push notifications using java to my web application and getting them in javascript.

I also want to send push notification to my browser.Is there an easy way to do these two things? I have been trying for days but I have not found a well explained tutorial that I can understand.

Thank you in advance.

E.Volt
  • 235
  • 1
  • 4
  • 8

3 Answers3

2

It sounds like you may want to try websockets which any modern browser will support.

Effectively, it opens a TCP channel between the browser (javascript) and the server. Messages can be written to the socket and received and handled by the browser.

An old but good introduction is: https://www.html5rocks.com/en/tutorials/websockets/basics/

The only other alternative would be to perform either polling at various intervals using javascript, or long polling. Both have issues, particularly at high volume. Websockets make more efficient use of network.

Chris
  • 254
  • 1
  • 11
1

If you want to display the notifications when the users are surfing your website then you can:

  1. Get the notifications from your server: you can use WebSockets, SSE or long polling with the Fetch API or AJAX.
  2. Display a notification to the user: you can use HTML and CSS or use the the Notification API.

If you want to display the notifications or process the push events even when your website is closed (like native apps) then you must use more advanced technologies. In particular:

  1. you need to use the Push API to send a message from your server to a browser
  2. the push event generated with the Push API activates a callback in the service worker
  3. the service worker process the event and display the notification using the Notification API

For web push I recommend Pushpad (I am the founder), so that you don't have to build from scratch. It has also a Java library.

collimarco
  • 34,231
  • 36
  • 108
  • 142
0

In short, no. There isn't a necessarily "easy" way to do it. Most browsers handle push notifications differently, and as far as sending push notifications to your web application, you should look up RESTful implementation, and once you have that set up, whenever data is passed to the web application, you will be able to capture it and send/receive any notification you'd like.

I would suggest looking into Firebase, as they have a pretty good implementation for push notifications.

Brandon Miller
  • 1,534
  • 1
  • 11
  • 16