java web 推送,从Java Web应用程序发送推送通知

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.

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?

How does the FCM, client's browser and my application connect?

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?

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值