简体   繁体   English

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

[英]Sending push Notifications from Java Web Application

I have a Java web application which needs to send manual and automatic notifications to the user. 我有一个Java Web应用程序,需要向用户发送手动和自动通知。 This notification should go to the user's browser as well as the mobile devices (both iOS & Android). 此通知应发送到用户的浏览器以及移动设备(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? 我可以直接从Java代码发送通知吗? Or do I have to use FCM(Firebse)? 或者我必须使用FCM(Firebse)? If so can I call FCM directly from Java code, such as by calling it using Apache http client libraries? 如果是这样,我可以直接从Java代码调用FCM,例如通过使用Apache http客户端库调用它吗?
  2. How does the FCM, client's browser and my application connect? FCM,客户端浏览器和我的应用程序如何连接?
  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? 如何将其与Java代码集成?

You can send them directly without using FCM and there are several libraries available, including a Java one. 您可以直接发送它们而无需使用FCM,并且有几个可用的库,包括Java库。

Unfortunately iOS currently has no support for Web Push. 不幸的是,iOS目前不支持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. 如果浏览器的实例正在运行,则会显示推送到Android的通知,在现实世界中(对我来说无论如何)Chrome似乎总是在后台运行,所以我几乎可以实时收到通知。 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. 用户访问您的页面,您加载服务工作者并检查Web推送功能,如果满意您可以请求发送通知的权限。

  • 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. 您的webpush库作为服务器实例运行并负责所有加密和令牌处理,您可以配置它,但是您希望分发消息,通常是为了响应HTTP POST请求,但这取决于您。
  • 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. 您的服务工作者只是一个用javascript编写的脚本,您可以从该页面链接。 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. 您还可以使用它们来实现复杂的缓存规则,在离线时提供内容,在不同的浏览器窗口之间推送数据等。服务工作者可以产生更多的服务工作者,并且当他们在浏览器的主线程之外运行时,他们非常适合卸载cpu密集的任务,而不会延迟页面的呈现。

Final point to note, your site must be served over SSL to be able to deploy a service worker. 最后要注意的是,您的站点必须通过SSL提供才能部署服务工作者。

1) It depends a bit on where the Java code that sends a message runs. 1)它取决于发送消息的Java代码的运行位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM