Web通知API不适用于chrome。如何让它可行? [英] Web notification API is not working for chrome . How to make it workable ?

查看:58
本文介绍了Web通知API不适用于chrome。如何让它可行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





最近我正在尝试为我的网络应用创建Chrome通知。



https://alxgbsn.co.uk/2013/02/20/notify-js-a-handy-wrapper-for-the-web-notifications-api/



我已经按照上面的网站代码。



但是在测试时,这不适用于我的Chrome。



 <   !DOCTYPE     html  >  
< html lang = zh >
< head >
< meta < span class =code-attribute> charset = utf-8 / >
< title > Notify.js演示< / title >
< meta 名称 = description content = / >
< meta name = aut hor content = / >
< meta 名称 = viewport content = width = device-width,initial-scale = 1 / >
< meta name = robots content = noindex,nofollow / >
< 样式 >
button {
width 100px;
height 44px;
}
< / style >

< / head >
< body < span class =code-keyword>>
< h1 > Notify.js演示< / h1 >
< p > Notify.js是Web Notification API的简单包装器。 GitHub仓库是< a < span class =code-attribute> href = https://github.com/alexgibson/ notify.js > 此处< / a > < / p >
< p > 单击下面的按钮以启动通知。目前支持Chrome,Safari,Firefox和Firefox OS。< / p >
< 按钮 id = 我的按钮 > 通知我!< / button >

<! - JavaScript资产 - >
< script < span class =code-attribute> src = ../ notify.js > < / script >
< script >
document .getElementById(' my-button').addEventListener(' 点击'功能(){

function onShowNotification(){
console .log(' 显示通知!');
}

function onCloseNotification(){
console .log(' 通知已关闭!');
}

function onClickNotification(){
console .log(' 点击了通知!');
}

function onErrorNotification(){
console .error(' 显示通知时出错。您可能需要请求权限。');
}

function onPermissionGranted(){
console .log(' 用户')授予权限;
doNotification();
}

function onPermissionDenied(){
console .warn(' 用户')拒绝了权限;
}

function doNotification(){
var myNotification = new 通知(' Yo dawg!',{
body:' 这是一个很棒的通知'
tag:' 我的唯一ID'
notifyShow:onShowNotification,
notifyClose :onCloseNotification,
notifyClick:onClickNotification,
notifyError:onErrorNotification,
timeout: 4
});

myNotification.show();
}

if (!Notify.needsPermission){
doNotification();
} else if (Notify.isSupported()){
Notify.requestPermission (onPermissionGranted,onPermissionDenied);
}

}, false );
< / script >
< / body >
< / html >







请检查我上面已经下载了此代码的链接。

当我在线查看时,chrome运行良好,但是当我下载代码时。它已停止工作。它没有要求允许/阻止弹出窗口,也没有显示chrome上的通知弹出窗口。





请建议

解决方案

仅供参考:我测试了Chrome上的基本代码示例,来自Mozilla(MDN)的示例:

https://developer.mozilla.org/en-US/docs/Web/API/notification [ ^ ]。

(试试单击通知我按钮。)



快速测试显示 Notification.permission 设置为默认(未授予)和快速搜索显示配置内容:打开Chrome设置,点击显示高级设置,点击隐私部分,内容设置...按钮,找到该部分通知并根据自己的喜好允许/阻止它们。



Mozilla源代码的片段非常合理地评论状态

最后,如果用户拒绝通知,并且您希望得到尊重,则不再需要打扰他们。



享受。

-SA


Hi ,

recently I am trying to create chrome notification for my web app.

https://alxgbsn.co.uk/2013/02/20/notify-js-a-handy-wrapper-for-the-web-notifications-api/

I have followed above website code.

but while testing , this is not working on my chrome .

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Notify.js demo</title>
<meta name="description" content="" />
<meta name="author" content="" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="robots" content="noindex, nofollow" />
<style>
button {
	width: 100px;
	height: 44px;
}
</style>

</head>
<body>
    <h1>Notify.js demo</h1>
    <p>Notify.js is a simple wrapper for the Web Notification API. The GitHub repo is <a href="https://github.com/alexgibson/notify.js">here</a>.</p>
    <p>Click the button below to fire up a notification. Currently supported in Chrome, Safari, Firefox and Firefox OS.</p>
    <button id="my-button">Notify Me!</button>

    <!-- JavaScript assets -->
    <script src="../notify.js"></script>
    <script>
        document.getElementById('my-button').addEventListener('click', function () {

            function onShowNotification () {
                console.log('notification is shown!');
            }

            function onCloseNotification () {
                console.log('notification is closed!');
            }

            function onClickNotification () {
                console.log('notification was clicked!');
            }

            function onErrorNotification () {
                console.error('Error showing notification. You may need to request permission.');
            }

            function onPermissionGranted () {
                console.log('Permission has been granted by the user');
                doNotification();
            }

            function onPermissionDenied () {
                console.warn('Permission has been denied by the user');
            }

            function doNotification () {
                var myNotification = new Notify('Yo dawg!', {
                    body: 'This is an awesome notification',
                    tag: 'My unique id',
                    notifyShow: onShowNotification,
                    notifyClose: onCloseNotification,
                    notifyClick: onClickNotification,
                    notifyError: onErrorNotification,
                    timeout: 4
                });

                myNotification.show();
            }

            if (!Notify.needsPermission) {
                doNotification();
            } else if (Notify.isSupported()) {
                Notify.requestPermission(onPermissionGranted, onPermissionDenied);
            }

        }, false);
    </script>
</body>
</html>




Please check link I have given above from which I have downloaded this code .
When I check online , chrome works well , but when i downloaded the code . it has stopped working . It didnt ask for allow / block popup nor it is showing notification popup on chrome.


Please suggest

解决方案

Just for your information: I tested the rudimentary code sample on Chrome, the one from Mozilla (MDN):
https://developer.mozilla.org/en-US/docs/Web/API/notification[^].
(Try to click the button "Notify me".)

Quick testing shows then Notification.permission was set to "default" (not "granted") and quick search shows where to configure things: Open Chrome Settings, click "Show advanced settings", click, under the section "Privacy", the button "Content settings...", locate the section "Notification" and allow/block them to your liking.

The fragment of Mozilla source code comment pretty reasonably states

At last, if the user has denied notifications, and you want to be respectful there is no need to bother them any more.


Enjoy.

—SA


这篇关于Web通知API不适用于chrome。如何让它可行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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