Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending push Notifications from Java Web Application

I have a Java web application which needs to send manual and automatic notifications to the user. This notification should go to the user's browser as well as the mobile devices (both iOS & Android). I found out there is no way to send notifications to mobile devices directly if there is no native application running on the mobile device. So my only option seems to be web push notifications. I went through a few articles and I found it very confusing. I don't know where to start.

  1. Can I send the notifications directly from Java Code? Or do I have to use FCM(Firebse)? If so can I call FCM directly from Java code, such as by calling it using Apache http client libraries?
  2. How does the FCM, client's browser and my application connect?
  3. I also found out that a service worker should run in the background to receive the notifications. How do I integrate it with the Java code?
like image 496
UserR Avatar asked Jan 26 '18 17:01

UserR


People also ask

How to send push notifications from mobile app to server?

When an event occurs, service will accept the parameters from the mobile or web app related to particular event like user id, userName, etc. Next the service initiates push of information by sending parameters and URI to the server. Server will identify the registered devices with the help of URI and send push notifications by those parameters.

What is the web push notifications protocol?

The Web Push Notifications protocol is relatively new. It gives web applications the ability to act as native applications and receive messages pushed to them from a server at any time even when the web app is not active or not currently loaded in a browser.

What are the benefits of push notifications for your application?

The overall value of your application increases too, because push notifications make your application more useful for your users. This improves the usability of web applications and moves us closer to developing a single web application for all platforms instead of having to develop a native application for every platform.

How do I create a key pair for push notifications?

You can create this key pair in different ways. For example, you could do this beforehand with a command-line tool and then hardcode the public key into the JavaScript application and the private key into the application that sends the push notifications. In this demo application, I create the key pair when the Spring Boot application starts up.


1 Answers

You can send them directly without using FCM and there are several libraries available, including a Java one.

Unfortunately iOS currently has no support for Web Push. Subscriptions to your service are on a device level rather than by user, so if I sign up on my desktop you cannot send notifications to my mobile unless I sign up again in my mobile browser.

Notifications pushed to Android will be displayed if an instance of the browser is running, in the real world (for me anyway) Chrome always seems to be running in the background somewhere so I get notifications through in pretty much real time. The downside is web push notifications go straight into the notification shade, they do not pop up on screen first.

The rough workflow goes like this:

  • User visits your page, you load service worker and check for web push capability, if satisfied you can request permission to send notifications.

  • If user grants permission you pass your public key to your service worker to create a subscription for that user, this returns an endpoint and two keys which you need to push a notification to them.

  • Your webpush library runs as a server instance and takes care of all the encryption and token handling, you configure it however you like to dispatch messages, usually in response to HTTP POST requests but it's up to you.
  • Within your service worker you define an event handler for receipt of a push message. This is where you create and display the notification to the user, again how you do this is up to you.

You can pass parameters in the payload of the notification and use them as variables within the notification you display or you can hard code values, you can specify different behaviours depending upon whether the user has your page in focus or not, you can add buttons and set different actions for them, trigger events upon dismissal, customise the vibrate pattern, replace or stack the notifications, access the data in existing notifications etc etc. All this is handled by your service worker, receiving the notification alone does nothing at all.

Your service worker is just a script written in javascript which you link from your page. It is loaded and installed by the browser the first time a user visits and then runs independently when invoked.

Service workers are very powerful. You can also use them to implement complex caching rules, serve content while offline, push data between different browser windows etc. A service worker can spawn more service workers and as they run outside of the main thread of your browser they are ideal for offloading cpu intensive tasks to without delaying the rendering of your page.

Final point to note, your site must be served over SSL to be able to deploy a service worker.

like image 106
miknik Avatar answered Oct 19 '22 22:10

miknik