在Android版Google Chrome上调用Javascript通知API不能直接从网页上进行 [英] Calling Javascript Notification API on Google Chrome for Android not working directly from a web page

查看:49
本文介绍了在Android版Google Chrome上调用Javascript通知API不能直接从网页上进行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JavaScript Notification API向我的用户显示一条小消息.它可以在我测试过的所有台式机浏览器上使用,包括Chrome ...但不适用于Android版Chrome(至少是KitKat).我读到的所有内容都表明,Android Chrome自2015年4月起就支持Notification API,并且该应用甚至具有Notification权限的设置...但是,当我从其中调用new Notification(...)时,什么也没有发生.实际上,即使能够获取权限信息等,即使Mozilla Notification API官方演示页面也没有显示它们.

I'm working with the JavaScript Notification API to show a small message to my users. It works on every desktop browser I've tested with, including Chrome...but not Chrome for Android (KitKat, at least). Everything I've read says that Android Chrome supports the Notification API as of April 2015, and the app even has settings for Notification permissions...yet nothing happens when I call new Notification(...) from within it. In fact, even the official Mozilla Notification API demo page doesn't show them, despite being able to grab permissions information, etc.

要使通知与Android Chrome兼容,我需要做些特别的事情吗?

Is there something special I need to do to make Notifications compatible with Android Chrome?

推荐答案

在Android版Chrome上,您只能通过

On Chrome for Android you can only open a notification via a Service Worker. A service worker is a script that runs alongside the browser and can be loaded even when the page or browser are currently closed.

服务工作者要求背后的主要原因是,通知可以超过浏览器的生存期(即用户可以关闭浏览器),并且该通知仍将处于活动状态,因此,当用户单击Android通知时需要唤醒某物",这某物"就是服务工作者.

The main reasoning behind the service worker requirement is that a notification can outlive the length of the browser's lifetime (i.e, the user can close the browser) and the notification will still be active, therefore when a user clicks on the notification Android needs to wake "something" up, this "something" is the Service Worker.

通知指南很好地介绍了Web和Chrome上的Notifications的当前状态-它仅侧重于Push消息传递,还提供了如何触发来自Service Worker的通知的所有详细信息.

This Notification Guide is a good introduction to the current state of Notifications on the Web and in Chrome - it focuses a little on Push messaging but also has all the details of how to trigger a notification from the Service Worker.

这似乎是一个额外的循环,但这实际上是一个净好处:即使单击了浏览器,您的通知在单击时仍将起作用.

This might seem like an extra hoop to jump through but it actually is a net benefit: your notification will still work when clicked even when the browser is closed.

这篇关于在Android版Google Chrome上调用Javascript通知API不能直接从网页上进行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆