如何向 Web 应用程序和浏览器发送推送通知?

问题描述:

我有一个数据库,我在其中使用 java 插入和发送数据.所以我想使用 java 向我的 Web 应用程序发送推送通知并在 javascript 中获取它们.

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.

提前致谢.

听起来您可能想尝试任何现代浏览器都支持的 websockets.

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

实际上,它在浏览器 (javascript) 和服务器之间打开了一个 TCP 通道.消息可以写入套接字并由浏览器接收和处理.

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.

一个古老但很好的介绍是:https://www.html5rocks.com/en/tutorials/websockets/basics/

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

唯一的另一种选择是使用 javascript 以不同的时间间隔执行轮询或长轮询.两者都有问题,特别是在高容量时.Websocket 可以更有效地利用网络.

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.