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

问题描述:

我有一个Java Web应用程序,它需要向用户发送手动和自动通知. 此通知应转到用户的浏览器以及移动设备(包括iOS和Android). 我发现,如果移动设备上没有运行本机应用程序,则无法直接向移动设备发送通知.因此,我唯一的选择似乎是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.

  1. 我可以直接从Java代码发送通知吗?还是我必须使用FCM(Firebse)?如果可以,我是否可以直接从Java代码中调用FCM,例如使用Apache http客户端库对其进行调用?
  2. FCM,客户端的浏览器和我的应用程序如何连接?
  3. 我还发现,服务人员应在后台运行以接收通知.如何将其与Java代码集成?

您可以直接发送它们而无需使用FCM,并且有几个可用的库,包括

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

不幸的是,iOS当前不支持Web Push.您对服务的订阅是在设备级别上进行的,而不是按用户进行的,因此,如果我在桌面上进行注册,则除非我再次在移动浏览器中进行注册,否则无法向移动设备发送通知.

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.

如果浏览器实例正在运行,则将显示推送到Android的通知(无论如何对我来说),Chrome似乎总是在后台某处运行,因此我可以实时获取通知.缺点是Web推送通知会直接进入通知阴影,它们不会首先在屏幕上弹出.

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:

  • 用户访问您的页面,加载Service Worker并检查网络推送 功能,如果满意,您可以请求发送权限 通知.

  • 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.

您可以在通知的有效负载中传递参数,并将其用作显示的通知中的变量,也可以硬编码值,您可以根据用户是否关注页面来指定不同的行为,您可以添加按钮并为其设置不同的操作,解雇后触发事件,自定义振动模式,替换或堆叠通知,访问现有通知中的数据等.所有这些都由您的服务人员处理,仅接收通知就无济于事全部.

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.

您的服务工作者只是一个用javascript编写的脚本,您可以从页面中进行链接.它是由浏览器在用户首次访问时加载并安装的,然后在调用时独立运行.

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.

服务人员非常强大.您还可以使用它们来实现复杂的缓存规则,在脱机时提供内容,在不同的浏览器窗口之间推送数据等.服务工作者可以产生更多的服务工作者,并且当它们在浏览器主线程之外运行时,它们是卸载cpu的理想选择繁琐的任务,而不会延迟页面的呈现.

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.

最后要注意的是,您的网站必须通过SSL进行服务,才能部署服务工作者.

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